PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

189 Zeilen
7.2KB

  1. /*
  2. ks0108.h - Arduino library support for ks0108 and compatable graphic LCDs
  3. Copyright (c)2008 Michael Margolis All right reserved
  4. mailto:memargolis@hotmail.com?subject=KS0108_Library
  5. The high level functions of this library are based on version 1.1 of ks0108 graphics routines
  6. written and copyright by Fabian Maximilian Thiele. His sitelink is dead but
  7. you can obtain a copy of his original work here:
  8. http://www.scienceprog.com/wp-content/uploads/2007/07/glcd_ks0108.zip
  9. Code changes include conversion to an Arduino C++ library, rewriting the low level routines
  10. to read busy status flag and support a wider range of displays, adding more flexibility
  11. in port addressing and improvements in I/O speed. The interface has been made more Arduino friendly
  12. and some convenience functions added.
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. Version: 1.0 - May 8 2008 - initial release
  17. Version: 1.0a - Sep 1 2008 - simplified command pin defines
  18. Version: 1.0b - Sep 18 2008 - replaced <wiring.h> with boolean typedef for rel 0012
  19. Version: 1.1 - Nov 7 2008 - restructured low level code to adapt to panel speed
  20. - moved chip and panel configuration into seperate header files
  21. - added fixed width system font
  22. Version: 2 - May 26 2009 - second release
  23. - added support for Mega and Sanguino, improved panel speed tolerance, added bitmap support
  24. */
  25. #include <inttypes.h>
  26. //typedef uint8_t boolean; // conflicts with modern Arduino
  27. typedef uint8_t byte;
  28. #include <avr/pgmspace.h>
  29. #ifndef KS0108_H
  30. #define KS0108_H
  31. #define GLCD_VERSION 2 // software version of this library
  32. // Chip specific includes
  33. #if defined(__AVR_ATmega1280__)
  34. #include "ks0108_Mega.h" // include this for the Arduino Mega other ATmega1280 boards
  35. #elif defined (__AVR_ATmega644__) // TODO - check this define
  36. #include "ks0108_Sanguino.h" // include this for Sanguino or ATmega644 boards
  37. #elif defined(__AVR_ATmega32U4__)
  38. #include "ks0108_Teensy.h" // http://www.pjrc.com/teensy/teensyduino.html
  39. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  40. #include "ks0108_Teensy++.h" // http://www.pjrc.com/teensy/teensyduino.html
  41. #elif defined(TEENSYDUINO) && defined(__arm__)
  42. #error "Sorry, this library does not work on 32 bit Teensy. Use openGLCD."
  43. #else
  44. #include "ks0108_Arduino.h" // include this for the Arduino or other ATmega168 boards
  45. #endif
  46. #include "ks0108_Panel.h" // this contains LCD panel specific configuration
  47. // macros for pasting port defines
  48. #define GLUE(a, b) a##b
  49. #define PORT(x) GLUE(PORT, x)
  50. #define PIN(x) GLUE(PIN, x)
  51. #define DDR(x) GLUE(DDR, x)
  52. // paste together the port definitions if using nibbles
  53. #define LCD_DATA_IN_LOW PIN(LCD_DATA_LOW_NBL) // Data I/O Register, low nibble
  54. #define LCD_DATA_OUT_LOW PORT(LCD_DATA_LOW_NBL) // Data Output Register - low nibble
  55. #define LCD_DATA_DIR_LOW DDR(LCD_DATA_LOW_NBL) // Data Direction Register for Data Port, low nibble
  56. #define LCD_DATA_IN_HIGH PIN(LCD_DATA_HIGH_NBL) // Data Input Register high nibble
  57. #define LCD_DATA_OUT_HIGH PORT(LCD_DATA_HIGH_NBL) // Data Output Register - high nibble
  58. #define LCD_DATA_DIR_HIGH DDR(LCD_DATA_HIGH_NBL) // Data Direction Register for Data Port, high nibble
  59. #define lcdDataOut(_val_) LCD_DATA_OUT(_val_)
  60. #define lcdDataDir(_val_) LCD_DATA_DIR(_val_)
  61. // macros to handle data output
  62. #ifdef LCD_DATA_NIBBLES // data is split over two ports
  63. #define LCD_DATA_OUT(_val_) \
  64. LCD_DATA_OUT_LOW = (LCD_DATA_OUT_LOW & 0xF0)| (_val_ & 0x0F); LCD_DATA_OUT_HIGH = (LCD_DATA_OUT_HIGH & 0x0F)| (_val_ & 0xF0);
  65. #define LCD_DATA_DIR(_val_)\
  66. LCD_DATA_DIR_LOW = (LCD_DATA_DIR_LOW & 0xF0)| (_val_ & 0x0F); LCD_DATA_DIR_HIGH = (LCD_DATA_DIR_HIGH & 0x0F)| (_val_ & 0xF0);
  67. #else // all data on same port (low equals high)
  68. #define LCD_DATA_OUT(_val_) LCD_DATA_OUT_LOW = (_val_);
  69. #define LCD_DATA_DIR(_val_) LCD_DATA_DIR_LOW = (_val_);
  70. #endif
  71. // Commands
  72. #ifdef HD44102
  73. #define LCD_ON 0x39
  74. #define LCD_OFF 0x38
  75. #define LCD_DISP_START 0x3E // Display start page 0
  76. #else
  77. #define LCD_ON 0x3F
  78. #define LCD_OFF 0x3E
  79. #define LCD_DISP_START 0xC0
  80. #endif
  81. #define LCD_SET_ADD 0x40
  82. #define LCD_SET_PAGE 0xB8
  83. #define LCD_BUSY_FLAG 0x80
  84. // Colors
  85. #define BLACK 0xFF
  86. #define WHITE 0x00
  87. // useful user contants
  88. #define NON_INVERTED false
  89. #define INVERTED true
  90. // Font Indices
  91. #define FONT_LENGTH 0
  92. #define FONT_FIXED_WIDTH 2
  93. #define FONT_HEIGHT 3
  94. #define FONT_FIRST_CHAR 4
  95. #define FONT_CHAR_COUNT 5
  96. #define FONT_WIDTH_TABLE 6
  97. // Uncomment for slow drawing
  98. // #define DEBUG
  99. typedef struct {
  100. uint8_t x;
  101. uint8_t y;
  102. uint8_t page;
  103. } lcdCoord;
  104. typedef uint8_t (*FontCallback)(const uint8_t*);
  105. uint8_t ReadPgmData(const uint8_t* ptr); //Standard Read Callback
  106. #define DrawVertLine(x, y, length, color) FillRect(x, y, 0, length, color)
  107. #define DrawHoriLine(x, y, length, color) FillRect(x, y, length, 0, color)
  108. #define DrawCircle(xCenter, yCenter, radius, color) DrawRoundRect(xCenter-radius, yCenter-radius, 2*radius, 2*radius, radius, color)
  109. #define ClearScreenX() FillRect(0, 0, (DISPLAY_WIDTH-1), (DISPLAY_HEIGHT-1), WHITE)
  110. #define ClearSysTextLine(_line) FillRect(0, (line*8), (DISPLAY_WIDTH-1), ((line*8)+ 7), WHITE )
  111. class ks0108 // shell class for ks0108 glcd code
  112. {
  113. private:
  114. lcdCoord Coord;
  115. boolean Inverted;
  116. FontCallback FontRead;
  117. uint8_t FontColor;
  118. const uint8_t* Font;
  119. uint8_t ReadData(void);
  120. uint8_t DoReadData(uint8_t first);
  121. void WriteCommand(uint8_t cmd, uint8_t chip);
  122. void WriteData(uint8_t data); // experts can make this public but the functionality is not documented
  123. inline void Enable(void);
  124. inline void SelectChip(uint8_t chip);
  125. void WaitReady( uint8_t chip);
  126. public:
  127. ks0108();
  128. // Control functions
  129. void Init(boolean invert);
  130. void GotoXY(uint8_t x, uint8_t y);
  131. // Graphic Functions
  132. void ClearPage(uint8_t page, uint8_t color);
  133. void ClearScreen(uint8_t color = WHITE);
  134. void DrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t color);
  135. void DrawRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color);
  136. void DrawRoundRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t radius, uint8_t color);
  137. void FillRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color);
  138. void InvertRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height);
  139. void SetInverted(boolean invert);
  140. void SetDot(uint8_t x, uint8_t y, uint8_t color);
  141. void DrawBitmap(const uint8_t * bitmap, uint8_t x, uint8_t y, uint8_t color);
  142. // Font Functions
  143. void SelectFont(const uint8_t* font, uint8_t color=BLACK, FontCallback callback=ReadPgmData); // defualt arguments added, callback now last arg
  144. int PutChar(char c);
  145. void Puts(const char* str);
  146. void Puts_P(PGM_P str);
  147. void PrintNumber(long n);
  148. void CursorTo( uint8_t x, uint8_t y); // 0 based coordinates for fixed width fonts (i.e. systemFont5x7)
  149. uint8_t CharWidth(char c);
  150. uint16_t StringWidth(char* str);
  151. uint16_t StringWidth_P(PGM_P str);
  152. };
  153. extern ks0108 GLCD;
  154. #endif