u8g_dev_ssd1353_160x128.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. u8g_dev_ssd1353_160x128.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2015, hugodan3@googlemail.com
  5. Copyright (c) 2013, jamjardavies@gmail.com
  6. Copyright (c) 2013, olikraus@gmail.com
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright notice, this list
  11. of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright notice, this
  13. list of conditions and the following disclaimer in the documentation and/or other
  14. materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. History:
  29. Initial version 8 Mar 2015 hugodan3@googlemail.com. This version has
  30. been derived from the ssd1351 driver by jamjarda. It has
  31. been improved by in-lining time critical functions.
  32. */
  33. #include "u8g.h"
  34. #define WIDTH 160
  35. #define HEIGHT 128
  36. #define PAGE_HEIGHT 8
  37. #define USE_GREY_TABLE 0
  38. static const uint8_t u8g_dev_ssd1353_160x128_init_seq[] PROGMEM = {
  39. U8G_ESC_CS(0), /* disable chip */
  40. U8G_ESC_DLY(50),
  41. U8G_ESC_ADR(0), /* instruction mode */
  42. U8G_ESC_RST(1), /* do reset low pulse with (1*16)+2 milliseconds */
  43. U8G_ESC_DLY(50),
  44. U8G_ESC_CS(1), /* enable chip */
  45. 0xFD, /* Command unlock */
  46. U8G_ESC_ADR(1),
  47. 0x12,
  48. U8G_ESC_ADR(0),
  49. 0xAE, /* Set Display Off */
  50. U8G_ESC_ADR(0),
  51. 0xA8,
  52. U8G_ESC_ADR(1),
  53. 0x7F, /* Set Multiplex Ratio */
  54. U8G_ESC_ADR(0),
  55. 0xA0,
  56. U8G_ESC_ADR(1),
  57. 0xB4, /* Set remapping */
  58. U8G_ESC_ADR(0),
  59. 0xA1,
  60. U8G_ESC_ADR(1),
  61. 0x00, /* Set Display Start Line */
  62. U8G_ESC_ADR(0),
  63. 0xA2,
  64. U8G_ESC_ADR(1),
  65. 0x00, /* Set Display Offset */
  66. U8G_ESC_ADR(0),
  67. 0xB1,
  68. U8G_ESC_ADR(1),
  69. 0x32, /* Set Phase Length */
  70. U8G_ESC_ADR(0),
  71. 0xB4,
  72. U8G_ESC_ADR(1),
  73. 0x04, /* Set Second Precharge Period */
  74. U8G_ESC_ADR(0),
  75. 0xA4, /* Set Display Mode ON */
  76. U8G_ESC_ADR(0),
  77. 0xB3,
  78. U8G_ESC_ADR(1), /* frame rate */
  79. 0x40,
  80. U8G_ESC_ADR(0),
  81. 0xBB,
  82. U8G_ESC_ADR(1), /* pre-charge level */
  83. 0x08,
  84. U8G_ESC_ADR(0),
  85. 0xBE,
  86. U8G_ESC_ADR(1), /* vcomh */
  87. 0x3C,
  88. /* color adjustments */
  89. #if USE_GREY_TABLE != 1
  90. U8G_ESC_ADR(0), /* instruction mode */
  91. 0x81,
  92. U8G_ESC_ADR(1),
  93. 0xC8, /* Set Contrast Color 1*/
  94. U8G_ESC_ADR(0), /* instruction mode */
  95. 0x82,
  96. U8G_ESC_ADR(1),
  97. 0x80, /* Set Contrast Color 2*/
  98. U8G_ESC_ADR(0), /* instruction mode */
  99. 0x83,
  100. U8G_ESC_ADR(1),
  101. 0xF8, /* Set Contrast Color 3*/
  102. U8G_ESC_ADR(0), /* instruction mode */
  103. 0x87,
  104. U8G_ESC_ADR(1),
  105. 0x09, /* Set Master Contrast MAX */
  106. U8G_ESC_ADR(0), /* instruction mode */
  107. 0xB9, /* Set CMD Grayscale Linear */
  108. #else
  109. U8G_ESC_ADR(0), /* instruction mode */
  110. 0x81,
  111. U8G_ESC_ADR(1),
  112. 0xC8, /* Set Contrast Color 1*/
  113. U8G_ESC_ADR(0), /* instruction mode */
  114. 0x82,
  115. U8G_ESC_ADR(1),
  116. 0xA0, /* Set Contrast Color 2*/
  117. U8G_ESC_ADR(0), /* instruction mode */
  118. 0x83,
  119. U8G_ESC_ADR(1),
  120. 0xB0, /* Set Contrast Color 3*/
  121. U8G_ESC_ADR(0), /* instruction mode */
  122. 0x87,
  123. U8G_ESC_ADR(1),
  124. 0x0F, /* Set Master Contrast MAX */
  125. U8G_ESC_ADR(0), /* instruction mode */
  126. 0xB8, /* Set CMD Grayscale Lookup */
  127. U8G_ESC_ADR(1),
  128. 0x05, 0x06, 0x07, 0x08,
  129. 0x09, 0x0a, 0x0b, 0x0c,
  130. 0x0D, 0x0E, 0x0F, 0x10,
  131. 0x11, 0x12, 0x13, 0x14,
  132. 0x15, 0x16, 0x18, 0x1a,
  133. 0x1b, 0x1C, 0x1D, 0x1F,
  134. 0x21, 0x23, 0x25, 0x27,
  135. 0x2A, 0x2D, 0x30, 0x33,
  136. 0x36, 0x39, 0x3C, 0x3F,
  137. 0x42, 0x45, 0x48, 0x4C,
  138. 0x50, 0x54, 0x58, 0x5C,
  139. 0x60, 0x64, 0x68, 0x6C,
  140. 0x70, 0x74, 0x78, 0x7D,
  141. 0x82, 0x87, 0x8C, 0x91,
  142. 0x96, 0x9B, 0xA0, 0xA5,
  143. 0xAA, 0xAF, 0xB4,
  144. #endif
  145. U8G_ESC_ADR(0),
  146. 0xAF, /* Set Display On */
  147. U8G_ESC_CS(0), /* disable chip */
  148. U8G_ESC_ADR(1),
  149. U8G_ESC_END /* end of sequence */
  150. };
  151. static const uint8_t u8g_dev_ssd1353_160x128_column_seq[] PROGMEM = {
  152. U8G_ESC_CS(1),
  153. U8G_ESC_ADR(0), 0x15,
  154. U8G_ESC_ADR(1), 0x00, 0x9f,
  155. U8G_ESC_ADR(0), 0x75,
  156. U8G_ESC_ADR(1), 0x00, 0x7f,
  157. U8G_ESC_ADR(0), 0x5c,
  158. U8G_ESC_ADR(1),
  159. U8G_ESC_CS(0),
  160. U8G_ESC_END
  161. };
  162. static const uint8_t u8g_dev_ssd1353_160x128_sleep_on[] PROGMEM = {
  163. U8G_ESC_CS(1),
  164. U8G_ESC_ADR(0), 0xAE,
  165. U8G_ESC_CS(0),
  166. U8G_ESC_END
  167. };
  168. static const uint8_t u8g_dev_ssd1353_160x128_sleep_off[] PROGMEM = {
  169. U8G_ESC_CS(1),
  170. U8G_ESC_ADR(0), 0xAF,
  171. U8G_ESC_CS(0),
  172. U8G_ESC_END
  173. };
  174. #define RGB332_STREAM_BYTES 8
  175. static uint8_t u8g_ssd1353_stream_bytes[RGB332_STREAM_BYTES*3];
  176. uint8_t u8g_dev_ssd1353_160x128_332_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  177. {
  178. switch(msg)
  179. {
  180. case U8G_DEV_MSG_INIT:
  181. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_50NS);
  182. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_init_seq);
  183. break;
  184. case U8G_DEV_MSG_STOP:
  185. break;
  186. case U8G_DEV_MSG_PAGE_FIRST:
  187. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_column_seq);
  188. break;
  189. case U8G_DEV_MSG_PAGE_NEXT:
  190. {
  191. u8g_uint_t x;
  192. uint8_t page_height;
  193. uint8_t i;
  194. uint8_t cnt;
  195. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  196. uint8_t *ptr = pb->buf;
  197. u8g_SetChipSelect(u8g, dev, 1);
  198. page_height = pb->p.page_y1;
  199. page_height -= pb->p.page_y0;
  200. page_height++;
  201. for( i = 0; i < page_height; i++ )
  202. {
  203. for (x = 0; x < pb->width; x+=RGB332_STREAM_BYTES)
  204. {
  205. /* inline operation for better perf */
  206. uint8_t *dest = u8g_ssd1353_stream_bytes;
  207. for( cnt = 0; cnt < RGB332_STREAM_BYTES; cnt++ )
  208. {
  209. uint8_t val = *ptr++;
  210. *dest++ = ((val & 0xe0) >> 2);
  211. *dest++ = ((val & 0x1c) << 1);
  212. *dest++ = ((val & 0x03) << 4);
  213. }
  214. u8g_WriteSequence(u8g, dev, RGB332_STREAM_BYTES*3, u8g_ssd1353_stream_bytes);
  215. }
  216. }
  217. u8g_SetChipSelect(u8g, dev, 0);
  218. }
  219. break;
  220. case U8G_DEV_MSG_SLEEP_ON:
  221. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_sleep_on);
  222. break;
  223. case U8G_DEV_MSG_SLEEP_OFF:
  224. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_sleep_off);
  225. break;
  226. }
  227. return u8g_dev_pb8h8_base_fn(u8g, dev, msg, arg);
  228. }
  229. /*
  230. * not tested and not released, just taken from ssd1351
  231. */
  232. static uint8_t u8g_dev_ssd1353_160x128_r[256];
  233. static uint8_t u8g_dev_ssd1353_160x128_g[256];
  234. static uint8_t u8g_dev_ssd1353_160x128_b[256];
  235. uint8_t u8g_dev_ssd1353_160x128_idx_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  236. {
  237. // u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  238. switch(msg)
  239. {
  240. case U8G_DEV_MSG_INIT:
  241. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_50NS);
  242. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_init_seq);
  243. break;
  244. case U8G_DEV_MSG_STOP:
  245. break;
  246. case U8G_DEV_MSG_SET_COLOR_ENTRY:
  247. u8g_dev_ssd1353_160x128_r[ ((u8g_dev_arg_irgb_t *)arg)->idx ] = ((u8g_dev_arg_irgb_t *)arg)->r;
  248. u8g_dev_ssd1353_160x128_g[ ((u8g_dev_arg_irgb_t *)arg)->idx ] = ((u8g_dev_arg_irgb_t *)arg)->g;
  249. u8g_dev_ssd1353_160x128_b[ ((u8g_dev_arg_irgb_t *)arg)->idx ] = ((u8g_dev_arg_irgb_t *)arg)->b;
  250. break;
  251. case U8G_DEV_MSG_PAGE_FIRST:
  252. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_column_seq);
  253. break;
  254. case U8G_DEV_MSG_PAGE_NEXT:
  255. {
  256. int x;
  257. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  258. uint8_t *ptr = pb->buf;
  259. u8g_SetChipSelect(u8g, dev, 1);
  260. for (x = 0; x < pb->width; x++)
  261. {
  262. u8g_WriteByte(u8g, dev, u8g_dev_ssd1353_160x128_r[(*ptr)>>2]);
  263. u8g_WriteByte(u8g, dev, u8g_dev_ssd1353_160x128_g[(*ptr)>>2]);
  264. u8g_WriteByte(u8g, dev, u8g_dev_ssd1353_160x128_b[(*ptr)>>2]);
  265. ptr++;
  266. }
  267. u8g_SetChipSelect(u8g, dev, 0);
  268. }
  269. break;
  270. case U8G_DEV_MSG_GET_MODE:
  271. return U8G_MODE_INDEX;
  272. }
  273. return u8g_dev_pb8h8_base_fn(u8g, dev, msg, arg);
  274. }
  275. uint8_t u8g_dev_ssd1353_160x128_hicolor_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  276. {
  277. switch(msg)
  278. {
  279. case U8G_DEV_MSG_INIT:
  280. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_50NS);
  281. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_init_seq);
  282. break;
  283. case U8G_DEV_MSG_STOP:
  284. break;
  285. case U8G_DEV_MSG_PAGE_FIRST:
  286. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_column_seq);
  287. break;
  288. case U8G_DEV_MSG_PAGE_NEXT:
  289. {
  290. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  291. uint8_t i, j;
  292. uint8_t page_height;
  293. uint8_t *ptr = pb->buf;
  294. u8g_SetChipSelect(u8g, dev, 1);
  295. page_height = pb->p.page_y1;
  296. page_height -= pb->p.page_y0;
  297. page_height++;
  298. for( j = 0; j < page_height; j++ )
  299. {
  300. for (i = 0; i < pb->width; i+=RGB332_STREAM_BYTES)
  301. {
  302. register uint8_t cnt, low, high, r, g, b;
  303. uint8_t *dest = u8g_ssd1353_stream_bytes;
  304. for( cnt = 0; cnt < RGB332_STREAM_BYTES; cnt++ )
  305. {
  306. low = *ptr++;
  307. high = *ptr++;
  308. r = high & ~7;
  309. r >>= 2;
  310. b = low & 31;
  311. b <<= 1;
  312. g = high & 7;
  313. g <<= 3;
  314. g |= (low>>5)&7;
  315. *dest++ = r;
  316. *dest++ = g;
  317. *dest++ = b;
  318. }
  319. u8g_WriteSequence(u8g, dev, RGB332_STREAM_BYTES*3, u8g_ssd1353_stream_bytes);
  320. }
  321. }
  322. u8g_SetChipSelect(u8g, dev, 0);
  323. }
  324. break; /* continue to base fn */
  325. case U8G_DEV_MSG_SLEEP_ON:
  326. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_sleep_on);
  327. break;
  328. case U8G_DEV_MSG_SLEEP_OFF:
  329. u8g_WriteEscSeqP(u8g, dev, u8g_dev_ssd1353_160x128_sleep_off);
  330. break;
  331. }
  332. return u8g_dev_pbxh16_base_fn(u8g, dev, msg, arg);
  333. }
  334. uint8_t u8g_dev_ssd1353_160x128_byte_buf[WIDTH*PAGE_HEIGHT] U8G_NOCOMMON ;
  335. u8g_pb_t u8g_dev_ssd1353_160x128_byte_pb = { {PAGE_HEIGHT, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_ssd1353_160x128_byte_buf};
  336. u8g_dev_t u8g_dev_ssd1353_160x128_332_sw_spi = { u8g_dev_ssd1353_160x128_332_fn, &u8g_dev_ssd1353_160x128_byte_pb, U8G_COM_SW_SPI };
  337. u8g_dev_t u8g_dev_ssd1353_160x128_332_hw_spi = { u8g_dev_ssd1353_160x128_332_fn, &u8g_dev_ssd1353_160x128_byte_pb, U8G_COM_HW_SPI };
  338. //u8g_dev_t u8g_dev_ssd1353_160x128_idx_sw_spi = { u8g_dev_ssd1353_160x128_idx_fn, &u8g_dev_ssd1353_160x128_byte_pb, U8G_COM_SW_SPI };
  339. //u8g_dev_t u8g_dev_ssd1353_160x128_idx_hw_spi = { u8g_dev_ssd1353_160x128_idx_fn, &u8g_dev_ssd1353_160x128_byte_pb, U8G_COM_HW_SPI };
  340. /* only half of the height, because two bytes are needed for one pixel */
  341. u8g_pb_t u8g_dev_ssd1353_160x128_hicolor_byte_pb = { {PAGE_HEIGHT/2, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_ssd1353_160x128_byte_buf};
  342. u8g_dev_t u8g_dev_ssd1353_160x128_hicolor_sw_spi = { u8g_dev_ssd1353_160x128_hicolor_fn, &u8g_dev_ssd1353_160x128_hicolor_byte_pb, U8G_COM_SW_SPI };
  343. u8g_dev_t u8g_dev_ssd1353_160x128_hicolor_hw_spi = { u8g_dev_ssd1353_160x128_hicolor_fn, &u8g_dev_ssd1353_160x128_hicolor_byte_pb, U8G_COM_HW_SPI };
  344. /* the 4x buffer is removed since it does not fit the RAM space of very small MCUs */
  345. #if 0
  346. uint8_t u8g_dev_ssd1353_160x128_4x_byte_buf[WIDTH*PAGE_HEIGHT*4] U8G_NOCOMMON ;
  347. u8g_pb_t u8g_dev_ssd1353_160x128_4x_332_byte_pb = { {PAGE_HEIGHT*4, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_ssd1353_160x128_4x_byte_buf};
  348. u8g_dev_t u8g_dev_ssd1353_160x128_4x_332_sw_spi = { u8g_dev_ssd1353_160x128_332_fn, &u8g_dev_ssd1353_160x128_4x_332_byte_pb, U8G_COM_SW_SPI };
  349. u8g_dev_t u8g_dev_ssd1353_160x128_4x_332_hw_spi = { u8g_dev_ssd1353_160x128_332_fn, &u8g_dev_ssd1353_160x128_4x_332_byte_pb, U8G_COM_HW_SPI };
  350. u8g_pb_t u8g_dev_ssd1353_160x128_4x_hicolor_byte_pb = { {PAGE_HEIGHT/2*4, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_ssd1353_160x128_4x_byte_buf};
  351. u8g_dev_t u8g_dev_ssd1353_160x128_4x_hicolor_sw_spi = { u8g_dev_ssd1353_160x128_hicolor_fn, &u8g_dev_ssd1353_160x128_4x_hicolor_byte_pb, U8G_COM_SW_SPI };
  352. u8g_dev_t u8g_dev_ssd1353_160x128_4x_hicolor_hw_spi = { u8g_dev_ssd1353_160x128_hicolor_fn, &u8g_dev_ssd1353_160x128_4x_hicolor_byte_pb, U8G_COM_HW_SPI };
  353. #endif