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

📄 lcdhal.c

📁 STM32+Grlib
💻 C
📖 第 1 页 / 共 3 页
字号:
/**
  * @brief  Sets the Font (Big or Small).
  * @param  uFont: specifies the Font (GL_FONT_BIG or GL_FONT_SMALL).
  * @retval None
  */
void GL_SetFont(uint8_t uFont)
{
  GL_Font = uFont;

  switch (uFont)
  {
    case GL_FONT_BIG:
      GL_FontWidth  = GL_FONT_BIG_WIDTH;
      GL_FontHeight = GL_FONT_BIG_HEIGHT;
      break;

    case GL_FONT_SMALL:
      GL_FontWidth  = GL_FONT_SMALL_WIDTH;
      GL_FontHeight = GL_FONT_SMALL_HEIGHT;
      break;

    default:
      break;
  } /* End switch */
}

/**
  * @brief  Switches the backlight either ON or OFF
  * @param  state. This parameter can be one of the following values:
  *     @arg   GL_ON
  *     @arg   GL_OFF
  * @retval None
  */
void GL_BackLightSwitch(uint8_t u8_State)
{
  if (u8_State == GL_OFF)
  { /* Turning OFF the LCD Backlight */
    /* GPIO_ResetBits(LCD_GPIO_DATA_PORT, GPIO_Pin_4); */
    LCD_DisplayOff();
  }
  else if (u8_State == GL_ON)
  { /* Turning ON the LCD Backlight */
    /* GPIO_SetBits(LCD_GPIO_DATA_PORT, GPIO_Pin_4); */
    LCD_DisplayOn();
  }
}

/**
  * @brief  Initializes the LCD.
  * @param  None
  * @retval None
  */
void GL_LCD_Init(void)
{
    /* Setups the LCD */
#if defined(USE_STM3210C_EVAL)
  STM3210C_LCD_Init();  
#elif defined (USE_STM3210B_EVAL)
  STM3210B_LCD_Init();
#elif  defined (USE_STM32100B_EVAL)
  STM32100B_LCD_Init();
#elif defined(USE_STM3210E_EVAL)
  STM3210E_LCD_Init();
#elif defined(USE_STM32100E_EVAL)
  STM32100E_LCD_Init();
#elif defined(USE_STM322xG_EVAL)
  STM322xG_LCD_Init();
#elif defined(USE_STM32L152_EVAL)  
  STM32L152_LCD_Init();
#endif
}

/**
  * @brief  Disables LCD Window mode.
  * @param  None
  * @retval None
  */
void GL_LCD_WindowModeDisable(void)
{
  LCD_SetDisplayWindow(239, 0x13F, 240, 320);
  LCD_WriteReg(R3, 0x1018);
}

/**
  * @brief  Draw one pixel at position given by Xpos, Ypos of color Color.
  *     Three different modes are specified by PixelSpec in order to save time:
  *     1.FirstPixel, MiddlePixel and LasPixel are used for fastened block-writing
  *       to GRAM (for instance: drawing rectangle, horizontal line etc.)
  *     2.SinglePixel is used for drawing stand-alone pixel. (for instance:
  *       drawing circle, character etc.).
  * @param  Xpos: specifies X position
  * @param  Ypos: specifies Y position
  * @param  Color: RGB color of point
  * @param  PixelSpec: specifies Mode of putting pixel.
  *         This parameter can be one of the following values:
  *     @arg  - FirstPixel:  Starting pixel of block-writing sequence.
  *     @arg  - MiddlePixel: Middle-located pixel of block-writing sequence.
  *     @arg  - LasPixel:    Ending pixel of block-writing sequence
  *     @arg  - SinglePixel: Separated pixel.
  * @retval None
  */
void LCD_PutPixel(uint16_t Xpos, uint16_t Ypos, uint16_t Color, uint8_t PixelSpec)
{
  /*Start part of put pixel for first pixel of block and for single one.
  It consists of set the cursor's position and GRAM prepare sequence.*/
  if ((PixelSpec & (FirstPixel | SinglePixel)) != GL_RESET)
  {
    LCD_SetCursor(Xpos, Ypos);
    LCD_WriteRAM_Prepare();
  }

  /*Write pixel's color to GRAM. Obligatory for all modes of PutPixel call.*/
  LCD_WriteRAM(Color);

  if ( pLcdHwParam.LCD_Connection_Mode == GL_SPI)
  {
    /*End part of putting pixel for last pixel of block or single pixel.
    Close usage of display by setting CS high.*/
    if ((PixelSpec &(LastPixel | SinglePixel) ) != GL_RESET)
    {
      if ( pLcdHwParam.LCD_Connection_Mode == GL_SPI )
      {
        GL_LCD_CtrlLinesWrite(pLcdHwParam.LCD_Ctrl_Port_NCS, pLcdHwParam.LCD_Ctrl_Pin_NCS, GL_HIGH);
      }
    }
  }
}

/**
  * @brief  Get color of pixel located at appropriate position
  * @param  Xpos: specifies X position
  * @param  Ypos: specifies Y position
  * @retval uint16_t - RGB color of required pixel.
  */
uint16_t LCD_GetPixel(uint16_t Xpos, uint16_t Ypos)
{
  uint16_t tmpColor = 0, tmpRed = 0, tmpBlue = 0;

  /*Read the color of given pixel*/
  LCD_SetCursor(Xpos, Ypos);
  if ( pLcdHwParam.LCD_Connection_Mode == GL_SPI)
  {
    tmpColor = LCD_ReadReg(R34);
  }
  else
  {
    tmpColor = GL_LCD_ReadRAM();
  }

  /*Swap the R and B color channels*/
  tmpRed = (tmpColor & Blue) << 11;
  tmpBlue = (tmpColor & Red) >> 11;
  tmpColor &= ~(Red | Blue);
  tmpColor |= (tmpRed | tmpBlue);

  return tmpColor;
}

/**
  * @brief  Draws a character on LCD.
  * @param  Xpos: the Line where to display the character shape.
  *         This parameter can be one of the following values:
  *     @arg  - Linex: where x can be 0..9
  * @param  Ypos: start column address.
  * @param  c: pointer to the character data.
  * @retval None
  */
void GL_LCD_DrawChar(uint8_t Xpos, uint16_t Ypos, const uint16_t *c) /* 16bit char */
{
  uint32_t line_index = 0, pixel_index = 0;
  uint8_t Xaddress = 0;
  uint16_t Yaddress = 0;
  __IO uint16_t tmp_color = 0;

  Xaddress = Xpos;
  Yaddress = Ypos;

  for (line_index = 0; line_index < GL_FontHeight; line_index++)
  {
    /* SmallFonts have bytes in reverse order */
    if (( GL_Font == GL_FONT_BIG   && (((const uint16_t*)c)[line_index] & (1 << 0)) == 0x00) ||
        ( GL_Font == GL_FONT_SMALL && (((const uint16_t*)c)[line_index] & (0x80 >> 0)) == 0x00))
    {
      tmp_color = GL_BackColor;
    }
    else
    {
      tmp_color = GL_TextColor;
    }

    LCD_PutPixel(Xaddress, Yaddress--, tmp_color, FirstPixel);

    for (pixel_index = 1; pixel_index < GL_FontWidth - 1; pixel_index++)
    {
      /* SmallFonts have bytes in reverse order */
      if (( GL_Font == GL_FONT_BIG   && (((const uint16_t*)c)[line_index] & (1 << pixel_index)) == 0x00) ||
          ( GL_Font == GL_FONT_SMALL && (((const uint16_t*)c)[line_index] & (0x80 >> pixel_index)) == 0x00))
      {
        tmp_color = GL_BackColor;
      }
      else
      {
        tmp_color = GL_TextColor;
      }

      LCD_PutPixel(Xaddress, Yaddress--, tmp_color, MiddlePixel);
    }
    pixel_index++;
    /* SmallFonts have bytes in reverse order */
    if (( GL_Font == GL_FONT_BIG   && (((const uint16_t*)c)[line_index] & (1 << pixel_index)) == 0x00) ||
        ( GL_Font == GL_FONT_SMALL && (((const uint16_t*)c)[line_index] & (0x80 >> pixel_index)) == 0x00))
    {
      tmp_color = GL_BackColor;
    }
    else
    {
      tmp_color = GL_TextColor;
    }

    LCD_PutPixel(Xaddress, Yaddress--, tmp_color, LastPixel);

    Xaddress++;
    Yaddress = Ypos;
  }
}

/**
  * @brief  Copy 4 bytes from bitmap array to 32Bit buffer
  * @param  ptrBitmap - Bitmap pointer
  * @param  ptr32BitBuffer - 32Bit buffer to fill
  * @retval None
  */
void BmpBuffer32BitRead(uint32_t* ptr32BitBuffer, uint8_t* ptrBitmap)
{
  *ptr32BitBuffer = 0;
  *ptr32BitBuffer = (*ptrBitmap);
  *ptr32BitBuffer += (*(ptrBitmap + 1)) << 8;
  *ptr32BitBuffer += (*(ptrBitmap + 2)) << 16;
  *ptr32BitBuffer += (*(ptrBitmap + 3)) << 24;
}

/**
  * @brief  Sets or reset LCD control lines.
  * @param  GPIOx: where x can be B or D to select the GPIO peripheral.
  * @param  CtrlPins: the Control line.
  *         This parameter can be one of the following values:
  *     @arg  - LCD_CtrlPin_NCS: Chip Select pin 
  *     @arg  - CtrlPin_NWR: Read/Write Selection pin
  *     @arg  - CtrlPin_RS: Register/RAM Selection pin
  * @param  BitVal: specifies the value to be written to the selected bit.
  *         This parameter can be one of the following values:
  *     @arg  - GL_LOW: to clear the port pin
  *     @arg  - GL_HIGH: to set the port pin
  * @retval None
  */
void GL_LCD_CtrlLinesWrite(GPIO_TypeDef* GPIOx, uint16_t CtrlPins, GL_SignalActionType BitVal)
{
#if defined(USE_STM3210C_EVAL) || defined (USE_STM3210B_EVAL) ||\
  defined (USE_STM32100B_EVAL) || defined(USE_STM32L152_EVAL)
  /* Set or Reset the control line */
  LCD_CtrlLinesWrite(GPIOx, CtrlPins, (BitAction)BitVal);
#endif
}

/**
  * @brief  Reads the LCD RAM.
  * @param  None
  * @retval uint16_t - LCD RAM Value.
  */
uint16_t GL_LCD_ReadRAM(void)
{
  __IO uint16_t tmp = 0;

#if defined(GL_LCD_BASE)
  /* Write 16-bit Index (then Read Reg) */
  GL_LCD->LCD_REG = R34; /* Select GRAM Reg */
  /* Read 16-bit Reg */
  tmp = GL_LCD->LCD_RAM;
  tmp = GL_LCD->LCD_RAM; /* This line must be removed to work with some LCD controller */
#else
  if( pLcdHwParam.LCD_Connection_Mode == GL_SPI)
  {
    tmp = LCD_ReadReg(R34);
  }
#endif

  return tmp;
}

#if defined (USE_STM3210E_EVAL) || defined (USE_STM32100E_EVAL) || defined (USE_STM322xG_EVAL)
/**
  * @brief  Writes 1 word to the LCD RAM.
  * @param  RGB_Code: the pixel color in RGB mode (5-6-5).
  * @retval None
  */
void LCD_WriteRAMWord(uint16_t RGB_Code)
{
  LCD_WriteRAM_Prepare();
  LCD_WriteRAM(RGB_Code);
}
#endif /* USE_STM3210E_EVAL */

/**
  * @brief  LCD_Change_Direction
  * @param  RCC_APBPeriph: specifies the APB peripheral to gates its clock.
  * @param  Direction: The Drawing Direction
  *         This parameter can be one of the following values:
  *     @arg  _0_degree
  *     @arg  _90_degree
  *     @arg  _180_degree
  *     @arg  _270_degree
  * @retval None
  */
void LCD_Change_Direction(LCD_Direction_TypeDef Direction)
{
  LCD_Direction = Direction;

  if (LCD_Direction == _0_degree)
  {
    /* Set GRAM write direction and BGR = 1 */
    /* I/D=01 (Horizontal : increment, Vertical : decrement) */
    /* AM=1 (address is updated in vertical writing direction) */
    LCD_WriteReg(R3, 0x1018);
  }
  else if (LCD_Direction == _90_degree)
  {
    /* Set GRAM write direction and BGR = 1 */
    /* I/D=11 (Horizontal : increment, Vertical : increment) */
    /* AM=0 (address is updated in orizontal writing direction) */
    LCD_WriteReg(R3, 0x1030);
  }
  else if (LCD_Direction == _180_degree)
  {
    /* Set GRAM write direction and BGR = 1 */
    /* I/D=10 (Horizontal : decrement, Vertical : increment) */
    /* AM=1 (address is updated in vertical writing direction) */
    LCD_WriteReg(R3, 0x1028);
  }
  else if (LCD_Direction == _270_degree)
  {
    /* Set GRAM write direction and BGR = 1 */
    /* I/D=00 (Horizontal : decrement, Vertical : decrement) */
    /* AM=0 (address is updated in orizontal writing direction) */
    LCD_WriteReg(R3, 0x1000);
  }
}

/**
  * @brief  LCD_WriteChar
  * @param  Xpos: The X axis position
  * @param  Ypos: The Y axis position
  * @param  *c:   The Character pointer
  * @retval None
  */
void LCD_WriteChar(uint16_t Xpos, uint16_t Ypos, const uint16_t *c)
{
  uint32_t index = 0, counter = 0;
  uint16_t Xaddress = 0;
  uint16_t Yaddress = 0;

  if ((LCD_Direction == _0_degree) || (LCD_Direction == _180_degree))
  {
    Xaddress = Xpos;
    LCD_SetCursor(Xaddress, Ypos);
  }
  else if ((LCD_Direction == _90_degree) || (LCD_Direction == _270_degree))
  {
    Yaddress = Ypos;
    LCD_SetCursor(Xpos, Yaddress);
  }

  for (index = 0; index < GL_FontHeight; index++)
  {
    if ((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
    {
      LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
    }
    for (counter = 0; counter < GL_FontWidth; counter++)
    {
      /* SmallFonts have bytes in reverse order */

⌨️ 快捷键说明

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