display_driver.ino 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <SoftI2C.h>
  2. #include "display_char.h"
  3. // Address of the I2C OLED
  4. #define OLED_I2C_ADDRESS 0x3C
  5. // Commands for SSD1306
  6. #define SSD1306_DISPLAYOFF 0xAE
  7. #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
  8. #define SSD1306_SETMULTIPLEX 0xA8
  9. #define SSD1306_SETDISPLAYOFFSET 0xD3
  10. #define SSD1306_SETSTARTLINE 0x40
  11. #define SSD1306_CHARGEPUMP 0x8D
  12. #define SSD1306_MEMORYMODE 0x20
  13. #define SSD1306_SEGREMAP 0xA1
  14. #define SSD1306_COMSCANDEC 0xC8
  15. #define SSD1306_SETCOMPINS 0xDA
  16. #define SSD1306_SETCONTRAST 0x81
  17. #define SSD1306_SETPRECHARGE 0xD9
  18. #define SSD1306_SETVCOMDETECT 0xDB
  19. #define SSD1306_DISPLAYALLON_RESUME 0xA4
  20. #define SSD1306_NORMALDISPLAY 0xA6
  21. #define SSD1306_DISPLAYON 0xAF
  22. //Global Variables
  23. static uint16_t display_num[10][16] = display16X16_number;
  24. static uint16_t display_A[16] = display16X16_A;
  25. static uint16_t display_C[16] = display16X16_C;
  26. static uint16_t display_V[16] = display16X16_V;
  27. static uint16_t display_blank[16] = display16X16_blank;
  28. uint8_t voltage_dec_3;
  29. uint8_t voltage_dec_2;
  30. uint8_t voltage_dec_1;
  31. uint8_t voltage_dec_0;
  32. uint8_t current_dec_2;
  33. uint8_t current_dec_1;
  34. uint8_t current_dec_0;
  35. uint16_t* display_addr_list[16];
  36. void I2CWriteCommand(uint8_t command) {
  37. I2CStart();
  38. I2CSend(OLED_I2C_ADDRESS << 1); // I2C address + Write bit
  39. I2CSend(0x00); // Co = 0, D/C# = 0
  40. I2CSend(command);
  41. I2CStop();
  42. }
  43. void I2CWriteData(uint8_t data) {
  44. I2CStart();
  45. I2CSend(OLED_I2C_ADDRESS << 1); // I2C address + Write bit
  46. I2CSend(0x40); // Co = 0, D/C# = 1
  47. I2CSend(data);
  48. I2CStop();
  49. }
  50. //Setup the OLED SoftI2C Config, call to initializeOLED to start connection
  51. void setupOLED() {
  52. scl_pin = OLED_SCL; // Default: P3.0
  53. sda_pin = OLED_SDA; // Default: P3.1
  54. I2CInit();
  55. }
  56. //Start the OLED screen in desired mode
  57. void initializeOLED() {
  58. I2CWriteCommand(SSD1306_DISPLAYOFF);
  59. I2CWriteCommand(SSD1306_SETDISPLAYCLOCKDIV);
  60. I2CWriteCommand(0x80); // Suggested value
  61. I2CWriteCommand(SSD1306_SETMULTIPLEX);
  62. I2CWriteCommand(0x3F);
  63. I2CWriteCommand(SSD1306_SETDISPLAYOFFSET);
  64. I2CWriteCommand(0x00);
  65. I2CWriteCommand(SSD1306_SETSTARTLINE | 0x0);
  66. I2CWriteCommand(SSD1306_CHARGEPUMP);
  67. I2CWriteCommand(0x14);
  68. I2CWriteCommand(SSD1306_MEMORYMODE);
  69. I2CWriteCommand(0x00);
  70. I2CWriteCommand(SSD1306_SEGREMAP | 0x1);
  71. I2CWriteCommand(SSD1306_COMSCANDEC);
  72. I2CWriteCommand(SSD1306_SETCOMPINS);
  73. I2CWriteCommand(0x12);
  74. I2CWriteCommand(SSD1306_SETCONTRAST);
  75. I2CWriteCommand(0xCF);
  76. I2CWriteCommand(SSD1306_SETPRECHARGE);
  77. I2CWriteCommand(0xF1);
  78. I2CWriteCommand(SSD1306_SETVCOMDETECT);
  79. I2CWriteCommand(0x40);
  80. I2CWriteCommand(SSD1306_DISPLAYALLON_RESUME);
  81. I2CWriteCommand(SSD1306_NORMALDISPLAY);
  82. I2CWriteCommand(SSD1306_DISPLAYON);
  83. }
  84. //Clear the screen with setting all bits to 0
  85. void clearScreen() {
  86. for (uint8_t i = 0; i < 8; i++) {
  87. I2CWriteCommand(0xB0 + i); // Set page address
  88. I2CWriteCommand(0x00); // Set lower column address
  89. I2CWriteCommand(0x10); // Set higher column address
  90. for (uint8_t j = 0; j < 128; j++) {
  91. I2CWriteData(0x00);
  92. }
  93. }
  94. }
  95. //Clear the screen with setting all bits to 1
  96. void lightScreen() {
  97. for (uint8_t i = 0; i < 8; i++) {
  98. I2CWriteCommand(0xB0 + i); // Set page address
  99. I2CWriteCommand(0x00); // Set lower column address
  100. I2CWriteCommand(0x10); // Set higher column address
  101. for (uint8_t j = 0; j < 128; j++) {
  102. I2CWriteData(0xFF);
  103. }
  104. }
  105. }
  106. //Print the current power information to the screen
  107. void PS_Screen(uint16_t voltage_num, uint16_t current_num, bool CC_mode) {
  108. voltage_dec_3 = (voltage_num / 1000) % 10;
  109. voltage_dec_2 = (voltage_num / 100) % 10;
  110. voltage_dec_1 = (voltage_num / 10) % 10;
  111. voltage_dec_0 = voltage_num % 10;
  112. current_dec_2 = (current_num / 100) % 10;
  113. current_dec_1 = (current_num / 10) % 10;
  114. current_dec_0 = current_num % 10;
  115. display_addr_list[0] = (!CC_mode) ? display_C : display_blank;
  116. display_addr_list[1] = (!CC_mode) ? display_V : display_blank;
  117. display_addr_list[2] = display_blank;
  118. display_addr_list[3] = display_num[voltage_dec_3];
  119. display_addr_list[4] = display_num[voltage_dec_2];
  120. display_addr_list[5] = display_num[voltage_dec_1];
  121. display_addr_list[6] = display_num[voltage_dec_0];
  122. display_addr_list[7] = display_V;
  123. display_addr_list[8] = (!CC_mode) ? display_blank : display_C;
  124. display_addr_list[9] = (!CC_mode) ? display_blank : display_C;
  125. display_addr_list[10] = display_blank;
  126. display_addr_list[11] = display_blank;
  127. display_addr_list[12] = display_num[current_dec_2];
  128. display_addr_list[13] = display_num[current_dec_1];
  129. display_addr_list[14] = display_num[current_dec_0];
  130. display_addr_list[15] = display_A;
  131. for (uint8_t i = 0; i < 8; i++) {
  132. I2CWriteCommand(0xB0 + i); // Set page address
  133. I2CWriteCommand(0x00); // Set lower column address
  134. I2CWriteCommand(0x10); // Set higher column address
  135. for (uint8_t j = 0; j < 128; j++) {
  136. if (i % 4 == 3 && (j == 79 || j == 80)) {
  137. I2CWriteData(0xA0);
  138. } else {
  139. uint16_t push_temp = 0;
  140. uint8_t sel_col = 15 - j % 16;
  141. for (uint8_t k = 0; k < 4; k++) {
  142. uint8_t bit = ((display_addr_list[(j >> 4) + (i / 4 * 8)])[((i % 4) << 2) + k] >> (sel_col)) & 0x0001;
  143. push_temp |= (bit << (2 * k + 1));
  144. }
  145. I2CWriteData(push_temp);
  146. }
  147. }
  148. }
  149. }