ssd13x6.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * Copyright (c) 2017-2018 Tara Keeling
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <esp_heap_caps.h>
  14. #include "ssd13x6.h"
  15. #define SHADOW_BUFFER
  16. // used by both but different
  17. static uint8_t SSDCmd_Set_Display_Start_Line;
  18. static uint8_t SSDCmd_Set_Display_Offset;
  19. static uint8_t SSDCmd_Set_Column_Address;
  20. static uint8_t SSDCmd_Set_Display_CLK;
  21. static uint8_t SSDCmd_Set_Page_Address;
  22. // misc boundaries
  23. static uint8_t SSD13x6_Max_Col;
  24. static const uint8_t SSD13x6_Max_Row = 7;
  25. static bool SSD13x6_Init( struct SSD13x6_Device* DeviceHandle, int Width, int Height );
  26. int SSD13x6_GetCaps( struct SSD13x6_Device* DeviceHandle ) {
  27. if (DeviceHandle->Model == SH1106) return 0;
  28. else return CAPS_COLUMN_RANGE | CAPS_PAGE_RANGE | CAPS_ADDRESS_VERTICAL;
  29. }
  30. bool SSD13x6_WriteCommand( struct SSD13x6_Device* DeviceHandle, SSDCmd SSDCommand ) {
  31. NullCheck( DeviceHandle->WriteCommand, return false );
  32. return ( DeviceHandle->WriteCommand ) ( DeviceHandle, SSDCommand );
  33. }
  34. bool SSD13x6_WriteData( struct SSD13x6_Device* DeviceHandle, uint8_t* Data, size_t DataLength ) {
  35. NullCheck( DeviceHandle->WriteData, return false );
  36. return ( DeviceHandle->WriteData ) ( DeviceHandle, Data, DataLength );
  37. }
  38. void SSD13x6_SetMuxRatio( struct SSD13x6_Device* DeviceHandle, uint8_t Ratio ) {
  39. SSD13x6_WriteCommand( DeviceHandle, 0xA8 );
  40. SSD13x6_WriteCommand( DeviceHandle, Ratio );
  41. }
  42. void SSD13x6_SetDisplayOffset( struct SSD13x6_Device* DeviceHandle, uint8_t Offset ) {
  43. SSD13x6_WriteCommand( DeviceHandle, SSDCmd_Set_Display_Offset );
  44. SSD13x6_WriteCommand( DeviceHandle, Offset );
  45. }
  46. void SSD13x6_SetDisplayStartLine( struct SSD13x6_Device* DeviceHandle, int Line ) {
  47. SSD13x6_WriteCommand( DeviceHandle, SSDCmd_Set_Display_Start_Line + ( uint32_t ) ( Line & 0x1F ) );
  48. }
  49. void SSD13x6_SetContrast( struct SSD13x6_Device* DeviceHandle, uint8_t Contrast ) {
  50. SSD13x6_WriteCommand( DeviceHandle, 0x81 );
  51. SSD13x6_WriteCommand( DeviceHandle, Contrast );
  52. }
  53. void SSD13x6_EnableDisplayRAM( struct SSD13x6_Device* DeviceHandle ) {
  54. SSD13x6_WriteCommand( DeviceHandle, 0xA4 );
  55. }
  56. void SSD13x6_DisableDisplayRAM( struct SSD13x6_Device* DeviceHandle ) {
  57. SSD13x6_WriteCommand( DeviceHandle, 0xA5 );
  58. }
  59. void SSD13x6_SetInverted( struct SSD13x6_Device* DeviceHandle, bool Inverted ) {
  60. SSD13x6_WriteCommand( DeviceHandle, Inverted ? 0xA7 : 0xA6 );
  61. }
  62. void SSD13x6_SetDisplayClocks( struct SSD13x6_Device* DeviceHandle, uint32_t DisplayClockDivider, uint32_t OSCFrequency ) {
  63. DisplayClockDivider&= 0x0F;
  64. OSCFrequency&= 0x0F;
  65. SSD13x6_WriteCommand( DeviceHandle, SSDCmd_Set_Display_CLK );
  66. SSD13x6_WriteCommand( DeviceHandle, ( ( OSCFrequency << 4 ) | DisplayClockDivider ) );
  67. }
  68. void SSD13x6_DisplayOn( struct SSD13x6_Device* DeviceHandle ) {
  69. SSD13x6_WriteCommand( DeviceHandle, 0xAF );
  70. }
  71. void SSD13x6_DisplayOff( struct SSD13x6_Device* DeviceHandle ) {
  72. SSD13x6_WriteCommand( DeviceHandle, 0xAE );
  73. }
  74. void SSD132x_ReMap( struct SSD13x6_Device* DeviceHandle ) {
  75. SSD13x6_WriteCommand( DeviceHandle, 0xA0 );
  76. SSD13x6_WriteCommand( DeviceHandle, DeviceHandle->ReMap );
  77. }
  78. void SSD13x6_SetDisplayAddressMode( struct SSD13x6_Device* DeviceHandle, SSD13x6_AddressMode AddressMode ) {
  79. switch (DeviceHandle->Model) {
  80. case SH1106:
  81. // does not exist on SH1106
  82. break;
  83. case SSD1306:
  84. SSD13x6_WriteCommand( DeviceHandle, 0x20 );
  85. SSD13x6_WriteCommand( DeviceHandle, AddressMode );
  86. break;
  87. case SSD1326:
  88. DeviceHandle->ReMap = (AddressMode == AddressMode_Horizontal) ? (DeviceHandle->ReMap & ~0x80) : (DeviceHandle->ReMap | 0x80);
  89. SSD132x_ReMap(DeviceHandle);
  90. break;
  91. }
  92. }
  93. void SSD13x6_Update( struct SSD13x6_Device* DeviceHandle ) {
  94. #ifdef SHADOW_BUFFER
  95. // not sure the compiler does not have to redo all calculation in for loops, so local it is
  96. int width = DeviceHandle->Width, rows = DeviceHandle->Height / 8;
  97. uint8_t *optr = DeviceHandle->Shadowbuffer, *iptr = DeviceHandle->Framebuffer;
  98. // by row, find first and last columns that have been updated
  99. for (int r = 0; r < rows; r++) {
  100. uint8_t first = 0, last;
  101. for (int c = 0; c < width; c++) {
  102. if (*iptr != *optr) {
  103. if (!first) first = c + 1;
  104. last = c ;
  105. }
  106. *optr++ = *iptr++;
  107. }
  108. // now update the display by "byte rows"
  109. if (first--) {
  110. SSD13x6_SetColumnAddress( DeviceHandle, first, last );
  111. SSD13x6_SetPageAddress( DeviceHandle, r, r);
  112. SSD13x6_WriteData( DeviceHandle, DeviceHandle->Shadowbuffer + r*width + first, last - first + 1);
  113. }
  114. }
  115. #else
  116. if (DeviceHandle->Model == SH1106) {
  117. // SH1106 requires a page-by-page update and has no end Page/Column
  118. for (int i = 0; i < DeviceHandle->Height / 8 ; i++) {
  119. SSD13x6_SetPageAddress( DeviceHandle, i, 0);
  120. SSD13x6_SetColumnAddress( DeviceHandle, 0, 0);
  121. SSD13x6_WriteData( DeviceHandle, DeviceHandle->Framebuffer + i*DeviceHandle->Width, DeviceHandle->Width );
  122. }
  123. } else {
  124. // others have an automatic counter and end Page/Column
  125. SSD13x6_SetColumnAddress( DeviceHandle, 0, DeviceHandle->Width - 1);
  126. SSD13x6_SetPageAddress( DeviceHandle, 0, DeviceHandle->Height / 8 - 1);
  127. SSD13x6_WriteData( DeviceHandle, DeviceHandle->Framebuffer, DeviceHandle->FramebufferSize );
  128. }
  129. #endif
  130. }
  131. void SSD13x6_WriteRawData( struct SSD13x6_Device* DeviceHandle, uint8_t* Data, size_t DataLength ) {
  132. NullCheck( Data, return );
  133. DataLength = DataLength > DeviceHandle->FramebufferSize ? DeviceHandle->FramebufferSize : DataLength;
  134. if ( DataLength > 0 ) SSD13x6_WriteData( DeviceHandle, Data, DataLength );
  135. }
  136. void SSD13x6_SetHFlip( struct SSD13x6_Device* DeviceHandle, bool On ) {
  137. switch (DeviceHandle->Model) {
  138. case SH1106:
  139. case SSD1306:
  140. SSD13x6_WriteCommand( DeviceHandle, On ? 0xA1 : 0xA0 );
  141. break;
  142. case SSD1326:
  143. DeviceHandle->ReMap = On ? (DeviceHandle->ReMap | 0x01) : (DeviceHandle->ReMap & ~0x01);
  144. SSD132x_ReMap(DeviceHandle);
  145. break;
  146. }
  147. }
  148. void SSD13x6_SetVFlip( struct SSD13x6_Device* DeviceHandle, bool On ) {
  149. switch (DeviceHandle->Model) {
  150. case SH1106:
  151. case SSD1306:
  152. SSD13x6_WriteCommand( DeviceHandle, On ? 0xC8 : 0xC0 );
  153. break;
  154. case SSD1326:
  155. DeviceHandle->ReMap = On ? (DeviceHandle->ReMap | 0x05) : (DeviceHandle->ReMap & ~0x05);
  156. SSD132x_ReMap( DeviceHandle );
  157. break;
  158. }
  159. }
  160. void SSD13x6_SetColumnAddress( struct SSD13x6_Device* DeviceHandle, uint8_t Start, uint8_t End ) {
  161. CheckBounds( Start > SSD13x6_Max_Col, return );
  162. CheckBounds( End > SSD13x6_Max_Col, return );
  163. // on SH1106, there is no "end column"
  164. if (DeviceHandle->Model == SH1106) {
  165. // well, unfortunately this driver is 132 colums but most displays are 128...
  166. if (DeviceHandle->Width != 132) Start += 2;
  167. SSD13x6_WriteCommand( DeviceHandle, 0x10 | (Start >> 4) );
  168. SSD13x6_WriteCommand( DeviceHandle, 0x00 | (Start & 0x0f) );
  169. } else {
  170. SSD13x6_WriteCommand( DeviceHandle, SSDCmd_Set_Column_Address );
  171. SSD13x6_WriteCommand( DeviceHandle, Start );
  172. SSD13x6_WriteCommand( DeviceHandle, End );
  173. }
  174. }
  175. void SSD13x6_SetPageAddress( struct SSD13x6_Device* DeviceHandle, uint8_t Start, uint8_t End ) {
  176. NullCheck( DeviceHandle, return );
  177. CheckBounds( Start > SSD13x6_Max_Row, return );
  178. CheckBounds( End > SSD13x6_Max_Row, return );
  179. // on SH1106, there is no "end page"
  180. if (DeviceHandle->Model == SH1106) {
  181. SSD13x6_WriteCommand( DeviceHandle, 0xB0 | Start );
  182. } else {
  183. // in case of SSD1326, this is sub-optimal as it can address by line, not by page
  184. if (DeviceHandle->Model != SSD1306) {
  185. Start *= 8;
  186. End = (End + 1) * 8 - 1;
  187. }
  188. SSD13x6_WriteCommand( DeviceHandle, SSDCmd_Set_Page_Address );
  189. SSD13x6_WriteCommand( DeviceHandle, Start );
  190. SSD13x6_WriteCommand( DeviceHandle, End );
  191. }
  192. }
  193. bool SSD13x6_HWReset( struct SSD13x6_Device* DeviceHandle ) {
  194. NullCheck( DeviceHandle, return 0 );
  195. if ( DeviceHandle->Reset != NULL ) {
  196. return ( DeviceHandle->Reset ) ( DeviceHandle );
  197. }
  198. /* This should always return true if there is no reset callback as
  199. * no error would have occurred during the non existant reset.
  200. */
  201. return true;
  202. }
  203. static bool SSD13x6_Init( struct SSD13x6_Device* DeviceHandle, int Width, int Height ) {
  204. DeviceHandle->Width = Width;
  205. DeviceHandle->Height = Height;
  206. #ifdef SHADOW_BUFFER
  207. DeviceHandle->Shadowbuffer = heap_caps_malloc( DeviceHandle->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  208. memset( DeviceHandle->Shadowbuffer, 0xFF, DeviceHandle->FramebufferSize );
  209. #endif
  210. SSD13x6_HWReset( DeviceHandle );
  211. SSD13x6_DisplayOff( DeviceHandle );
  212. if (DeviceHandle->Model == SSD1306 || DeviceHandle->Model == SH1106) {
  213. SSDCmd_Set_Display_Start_Line = 0x40;
  214. SSDCmd_Set_Display_Offset = 0xD3;
  215. SSDCmd_Set_Column_Address = 0x21,
  216. SSDCmd_Set_Display_CLK = 0xD5;
  217. SSDCmd_Set_Page_Address = 0x22;
  218. SSD13x6_Max_Col = 127;
  219. if (DeviceHandle->Model == SSD1306) {
  220. // charge pump regulator, do direct init
  221. SSD13x6_WriteCommand( DeviceHandle, 0x8D );
  222. SSD13x6_WriteCommand( DeviceHandle, 0x14 );
  223. // COM pins HW config (alternative:EN if 64, DIS if 32, remap:DIS) - some display might need something difference
  224. SSD13x6_WriteCommand( DeviceHandle, 0xDA );
  225. SSD13x6_WriteCommand( DeviceHandle, ((Height == 64 ? 1 : 0) << 4) | (0 < 5) );
  226. } else {
  227. // charge pump regulator, do direct init
  228. SSD13x6_WriteCommand( DeviceHandle, 0xAD );
  229. SSD13x6_WriteCommand( DeviceHandle, 0x8B );
  230. // COM pins HW config (alternative:EN) - some display might need something difference
  231. SSD13x6_WriteCommand( DeviceHandle, 0xDA );
  232. SSD13x6_WriteCommand( DeviceHandle, 1 << 4);
  233. }
  234. } else if (DeviceHandle->Model == SSD1326) {
  235. SSDCmd_Set_Display_Start_Line = 0xA1;
  236. SSDCmd_Set_Display_Offset = 0xA2;
  237. SSDCmd_Set_Column_Address = 0x15;
  238. SSDCmd_Set_Display_CLK = 0xB3;
  239. SSDCmd_Set_Page_Address = 0x75; // not really a page but a row
  240. SSD13x6_Max_Col = 255;
  241. // no gray scale
  242. DeviceHandle->ReMap |= 0x10;
  243. SSD132x_ReMap( DeviceHandle );
  244. }
  245. SSD13x6_SetMuxRatio( DeviceHandle, Height - 1 );
  246. SSD13x6_SetDisplayOffset( DeviceHandle, 0x00 );
  247. SSD13x6_SetDisplayStartLine( DeviceHandle, 0 );
  248. SSD13x6_SetContrast( DeviceHandle, 0x7F );
  249. SSD13x6_DisableDisplayRAM( DeviceHandle );
  250. SSD13x6_SetVFlip( DeviceHandle, false );
  251. SSD13x6_SetHFlip( DeviceHandle, false );
  252. SSD13x6_SetInverted( DeviceHandle, false );
  253. SSD13x6_SetDisplayClocks( DeviceHandle, 0, 8 );
  254. SSD13x6_SetDisplayAddressMode( DeviceHandle, AddressMode_Horizontal );
  255. SSD13x6_SetColumnAddress( DeviceHandle, 0, DeviceHandle->Width - 1 );
  256. SSD13x6_SetPageAddress( DeviceHandle, 0, ( DeviceHandle->Height / 8 ) - 1 );
  257. SSD13x6_EnableDisplayRAM( DeviceHandle );
  258. SSD13x6_DisplayOn( DeviceHandle );
  259. SSD13x6_Update( DeviceHandle );
  260. return true;
  261. }
  262. bool SSD13x6_Init_I2C( struct SSD13x6_Device* DeviceHandle, int Width, int Height, int I2CAddress, int ResetPin, WriteCommandProc WriteCommand, WriteDataProc WriteData, ResetProc Reset ) {
  263. NullCheck( DeviceHandle, return false );
  264. NullCheck( WriteCommand, return false );
  265. NullCheck( WriteData, return false );
  266. DeviceHandle->WriteCommand = WriteCommand;
  267. DeviceHandle->WriteData = WriteData;
  268. DeviceHandle->Reset = Reset;
  269. DeviceHandle->Address = I2CAddress;
  270. DeviceHandle->RSTPin = ResetPin;
  271. DeviceHandle->FramebufferSize = ( Width * Height ) / 8;
  272. DeviceHandle->Framebuffer = calloc( 1, DeviceHandle->FramebufferSize );
  273. NullCheck( DeviceHandle->Framebuffer, return false );
  274. return SSD13x6_Init( DeviceHandle, Width, Height );
  275. }
  276. bool SSD13x6_Init_SPI( struct SSD13x6_Device* DeviceHandle, int Width, int Height, int ResetPin, int CSPin, spi_device_handle_t SPIHandle, WriteCommandProc WriteCommand, WriteDataProc WriteData, ResetProc Reset ) {
  277. NullCheck( DeviceHandle, return false );
  278. NullCheck( WriteCommand, return false );
  279. NullCheck( WriteData, return false );
  280. DeviceHandle->WriteCommand = WriteCommand;
  281. DeviceHandle->WriteData = WriteData;
  282. DeviceHandle->Reset = Reset;
  283. DeviceHandle->SPIHandle = SPIHandle;
  284. DeviceHandle->RSTPin = ResetPin;
  285. DeviceHandle->CSPin = CSPin;
  286. DeviceHandle->FramebufferSize = ( Width * Height ) / 8;
  287. #ifdef SHADOW_BUFFER
  288. DeviceHandle->Framebuffer = calloc( 1, DeviceHandle->FramebufferSize );
  289. #else
  290. DeviceHandle->Framebuffer = heap_caps_calloc( 1, DeviceHandle->FramebufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  291. #endif
  292. NullCheck( DeviceHandle->Framebuffer, return false );
  293. return SSD13x6_Init( DeviceHandle, Width, Height );
  294. }