⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 screen.c

📁 嵌入式开发中触摸屏的显示读写设置,可以显示文字等简单功能
💻 C
📖 第 1 页 / 共 2 页
字号:
  int horiz, vert;
  unsigned short * f1, * f2, * pix, * p;
  unsigned char * p8;
  int colorDepth = DM_GetColorDepth();

  /* Announce the start of the test and set up the screen
   */
  //DM_StartTest("Color Bars");

  /* Clear the screen and get the frame buffer pointer
   */
  DM_ClearScreen();
  DM_GetScreenGeometry(&horiz, &vert, NULL, NULL);
  if ((pix = (unsigned short*)DM_GetFrameBuffer()) == NULL) {
    DM_Error("Screen has no frame buffer");
    return;
  }

  /* Draw bars on non interlaced display
   */
  if ((f1=(unsigned short *)DM_GetFieldBuffer(0)) ==
      (f2=(unsigned short *)DM_GetFieldBuffer(1))) {

    /* Cycle thru the colors and draw a bar for each
     */
    for (k=0; k < 8; k++) {
      unsigned color = DM_GetColor(k);
      int width = horiz/8;
      if (colorDepth < 16)
	p8 = (unsigned char*)pix + k*width;
      else
	p = pix + k*width;
      for (j=vert/10; j < vert*9/10; j++) {
	for (i=0; i < width; i += 2) {
	  if (colorDepth < 16) {
	    p8[j*horiz+i] = color;
	    p8[j*horiz+i+1] = color;
	  }
	  else {
	    p[j*horiz+i] = color >> 16;
	    p[j*horiz+i+1] = color & 0xffff;
	  }
	}
      }
    }
  }
  /* Draw bars on interlaced display
   */
  else {
    /* Cycle thru the colors and draw a bar for each
     */
    for (k=0; k < 8; k++) {
      unsigned color = DM_GetColor(k);
      int width = horiz/8;
      for (j=vert/10; j < vert*9/10; j++) {
	p = j&1 ? f2 + k*width : f1 + k*width;
	for (i=0; i < width; i += 2) {
	  p[j/2*horiz+i] = color >> 16;
	  p[j/2*horiz+i+1] = color & 0xffff;
	}
      }
    }
  }

  /* Draw a box around the bars
   */
  DM_DrawRect(2,vert/10,horiz-2,vert*9/10,DM_GetColor(DM_ColorBlack),2);

  /* Paint text and wait for key
   */
  DM_AnyKeyToContinue(NULL);

  //DM_FinishTest("Color Bars Complete");
}
#endif // 0

/*----------------------------------------------------------------------
 * Screen color foreground/background pairs
 */
#define MAX_COLOR 8
int colors[MAX_COLOR][2] = {
  DM_ColorBlack, DM_ColorWhite,
  DM_ColorBlack, DM_ColorYellow,
  DM_ColorBlack, DM_ColorCyan,
  DM_ColorBlack, DM_ColorMagenta,
  DM_ColorWhite, DM_ColorBlue,
  DM_ColorWhite, DM_ColorRed,
  DM_ColorWhite, DM_ColorBlack,
  DM_ColorWhite, DM_ColorGreen
};

#if 0
/*----------------------------------------------------------------------
 * Test rectangle filling
 */
static
void fillBoxes(void * arg)
{
  int h = horizPixel/2;
  int v = vertPixel/2;
  int x1, y1, x2, y2;
  int x, y, maxX, maxY;
  int color;

  DM_ClearScreen();
  DM_GetScreenGeometry(&maxX, &maxY, NULL, NULL);

  do {
    x1 = horizPixel * (float)rand()/RAND_MAX;
    y1 = vertPixel * (float)rand()/RAND_MAX;
    x = h * (float)rand()/RAND_MAX;
    y = v* (float)rand()/RAND_MAX;
    color = rand()/(RAND_MAX >> 3);
    x1 -= x/2;
    y1 -= y/2;
    if (x1 < 0)
      x1 = 0;
    if (y1 < 0)
      y1 = 0;
    x2 = x1 + x;
    y2 = y1 + y;
    if (x2 > maxX)
      x2 = maxX;
    if (y2 > maxY)
      y2 = maxY;

    DM_FillRect(x1,y1,x2,y2,DM_GetColor(color));
    DM_WaitMs(50);
  } while (!DM_StopTest(NULL));
}
#endif // 0

/*----------------------------------------------------------------------
 * I N T E R F A C E   F U N C T I O N S
 */
static
void printChar(ScreenContextT * ctxP,
	      char c,
	      int row,
	      int col,
	      unsigned fgColor,
	      unsigned bgColor)
{
  /* check if row/col in range */
  if ((row < 0) || (row > maxCharRow) || (col < 0) || (col > maxCharCol)) {
    return;
  }

  /* Check to see if were printing before init */
  if (fbP == NULL)
    return;

  if (fieldOffset)
    drawCharX(c, row, col, fgColor, bgColor, setPixelTV);
  else if (colorDepth < 16)
    drawCharX(c, row, col, fgColor, bgColor, setPixel8);
  else
    drawCharX(c, row, col, fgColor, bgColor, setPixel16);
}

/*----------------------------------------------------------------------
 * Print a string
 */
static
void printRow(ScreenContextT * ctxP,
	      char * m,
	      int row,
	      int col,
	      unsigned fgColor,
	      unsigned bgColor,
	      int flags)
{
  int i;

  if (row >= maxCharRow)
    row = maxCharRow-1;

  for (i=0; m[i]; i++) {
    if (i==0 && ((m[i] == '\n') || (flags & DM_PrintFlagCenter))) {
      /* use '\n' to center line */
      col = (maxCharCol - strlen(&m[1])) / 2;
      if ((col < 0) || (col > maxCharCol))
	col = 0;
    }
    if (m[i] != '\n') {
      if (m[i] >= ' ')
        printChar(ctxP, m[i],row,col+i,fgColor,bgColor);
      else
        printChar(ctxP, ' ',row,col+i,fgColor,bgColor);
    }
  }
}

/*----------------------------------------------------------------------
 * Scroll rows in a region
 */
static
void scrollRows(ScreenContextT * ctxP,
		int nStartRow,
		int nEndRow,
		int nRows,
		unsigned bg)
{
  if (fieldOffset)
    scrollRowsInterleave(nStartRow, nEndRow, nRows, bg);
  else if (colorDepth < 16)
    scrollRowsNonInterleave8(nStartRow, nEndRow, nRows, bg);
  else
    scrollRowsNonInterleave(nStartRow, nEndRow, nRows, bg);
}

/*----------------------------------------------------------------------
 * Clear rows in a region
 */
static
void clearRows(ScreenContextT * ctxP,
	       int nStartRow,
	       int nRows,
	       unsigned clearChar)
{
  
    if (fieldOffset)
    {
        clearRowsX(nStartRow, nRows, clearChar, setPixelTV);
    }
    else if (colorDepth < 16)
    {
        clearRowsX(nStartRow, nRows, clearChar, setPixel8);
    }
    else
    {  
        clearRowsX(nStartRow, nRows, clearChar, setPixel16);
    }
}

/*----------------------------------------------------------------------
 * Draw a line
 */
static
void drawLine(ScreenContextT * ctxP,
	      int from_x, int from_y,
	      int to_x, int to_y,
	      unsigned fg,
	      int width)
{
  if (fieldOffset)
    drawLineX(from_x, from_y, to_x, to_y, fg, width, setPixelTV);
  else if (colorDepth < 16)
    drawLineX(from_x, from_y, to_x, to_y, fg, width, setPixel8);
  else
    drawLineX(from_x, from_y, to_x, to_y, fg, width, setPixel16);
}

/*----------------------------------------------------------------------
 * Fill a rectangle
 */
static
void fillRect(ScreenContextT * ctxP,
	      int x1, int y1,
	      int x2, int y2,
	      unsigned fg)
{
  if (fieldOffset)
    fillRectX(x1, y1, x2, y2, fg, setPixelTV);
  else if (colorDepth < 16)
    fillRectX(x1, y1, x2, y2, fg, setPixel8);
  else
    fillRectX(x1, y1, x2, y2, fg, setPixel16);
}

/*----------------------------------------------------------------------
 * Get the bounding box for a region of text
 */
static
void getBBox(ScreenContextT * ctxP,
	      int start_row, int rows, int start_col, int cols,
	      DM_OutputBBox_T * bbox)
{
  int fontVert = FONT_VERT_PIX;
  int fontHoriz = FONT_HORZ_PIX;
  bbox->x1 = start_col * (fontHoriz+1);
  bbox->y1 = start_row * (fontVert-2);
  bbox->x2 = (start_col+cols) * (fontHoriz+1);
  bbox->y2 = (start_row+rows) * (fontVert-2);
}

/*----------------------------------------------------------------------
 * Return the current screen geometry
 */
static
void getScreenGeometry(ScreenContextT * ctxP,
		       int * widthP,
		       int * linesP,
		       int * charRowP,
		       int * charColP)
{
  if (widthP)
    *widthP = horizPixel;
  if (linesP)
    *linesP = vertPixel;
  if (charRowP)
    *charRowP = maxCharRow;
  if (charColP)
    *charColP = maxCharCol;
}

/*----------------------------------------------------------------------
 * Return the current color space (RGB, YUV etc)
 */
static
DM_ColorSpace_T getColorSpace(ScreenContextT * ctxP)
{
  LcdContextT * lcdP = (LcdContextT *)ctxP->lcdP;
  return (DM_ColorSpace_T)lcdP->getColorSpaceFnP(lcdP);
}

/*----------------------------------------------------------------------
 * Return the current color depth (4, 8, 16)
 */
static
int getColorDepth(ScreenContextT * ctxP)
{
  LcdContextT * lcdP = (LcdContextT *)ctxP->lcdP;
  return lcdP->getColorDepthFnP(lcdP);
}

/*----------------------------------------------------------------------
 * Return the current display format
 */
static
DM_DisplayFormat_T getDisplayFormat(ScreenContextT * ctxP)
{
  LcdContextT * lcdP = (LcdContextT *)ctxP->lcdP;
  return (DM_DisplayFormat_T)lcdP->getDisplayFormatFnP(lcdP);
}

/*----------------------------------------------------------------------
 * Fill the entire screen with the fill color
 */
static
void clearScreen(ScreenContextT * ctxP, unsigned bgColor)
{
  if (fieldOffset)
    clearLinesX(0,vertPixel,bgColor,setPixelTV);
  else if (colorDepth < 16)
    clearLinesX(0,vertPixel,bgColor,setPixel8);
  else
    clearLinesX(0,vertPixel,bgColor,setPixel16);
}

/*----------------------------------------------------------------------
 * Get a pointer to the frame pixel buffer (field 0 if interlaced)
 */
static
void * getFrameBufferPix(ScreenContextT * ctxP)
{
  return (void*)fbP->pixelP;
}

/*----------------------------------------------------------------------
 * Get a pointer to the field buffer pixel buffer
 */
static
void * getFieldBufferPix(ScreenContextT * ctxP,
			 int field)
{
  return field ? fbP->pixelP : fbP->pixelP + fieldOffset;
}

/*----------------------------------------------------------------------
 * Set the display format
 */
static
void setDisplay(ScreenContextT * ctxP,
		int dt,
		int cs)
{
  LcdContextT * lcdP = (LcdContextT *)ctxP->lcdP;

  // Turn Lcd panel on
  Lubbock_MiscWriteRegister(1, LCD_DISP);
  
  DM_WaitMs(50); /* Give the power some settling time */

  /* Setup the LCD controller
   */
  lcdP->setupFnP(lcdP, dt, cs);

  /* Get the screen geometry and setup the character limits
   */
  lcdP->getGeometryFnP(lcdP, &horizPixel, &vertPixel);
  lcdP->getOffsetsFnP(lcdP, &horizOffset, &vertOffset, &fieldOffset);
  maxCharCol = (horizPixel-horizOffset) / (FONT_HORZ_PIX+1);
  maxCharRow = (vertPixel-vertOffset) / (FONT_VERT_PIX-2);
  colorDepth = lcdP->getColorDepthFnP(lcdP);

  /* Get a pointer to the current frame buffer
   */
  fbP = (DM_FrameBuffer_T*)lcdP->getFrameBufferFnP(lcdP);
 // sprintf (s, "Frame Buffer: %X", fbP);
 // DM_PrintSerial(s);
  
  /* Finally turn on the display
   */
  lcdP->onFnP(lcdP);
//  DM_DumpRegistersDbg ((UINT*)0x44000000, 4);
//  DM_DumpRegistersDbg ((UINT*)0x44000200, 8);
//  DM_DumpRegistersDbg ((UINT*)0x44000038, 1);
}


/*----------------------------------------------------------------------
 * Set the current frame buffer for drawing. Allocate a new one if no
 * pointer is given.
 */
static
void * offScreenBuffer(ScreenContextT * ctxP, void * bufP)
{

  LcdContextT * lcdP = (LcdContextT *)ctxP->lcdP;
  if (bufP == NULL)
    bufP = lcdP->newFrameBufferFnP(lcdP);
  fbP = (DM_FrameBuffer_T*)bufP;
  return bufP;
}

/*----------------------------------------------------------------------
 * Set the current frame buffer for display and drawing.
 */
static
void setOffScreenBuffer(ScreenContextT * ctxP, void * bufP)
{
  LcdContextT * lcdP = (LcdContextT *)ctxP->lcdP;
  lcdP->setFrameBufferFnP(lcdP, (DM_FrameBuffer_T*)bufP);
  fbP = lcdP->getFrameBufferFnP(lcdP);
}

/*----------------------------------------------------------------------
 * Clock change callback handler
 */
static
void clockChangeFunc(ScreenContextT * ctxP,
		     int reason,
		     void * rate)
{
  LcdContextT * lcdP = (LcdContextT *)ctxP->lcdP;
  if (reason == DM_CallbackReasonPreClockChange) {
  }
  else if (reason == DM_CallbackReasonPostClockChange) {
    /* Get the screen geometry and setup the character limits
     */
    lcdP->getGeometryFnP(lcdP, &horizPixel, &vertPixel);
    lcdP->getOffsetsFnP(lcdP, &horizOffset, &vertOffset, &fieldOffset);
    maxCharCol = (horizPixel-horizOffset) / (FONT_HORZ_PIX+1);
    maxCharRow = (vertPixel-vertOffset) / (FONT_VERT_PIX-2);

    /* Get a pointer to the current frame buffer
     */
    fbP = (DM_FrameBuffer_T*)lcdP->getFrameBufferFnP(lcdP);
  }
}

/*----------------------------------------------------------------------
 * Clock change callback info record
 */
static DM_Callback_T clockChange = {
  (DM_CallbackFunc_T)clockChangeFunc
};

/*
*******************************************************************************
*
* FUNCTION:
*    ScreenSWInit
*
* DESCRIPTION:
*    Initialize the Device Context Structure for this device.
*
* INPUT PARAMETERS:	
*	 None
*
* RETURNS:
*    None
*
* GLOBAL EFFECTS:
*    None
*
* ASSUMPTIONS:
*    None
*
* CALLS:
*    None
*
* CALLED BY:
*
* PROTOTYPE:
*    void ScreenInit(void)
*
*******************************************************************************
*/
VOID ScreenSWInit(void)
{
	ScreenContextT * ctxP = &Screen;
	ctxP->lcdP = &Lcd;
//	clockChange.params = funcP;
	DM_RegisterClockChangeCallback(&clockChange);

	ctxP->printStringFnP = (DM_OutputPrintString_T)printRow;
	ctxP->printCharFnP =   (DM_OutputPrintChar_T)	printChar;
	ctxP->clearScreenFnP = (DM_OutputClearScreen_T)clearScreen;
	ctxP->clearRegionFnP = (DM_OutputClearRegion_T)clearRows;
	ctxP->scrollRegionFnP = (DM_OutputScrollRegion_T)scrollRows;
	ctxP->getGeometryFnP = (DM_OutputGetGeometry_T)getScreenGeometry;
  	ctxP->getColorSpaceFnP = (DM_OutputGetColorSpace_T)getColorSpace;
	ctxP->getColorDepthFnP = (DM_OutputGetColorDepth_T)getColorDepth;
	ctxP->getDisplayFormatFnP = (DM_OutputGetDisplayFormat_T)getDisplayFormat;
	ctxP->getFrameBufferFnP = (DM_OutputGetFrameBuffer_T)getFrameBufferPix;
	ctxP->getFieldBufferFnP = (DM_OutputGetFieldBuffer_T)getFieldBufferPix;
	ctxP->setDisplayFnP = (DM_OutputSetDisplay_T)setDisplay;
	ctxP->drawLineFnP = (DM_OutputDrawLine_T)drawLine;
	ctxP->getBBoxFnP = (DM_OutputGetBBox_T)getBBox;
	ctxP->fillRectFnP = (DM_OutputFill_T)fillRect;
	ctxP->offScreenBufferFnP = (DM_OutputOffScreenBuffer_T)offScreenBuffer;
	ctxP->setOffScreenBufferFnP = (DM_OutputSetOffScreenBuffer_T)setOffScreenBuffer;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -