Subversion Repositories libCharLCD

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "libCharLCD/lcd.h"
  2.  
  3. namespace
  4. {
  5.         unsigned const LCD_BUSY_FLAG = 0x80;
  6.         unsigned const LCD_INIT_SEQ = 0x30; // initialise
  7.         unsigned const LCD_DISP_CLEAR = 0x01;
  8.         unsigned const LCD_DISP_OFF = 0x08;
  9.         unsigned const LCD_DISP_ON = 0x0C;
  10.         unsigned const LCD_CURSOR_ON = 0x0E;
  11.         unsigned const LCD_CURSOR_OFF = 0x0C;
  12.         unsigned const LCD_CURSOR_BLINK = 0x0F;
  13.         unsigned const LCD_RETURN_HOME = 0x02;
  14.         unsigned const LCD_ENTRY_MODE = 0x06;
  15.         unsigned const LCD_4BIT_MODE = 0x20;
  16.         unsigned const LCD_8BIT_MODE = 0x30;
  17.         unsigned const LCD_1_ROWS = 0x00;
  18.         unsigned const LCD_2_ROWS = 0x08;
  19.         unsigned const LCD_FONT_5x8 = 0x00;
  20.         unsigned const LCD_FONT_5x10 = 0x04;
  21.         unsigned const LCD_POSITION = 0x80;
  22.         unsigned const LCD_CG = 0x40;
  23.  
  24.         unsigned const LCD_SHIFT = 0x10;
  25.         unsigned const LCD_CURSOR = 0x00;
  26.         unsigned const LCD_DISPLAY = 0x08;
  27.         unsigned const LCD_LEFT = 0x00;
  28.         unsigned const LCD_RIGHT = 0x04;
  29.  
  30.         unsigned const LCD_ROW1_START = 0x00;
  31.         unsigned const LCD_ROW2_START = 0x40;
  32. } // namespace
  33.  
  34. // initialise the display
  35. void lcd_t::init()
  36. {
  37.         // initialise low level code now ChibiOS is running
  38.         m_lcd.init();
  39.  
  40.         m_lcd.writeCtrl4(LCD_INIT_SEQ);
  41.  
  42.         HAL_Delay(5); // > 4.1ms
  43.         m_lcd.writeCtrl4(LCD_INIT_SEQ);
  44.  
  45.         HAL_Delay(5);
  46.         m_lcd.writeCtrlWait(LCD_4BIT_MODE | LCD_2_ROWS | LCD_FONT_5x8);
  47.  
  48.         HAL_Delay(5);
  49.  
  50.         m_lcd.writeCtrlWait(LCD_4BIT_MODE | LCD_2_ROWS | LCD_FONT_5x8);
  51.  
  52.         m_lcd.writeCtrlWait(LCD_DISP_OFF);
  53.  
  54.         m_lcd.writeCtrlWait(LCD_DISP_CLEAR);
  55.  
  56.         m_lcd.writeCtrlWait(LCD_ENTRY_MODE);
  57. }
  58.  
  59. /// \brief clear the display
  60. void lcd_t::clear()
  61. {
  62.         m_lcd.writeCtrlWait(LCD_DISP_CLEAR);
  63. }
  64.  
  65. void lcd_t::enable(bool ena_)
  66. {
  67.         m_lcd.writeCtrlWait(ena_ ? LCD_DISP_ON : LCD_DISP_OFF);
  68. }
  69.  
  70. void lcd_t::gotoxy(uint8_t x_, uint8_t y_)
  71. {
  72.         if (y_ >= displayYsize())
  73.                 return;
  74.         if (x_ >= displayXsize())
  75.                 return;
  76.         uint8_t row = (y_ == 1) ? LCD_ROW2_START : LCD_ROW1_START;
  77.  
  78.         // x_ is already constrained
  79.         m_lcd.writeCtrlWait(LCD_POSITION | row | x_);
  80. }
  81.  
  82. void lcd_t::printString(char *str_, uint8_t count_)
  83. {
  84.         printString(const_cast<char const *>(str_),count_);
  85. }
  86.  
  87. void lcd_t::printString(char const *str_, uint8_t count_)
  88. {
  89.         // if passed a null terminated string and a count less than display X size  
  90.         if (count_ > displayXsize())
  91.         {
  92.                 count_ = displayXsize();
  93.                 while (*str_ && count_--)
  94.                         m_lcd.writeData(*str_++);
  95.                 return;
  96.         }
  97.         else
  98.         {
  99.                 while (count_--)
  100.                         m_lcd.writeData(*str_++);
  101.         }
  102. }
  103.  
  104. uint8_t lcd_t::format_num(char *buff, uint8_t digits, uint8_t dp_pos, int val)
  105. {
  106.         digits++;
  107.         uint8_t pos = digits;
  108.         uint8_t dp_loc = pos - dp_pos;
  109.         uint8_t sign = 0;
  110.         if (val < 0)
  111.         {
  112.                 sign = 1;
  113.                 val = -val;
  114.         }
  115.  
  116.         buff[pos] = 0;
  117.         while (pos)
  118.         {
  119.                 if (pos == dp_loc)
  120.                 {
  121.                         buff[--pos] = '.';
  122.                 }
  123.                 else
  124.                 {
  125.                         buff[--pos] = val % 10 + '0';
  126.                         val /= 10;
  127.                         if (val == 0 && pos < dp_loc)
  128.                                 break;
  129.                 }
  130.         }
  131.         if (sign)
  132.         {
  133.                 buff[--pos] = '-';
  134.         }
  135.         return digits;
  136. }
  137.  
  138. void lcd_t::font_digits(uint8_t digits, uint8_t dp_pos, int val)
  139. {
  140.         char buff[10] = "        ";
  141.         format_num(buff, digits, dp_pos, val);
  142.         printString(buff);
  143. }
  144.  
  145. void lcd_t::defineChar(uint8_t code, uint8_t* rows)
  146. {
  147.         defineChar(code, const_cast<uint8_t const *>(rows));
  148. }
  149.  
  150.  
  151. void lcd_t::defineChar(uint8_t code, uint8_t const * rows)
  152. {
  153.         uint8_t addr =  (code & 0x7)* 8;
  154.         m_lcd.writeCtrlWait(LCD_CG | (addr &0x3F)  );
  155.         for (int i = 0; i < 8; i++)
  156.                 m_lcd.writeData(rows[i] & 0x1f);
  157. }