ssd1327_a.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. //
  2. // SSD1327 - 128x128x16 grayscale OLED library
  3. // Copyright 2019 (C) BitBank Software, Inc.
  4. // Project started 6/10/2019
  5. // Written by Larry Bank (bitbank@pobox.com)
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. #ifdef _LINUX_
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <fcntl.h>
  27. #include <sys/ioctl.h>
  28. #include <linux/i2c-dev.h>
  29. #include <math.h>
  30. #define PROGMEM
  31. #define memcpy_P memcpy
  32. static int file_i2c = 0;
  33. #define USE_BACKBUFFER
  34. #else // Arduino
  35. #include <Arduino.h>
  36. #ifdef __AVR__
  37. #include <avr/pgmspace.h>
  38. #else
  39. // On systems with enough RAM (not AVR), we allocate a static 8K back buffer
  40. #define USE_BACKBUFFER
  41. #endif
  42. #include <BitBang_I2C.h>
  43. #ifndef __AVR_ATtiny85__
  44. #include <Wire.h>
  45. #include <SPI.h>
  46. #endif
  47. #endif // _LINUX_
  48. #include <ssd1327.h>
  49. #ifdef USE_BACKBUFFER
  50. static uint8_t ucBackbuffer[8192];
  51. #endif
  52. void ssd1327Power(unsigned char bOn);
  53. // small (8x8) font
  54. const unsigned char ucFont[]PROGMEM = {
  55. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x5f,0x5f,0x06,0x00,0x00,
  56. 0x00,0x07,0x07,0x00,0x07,0x07,0x00,0x00,0x14,0x7f,0x7f,0x14,0x7f,0x7f,0x14,0x00,
  57. 0x24,0x2e,0x2a,0x6b,0x6b,0x3a,0x12,0x00,0x46,0x66,0x30,0x18,0x0c,0x66,0x62,0x00,
  58. 0x30,0x7a,0x4f,0x5d,0x37,0x7a,0x48,0x00,0x00,0x04,0x07,0x03,0x00,0x00,0x00,0x00,
  59. 0x00,0x1c,0x3e,0x63,0x41,0x00,0x00,0x00,0x00,0x41,0x63,0x3e,0x1c,0x00,0x00,0x00,
  60. 0x08,0x2a,0x3e,0x1c,0x1c,0x3e,0x2a,0x08,0x00,0x08,0x08,0x3e,0x3e,0x08,0x08,0x00,
  61. 0x00,0x00,0x80,0xe0,0x60,0x00,0x00,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00,
  62. 0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x60,0x30,0x18,0x0c,0x06,0x03,0x01,0x00,
  63. 0x3e,0x7f,0x59,0x4d,0x47,0x7f,0x3e,0x00,0x40,0x42,0x7f,0x7f,0x40,0x40,0x00,0x00,
  64. 0x62,0x73,0x59,0x49,0x6f,0x66,0x00,0x00,0x22,0x63,0x49,0x49,0x7f,0x36,0x00,0x00,
  65. 0x18,0x1c,0x16,0x53,0x7f,0x7f,0x50,0x00,0x27,0x67,0x45,0x45,0x7d,0x39,0x00,0x00,
  66. 0x3c,0x7e,0x4b,0x49,0x79,0x30,0x00,0x00,0x03,0x03,0x71,0x79,0x0f,0x07,0x00,0x00,
  67. 0x36,0x7f,0x49,0x49,0x7f,0x36,0x00,0x00,0x06,0x4f,0x49,0x69,0x3f,0x1e,0x00,0x00,
  68. 0x00,0x00,0x00,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x80,0xe6,0x66,0x00,0x00,0x00,
  69. 0x08,0x1c,0x36,0x63,0x41,0x00,0x00,0x00,0x00,0x14,0x14,0x14,0x14,0x14,0x14,0x00,
  70. 0x00,0x41,0x63,0x36,0x1c,0x08,0x00,0x00,0x00,0x02,0x03,0x59,0x5d,0x07,0x02,0x00,
  71. 0x3e,0x7f,0x41,0x5d,0x5d,0x5f,0x0e,0x00,0x7c,0x7e,0x13,0x13,0x7e,0x7c,0x00,0x00,
  72. 0x41,0x7f,0x7f,0x49,0x49,0x7f,0x36,0x00,0x1c,0x3e,0x63,0x41,0x41,0x63,0x22,0x00,
  73. 0x41,0x7f,0x7f,0x41,0x63,0x3e,0x1c,0x00,0x41,0x7f,0x7f,0x49,0x5d,0x41,0x63,0x00,
  74. 0x41,0x7f,0x7f,0x49,0x1d,0x01,0x03,0x00,0x1c,0x3e,0x63,0x41,0x51,0x33,0x72,0x00,
  75. 0x7f,0x7f,0x08,0x08,0x7f,0x7f,0x00,0x00,0x00,0x41,0x7f,0x7f,0x41,0x00,0x00,0x00,
  76. 0x30,0x70,0x40,0x41,0x7f,0x3f,0x01,0x00,0x41,0x7f,0x7f,0x08,0x1c,0x77,0x63,0x00,
  77. 0x41,0x7f,0x7f,0x41,0x40,0x60,0x70,0x00,0x7f,0x7f,0x0e,0x1c,0x0e,0x7f,0x7f,0x00,
  78. 0x7f,0x7f,0x06,0x0c,0x18,0x7f,0x7f,0x00,0x1c,0x3e,0x63,0x41,0x63,0x3e,0x1c,0x00,
  79. 0x41,0x7f,0x7f,0x49,0x09,0x0f,0x06,0x00,0x1e,0x3f,0x21,0x31,0x61,0x7f,0x5e,0x00,
  80. 0x41,0x7f,0x7f,0x09,0x19,0x7f,0x66,0x00,0x26,0x6f,0x4d,0x49,0x59,0x73,0x32,0x00,
  81. 0x03,0x41,0x7f,0x7f,0x41,0x03,0x00,0x00,0x7f,0x7f,0x40,0x40,0x7f,0x7f,0x00,0x00,
  82. 0x1f,0x3f,0x60,0x60,0x3f,0x1f,0x00,0x00,0x3f,0x7f,0x60,0x30,0x60,0x7f,0x3f,0x00,
  83. 0x63,0x77,0x1c,0x08,0x1c,0x77,0x63,0x00,0x07,0x4f,0x78,0x78,0x4f,0x07,0x00,0x00,
  84. 0x47,0x63,0x71,0x59,0x4d,0x67,0x73,0x00,0x00,0x7f,0x7f,0x41,0x41,0x00,0x00,0x00,
  85. 0x01,0x03,0x06,0x0c,0x18,0x30,0x60,0x00,0x00,0x41,0x41,0x7f,0x7f,0x00,0x00,0x00,
  86. 0x08,0x0c,0x06,0x03,0x06,0x0c,0x08,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
  87. 0x00,0x00,0x03,0x07,0x04,0x00,0x00,0x00,0x20,0x74,0x54,0x54,0x3c,0x78,0x40,0x00,
  88. 0x41,0x7f,0x3f,0x48,0x48,0x78,0x30,0x00,0x38,0x7c,0x44,0x44,0x6c,0x28,0x00,0x00,
  89. 0x30,0x78,0x48,0x49,0x3f,0x7f,0x40,0x00,0x38,0x7c,0x54,0x54,0x5c,0x18,0x00,0x00,
  90. 0x48,0x7e,0x7f,0x49,0x03,0x06,0x00,0x00,0x98,0xbc,0xa4,0xa4,0xf8,0x7c,0x04,0x00,
  91. 0x41,0x7f,0x7f,0x08,0x04,0x7c,0x78,0x00,0x00,0x44,0x7d,0x7d,0x40,0x00,0x00,0x00,
  92. 0x60,0xe0,0x80,0x84,0xfd,0x7d,0x00,0x00,0x41,0x7f,0x7f,0x10,0x38,0x6c,0x44,0x00,
  93. 0x00,0x41,0x7f,0x7f,0x40,0x00,0x00,0x00,0x7c,0x7c,0x18,0x78,0x1c,0x7c,0x78,0x00,
  94. 0x7c,0x78,0x04,0x04,0x7c,0x78,0x00,0x00,0x38,0x7c,0x44,0x44,0x7c,0x38,0x00,0x00,
  95. 0x84,0xfc,0xf8,0xa4,0x24,0x3c,0x18,0x00,0x18,0x3c,0x24,0xa4,0xf8,0xfc,0x84,0x00,
  96. 0x44,0x7c,0x78,0x4c,0x04,0x0c,0x18,0x00,0x48,0x5c,0x54,0x74,0x64,0x24,0x00,0x00,
  97. 0x04,0x04,0x3e,0x7f,0x44,0x24,0x00,0x00,0x3c,0x7c,0x40,0x40,0x3c,0x7c,0x40,0x00,
  98. 0x1c,0x3c,0x60,0x60,0x3c,0x1c,0x00,0x00,0x3c,0x7c,0x60,0x30,0x60,0x7c,0x3c,0x00,
  99. 0x44,0x6c,0x38,0x10,0x38,0x6c,0x44,0x00,0x9c,0xbc,0xa0,0xa0,0xfc,0x7c,0x00,0x00,
  100. 0x4c,0x64,0x74,0x5c,0x4c,0x64,0x00,0x00,0x08,0x08,0x3e,0x77,0x41,0x41,0x00,0x00,
  101. 0x00,0x00,0x00,0x77,0x77,0x00,0x00,0x00,0x41,0x41,0x77,0x3e,0x08,0x08,0x00,0x00,
  102. 0x02,0x03,0x01,0x03,0x02,0x03,0x01,0x00,0x70,0x78,0x4c,0x46,0x4c,0x78,0x70,0x00};
  103. // 5x7 font (in 6x8 cell)
  104. const unsigned char ucSmallFont[]PROGMEM = {
  105. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x5f,0x06,0x00,0x00,0x07,0x03,0x00,
  106. 0x07,0x03,0x00,0x24,0x7e,0x24,0x7e,0x24,0x00,0x24,0x2b,0x6a,0x12,0x00,0x00,0x63,
  107. 0x13,0x08,0x64,0x63,0x00,0x36,0x49,0x56,0x20,0x50,0x00,0x00,0x07,0x03,0x00,0x00,
  108. 0x00,0x00,0x3e,0x41,0x00,0x00,0x00,0x00,0x41,0x3e,0x00,0x00,0x00,0x08,0x3e,0x1c,
  109. 0x3e,0x08,0x00,0x08,0x08,0x3e,0x08,0x08,0x00,0x00,0xe0,0x60,0x00,0x00,0x00,0x08,
  110. 0x08,0x08,0x08,0x08,0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,
  111. 0x00,0x3e,0x51,0x49,0x45,0x3e,0x00,0x00,0x42,0x7f,0x40,0x00,0x00,0x62,0x51,0x49,
  112. 0x49,0x46,0x00,0x22,0x49,0x49,0x49,0x36,0x00,0x18,0x14,0x12,0x7f,0x10,0x00,0x2f,
  113. 0x49,0x49,0x49,0x31,0x00,0x3c,0x4a,0x49,0x49,0x30,0x00,0x01,0x71,0x09,0x05,0x03,
  114. 0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x06,0x49,0x49,0x29,0x1e,0x00,0x00,0x6c,0x6c,
  115. 0x00,0x00,0x00,0x00,0xec,0x6c,0x00,0x00,0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x24,
  116. 0x24,0x24,0x24,0x24,0x00,0x00,0x41,0x22,0x14,0x08,0x00,0x02,0x01,0x59,0x09,0x06,
  117. 0x00,0x3e,0x41,0x5d,0x55,0x1e,0x00,0x7e,0x11,0x11,0x11,0x7e,0x00,0x7f,0x49,0x49,
  118. 0x49,0x36,0x00,0x3e,0x41,0x41,0x41,0x22,0x00,0x7f,0x41,0x41,0x41,0x3e,0x00,0x7f,
  119. 0x49,0x49,0x49,0x41,0x00,0x7f,0x09,0x09,0x09,0x01,0x00,0x3e,0x41,0x49,0x49,0x7a,
  120. 0x00,0x7f,0x08,0x08,0x08,0x7f,0x00,0x00,0x41,0x7f,0x41,0x00,0x00,0x30,0x40,0x40,
  121. 0x40,0x3f,0x00,0x7f,0x08,0x14,0x22,0x41,0x00,0x7f,0x40,0x40,0x40,0x40,0x00,0x7f,
  122. 0x02,0x04,0x02,0x7f,0x00,0x7f,0x02,0x04,0x08,0x7f,0x00,0x3e,0x41,0x41,0x41,0x3e,
  123. 0x00,0x7f,0x09,0x09,0x09,0x06,0x00,0x3e,0x41,0x51,0x21,0x5e,0x00,0x7f,0x09,0x09,
  124. 0x19,0x66,0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x01,0x01,0x7f,0x01,0x01,0x00,0x3f,
  125. 0x40,0x40,0x40,0x3f,0x00,0x1f,0x20,0x40,0x20,0x1f,0x00,0x3f,0x40,0x3c,0x40,0x3f,
  126. 0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x07,0x08,0x70,0x08,0x07,0x00,0x71,0x49,0x45,
  127. 0x43,0x00,0x00,0x00,0x7f,0x41,0x41,0x00,0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00,
  128. 0x41,0x41,0x7f,0x00,0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x80,0x80,0x80,0x80,0x80,
  129. 0x00,0x00,0x03,0x07,0x00,0x00,0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x7f,0x44,0x44,
  130. 0x44,0x38,0x00,0x38,0x44,0x44,0x44,0x28,0x00,0x38,0x44,0x44,0x44,0x7f,0x00,0x38,
  131. 0x54,0x54,0x54,0x08,0x00,0x08,0x7e,0x09,0x09,0x00,0x00,0x18,0xa4,0xa4,0xa4,0x7c,
  132. 0x00,0x7f,0x04,0x04,0x78,0x00,0x00,0x00,0x00,0x7d,0x40,0x00,0x00,0x40,0x80,0x84,
  133. 0x7d,0x00,0x00,0x7f,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x7f,0x40,0x00,0x00,0x7c,
  134. 0x04,0x18,0x04,0x78,0x00,0x7c,0x04,0x04,0x78,0x00,0x00,0x38,0x44,0x44,0x44,0x38,
  135. 0x00,0xfc,0x44,0x44,0x44,0x38,0x00,0x38,0x44,0x44,0x44,0xfc,0x00,0x44,0x78,0x44,
  136. 0x04,0x08,0x00,0x08,0x54,0x54,0x54,0x20,0x00,0x04,0x3e,0x44,0x24,0x00,0x00,0x3c,
  137. 0x40,0x20,0x7c,0x00,0x00,0x1c,0x20,0x40,0x20,0x1c,0x00,0x3c,0x60,0x30,0x60,0x3c,
  138. 0x00,0x6c,0x10,0x10,0x6c,0x00,0x00,0x9c,0xa0,0x60,0x3c,0x00,0x00,0x64,0x54,0x54,
  139. 0x4c,0x00,0x00,0x08,0x3e,0x41,0x41,0x00,0x00,0x00,0x00,0x77,0x00,0x00,0x00,0x00,
  140. 0x41,0x41,0x3e,0x08,0x00,0x02,0x01,0x02,0x01,0x00,0x00,0x3c,0x26,0x23,0x26,0x3c};
  141. // some globals
  142. static int iCSPin, iDCPin, iResetPin;
  143. static int iMaxX, iMaxY, iPitch;
  144. static int oled_type, oled_flip, oled_addr;
  145. static int iSDAPin, iSCLPin;
  146. static void ssd1327WriteCommand(unsigned char c);
  147. // wrapper/adapter functions to make the code work on Linux
  148. #ifdef _LINUX_
  149. static uint8_t pgm_read_byte(uint8_t *ptr)
  150. {
  151. return *ptr;
  152. }
  153. static int16_t pgm_read_word(uint8_t *ptr)
  154. {
  155. return ptr[0] + (ptr[1]<<8);
  156. }
  157. int I2CReadRegister(uint8_t addr, uint8_t reg, uint8_t *pBuf, int iLen)
  158. {
  159. int rc;
  160. rc = write(file_i2c, &reg, 1);
  161. rc = read(file_i2c, pBuf, iLen);
  162. return (rc > 0);
  163. }
  164. int I2CInit(int iSDAPin, int iSCLPin, int32_t iSpeed)
  165. {
  166. char filename[32];
  167. sprintf(filename, "/dev/i2c-%d", iSDAPin); // I2C bus number passed in SDA pin
  168. if ((file_i2c = open(filename, O_RDWR)) < 0)
  169. return 1;
  170. if (ioctl(file_i2c, I2C_SLAVE, iSCLPin) < 0) // set slave address
  171. {
  172. close(file_i2c);
  173. file_i2c = 0;
  174. return 1;
  175. }
  176. return 0;
  177. }
  178. static void I2CWrite(unsigned char ucAddr, unsigned char *pData, int iLen)
  179. {
  180. write(file_i2c, pData, iLen);
  181. }
  182. #endif // _LINUX_
  183. // use only the bitbang version on ATtiny85 to avoid linking wire library
  184. #if defined( __AVR_ATtiny85__ ) || defined(_LINUX_)
  185. static void oledWrite(unsigned char *pData, int iLen)
  186. {
  187. I2CWrite(oled_addr, pData, iLen);
  188. } /* oledWrite() */
  189. #else
  190. // Wrapper function to write I2C data on Arduino
  191. static void oledWrite(unsigned char *pData, int iLen)
  192. {
  193. if (iCSPin != -1) // we're writing to SPI, treat it differently
  194. {
  195. digitalWrite(iDCPin, (pData[0] == 0) ? LOW : HIGH); // data versus command
  196. digitalWrite(iCSPin, LOW);
  197. #ifdef HAL_ESP32_HAL_H_
  198. {
  199. uint8_t ucTemp[1024];
  200. SPI.transferBytes(&pData[1], ucTemp, iLen-1);
  201. }
  202. #else
  203. SPI.transfer(&pData[1], iLen-1);
  204. #endif
  205. digitalWrite(iCSPin, HIGH);
  206. }
  207. else // must be I2C
  208. {
  209. if (iSDAPin != -1 && iSCLPin != -1)
  210. {
  211. I2CWrite(oled_addr, pData, iLen);
  212. }
  213. else
  214. {
  215. Wire.beginTransmission(oled_addr);
  216. Wire.write(pData, iLen);
  217. Wire.endTransmission();
  218. }
  219. } // I2C
  220. } /* oledWrite() */
  221. #endif // !__AVR_ATtiny85__
  222. //
  223. // Turn off the display
  224. //
  225. void ssd1327Shutdown(void)
  226. {
  227. ssd1327Power(0); // power off the display
  228. #ifdef _LINUX_
  229. close(file_i2c);
  230. file_i2c = 0;
  231. #endif
  232. } /* ssd1327Shutdown() */
  233. //
  234. // ssd1322
  235. //
  236. static uint8_t ssd1322_init_table[] = {
  237. 2, 0xfd, 0x12, // unlock the controller
  238. 1, 0xa4, // set display off
  239. // 1, 0xb9, // default grayscale mode
  240. 2, 0xb3, 0x91, // set clock divider
  241. 2, 0xca, 0x3f, // set COMS multiplex ratio 1/64
  242. 2, 0xa2, 0x00, // set display offset
  243. 2, 0xa1, 0x00, // set display start line
  244. 3, 0xa0, 0x14, 0x11, // set display remap
  245. 2, 0xb5, 0x00, // disable GPIO
  246. 2, 0xab, 0x01, // select external VDD regulator (none)
  247. 3, 0xb4, 0xa0, 0xfd, // external VSL display enhancement
  248. 2, 0xb1, 0xe2, // set phase length
  249. 3, 0xd1, 0x82, 0x20, // display enahancement B
  250. 1, 0xa6, // set normal display mode
  251. 0
  252. };
  253. void ssd1322Init(int bFlip)
  254. {
  255. uint8_t *s = (uint8_t *)ssd1322_init_table;
  256. uint8_t ucTemp[8], iCount;
  257. iCount = 1;
  258. if (bFlip)
  259. ssd1322_init_table[19] = 0x06;
  260. else
  261. ssd1322_init_table[19] = 0x14; // segment remap
  262. ucTemp[0] = 0x40; // pretend it's I2C data
  263. while (iCount)
  264. {
  265. iCount = *s++;
  266. if (iCount != 0)
  267. {
  268. ssd1327WriteCommand(*s++);
  269. memcpy(&ucTemp[1], s, iCount-1);
  270. oledWrite(ucTemp, iCount);
  271. s += iCount-1;
  272. }
  273. }
  274. } /* ssd1322Init() */
  275. #if !defined( __AVR_ATtiny85__ ) && !defined(_LINUX_)
  276. //
  277. // Initialize the OLED controller for SPI mode
  278. //
  279. void ssd1327SPIInit(int iType, int iDC, int iCS, int iReset, int bFlip, int bInvert, int32_t iSpeed)
  280. {
  281. uint8_t uc[32];
  282. iDCPin = iDC;
  283. iCSPin = iCS;
  284. oled_type = iType;
  285. iResetPin = iReset;
  286. oled_flip = bFlip;
  287. pinMode(iDCPin, OUTPUT);
  288. pinMode(iCSPin, OUTPUT);
  289. digitalWrite(iCSPin, HIGH);
  290. // Reset it
  291. if (iResetPin != -1)
  292. {
  293. pinMode(iResetPin, OUTPUT);
  294. digitalWrite(iResetPin, HIGH);
  295. delay(50);
  296. digitalWrite(iResetPin, LOW);
  297. delay(50);
  298. digitalWrite(iResetPin, HIGH);
  299. delay(10);
  300. }
  301. // Initialize SPI
  302. SPI.begin();
  303. SPI.beginTransaction(SPISettings(iSpeed, MSBFIRST, SPI_MODE0));
  304. // SPI.setClockDivider(16);
  305. // SPI.setBitOrder(MSBFIRST);
  306. // SPI.setDataMode(SPI_MODE0);
  307. if (oled_type == OLED_256x64)
  308. {
  309. iMaxX = 256;
  310. iMaxY = 64;
  311. iPitch = 128;
  312. ssd1322Init(bFlip);
  313. }
  314. else
  315. {
  316. iPitch = 64;
  317. iMaxX = 128;
  318. iMaxY = 128;
  319. }
  320. ssd1327Power(1);
  321. if (bInvert)
  322. {
  323. uc[0] = 0; // command
  324. uc[1] = 0xa7; // invert command
  325. oledWrite(uc, 2);
  326. }
  327. if (bFlip && oled_type == OLED_128x128) // rotate display 180
  328. {
  329. uc[0] = 0; // command
  330. uc[1] = 0xa0;
  331. oledWrite(uc, 2);
  332. uc[0] = 0;
  333. uc[1] = 0xc0;
  334. oledWrite(uc, 2);
  335. }
  336. } /* ssd1327SPIInit() */
  337. #endif
  338. //
  339. // Initializes the OLED controller
  340. //
  341. void ssd1327Init(int iType, int iAddr, int bFlip, int bInvert, int sda, int scl, int32_t iSpeed)
  342. {
  343. unsigned char uc[4];
  344. oled_addr = iAddr;
  345. oled_flip = bFlip;
  346. oled_type = iType;
  347. iSDAPin = sda;
  348. iSCLPin = scl;
  349. // Disable SPI mode code
  350. iCSPin = iDCPin = iResetPin = -1;
  351. #ifdef _LINUX_
  352. I2CInit(sda, iAddr, iSpeed); // bus number and address for Linux
  353. #else
  354. if (sda != -1 && scl != -1)
  355. {
  356. I2CInit(sda, scl, iSpeed);
  357. }
  358. #ifndef __AVR_ATtiny85__
  359. else
  360. {
  361. Wire.begin(); // Initiate the Wire library
  362. Wire.setClock(iSpeed); // use high speed I2C mode (default is 100Khz)
  363. }
  364. #endif
  365. #endif // _LINUX_
  366. if (oled_type == OLED_256x64)
  367. {
  368. iMaxX = 256;
  369. iMaxY = 64;
  370. iPitch = 128;
  371. ssd1322Init(bFlip);
  372. }
  373. else
  374. {
  375. iPitch = 64;
  376. iMaxX = 128;
  377. iMaxY = 128;
  378. }
  379. ssd1327Power(0); // turn off the power
  380. if (oled_type == OLED_128x128)
  381. {
  382. uc[0] = 0x00; // command
  383. uc[1] = 0xa0; // GDDRAM mapping
  384. if (bFlip)
  385. uc[2] = 0x42;
  386. else
  387. uc[2] = 0x51; // default (top to bottom, left to right mapping)
  388. oledWrite(uc, 3);
  389. }
  390. ssd1327Power(1); // turn on the power
  391. uc[0] = 0; // command
  392. uc[1] = (bInvert) ? 0xa7:0xa4; // invert command / normal display
  393. oledWrite(uc, 2);
  394. } /* ssd1327Init() */
  395. //
  396. // Sends a command to turn on or off the OLED display
  397. //
  398. void ssd1327Power(unsigned char bOn)
  399. {
  400. if (bOn)
  401. ssd1327WriteCommand(0xaf); // turn on OLED
  402. else
  403. ssd1327WriteCommand(0xae); // turn off OLED
  404. } /* oledPower() */
  405. // Send a single byte command to the OLED controller
  406. static void ssd1327WriteCommand(unsigned char c)
  407. {
  408. unsigned char buf[2];
  409. buf[0] = 0x00; // command introducer
  410. buf[1] = c;
  411. oledWrite(buf, 2);
  412. } /* ssd1327WriteCommand() */
  413. static void ssd1327WriteCommand2(unsigned char c, unsigned char d)
  414. {
  415. unsigned char buf[3];
  416. buf[0] = 0x00;
  417. buf[1] = c;
  418. buf[2] = d;
  419. oledWrite(buf, 3);
  420. } /* ssd1327WriteCommand2() */
  421. //
  422. // Send commands to position the "cursor" (aka memory write address)
  423. // to the given row and column as well as the ending col/row
  424. //
  425. static void ssd1327SetPosition(int x, int y, int cx, int cy)
  426. {
  427. unsigned char buf[8];
  428. buf[0] = 0x00; // command introducer
  429. buf[1] = 0x15; // column start/end
  430. if (oled_type == OLED_256x64)
  431. {
  432. oledWrite(buf, 2);
  433. buf[0] = 0x40; // data
  434. buf[1] = 28 + (x/4); // strange SSD1322 mapping
  435. buf[2] = 28 + (((x+cx)/4)-1);
  436. oledWrite(buf, 3); // need to write this part as data
  437. buf[0] = 0x00; // command
  438. buf[1] = 0x75; // set row
  439. oledWrite(buf, 2);
  440. buf[0] = 0x40; // data
  441. buf[1] = y;
  442. buf[2] = y+cy-1;
  443. oledWrite(buf, 3);
  444. buf[0] = 0x00; // command
  445. buf[1] = 0x5c; // enable RAM write
  446. oledWrite(buf, 2);
  447. }
  448. else
  449. {
  450. buf[2] = x/2; // start address
  451. buf[3] = (uint8_t)(((x+cx)/2)-1); // end address
  452. buf[4] = 0x75; // row start/end
  453. buf[5] = y; // start row
  454. buf[6] = y+cy-1; // end row
  455. oledWrite(buf, 7);
  456. }
  457. } /* ssd1327SetPosition() */
  458. //
  459. // Write a block of pixel data to the OLED
  460. // Length can be anything from 1 to 8192 (whole display)
  461. //
  462. static void ssd1327WriteDataBlock(unsigned char *ucBuf, int iLen)
  463. {
  464. unsigned char ucTemp[129];
  465. ucTemp[0] = 0x40; // data command
  466. // Copying the data has the benefit in SPI mode of not letting
  467. // the original data get overwritten by the SPI.transfer() function
  468. memcpy(&ucTemp[1], ucBuf, iLen);
  469. oledWrite(ucTemp, iLen+1);
  470. } /* ssd1327WriteDataBlock() */
  471. #ifdef FUTURE
  472. //
  473. // Load a 128x64 1-bpp Windows bitmap
  474. // Pass the pointer to the beginning of the BMP file
  475. // First pass version assumes a full screen bitmap
  476. //
  477. int oledLoadBMP(unsigned char *pBMP)
  478. {
  479. int16_t i16;
  480. int iOffBits, q, y, j; // offset to bitmap data
  481. int iPitch;
  482. unsigned char x, z, b, *s;
  483. unsigned char dst_mask;
  484. unsigned char ucTemp[16]; // process 16 bytes at a time
  485. unsigned char bFlipped = false;
  486. i16 = pgm_read_word(pBMP);
  487. if (i16 != 0x4d42) // must start with 'BM'
  488. return -1; // not a BMP file
  489. i16 = pgm_read_word(pBMP + 18);
  490. if (i16 != 128) // must be 128 pixels wide
  491. return -1;
  492. i16 = pgm_read_word(pBMP + 22);
  493. if (i16 != 64 && i16 != -64) // must be 64 pixels tall
  494. return -1;
  495. if (i16 == 64) // BMP is flipped vertically (typical)
  496. bFlipped = true;
  497. i16 = pgm_read_word(pBMP + 28);
  498. if (i16 != 1) // must be 1 bit per pixel
  499. return -1;
  500. iOffBits = pgm_read_word(pBMP + 10);
  501. iPitch = 16;
  502. if (bFlipped)
  503. {
  504. iPitch = -16;
  505. iOffBits += (63 * 16); // start from bottom
  506. }
  507. // rotate the data and send it to the display
  508. for (y=0; y<8; y++) // 8 lines of 8 pixels
  509. {
  510. ssd1327SetPosition(0, y);
  511. for (j=0; j<8; j++) // do 8 sections of 16 columns
  512. {
  513. s = &pBMP[iOffBits + (j*2) + (y * iPitch*8)]; // source line
  514. memset(ucTemp, 0, 16); // start with all black
  515. for (x=0; x<16; x+=8) // do each block of 16x8 pixels
  516. {
  517. dst_mask = 1;
  518. for (q=0; q<8; q++) // gather 8 rows
  519. {
  520. b = pgm_read_byte(s + (q * iPitch));
  521. for (z=0; z<8; z++) // gather up the 8 bits of this column
  522. {
  523. if (b & 0x80)
  524. ucTemp[x+z] |= dst_mask;
  525. b <<= 1;
  526. } // for z
  527. dst_mask <<= 1;
  528. } // for q
  529. s++; // next source byte
  530. } // for x
  531. ssd1327WriteDataBlock(ucTemp, 16);
  532. } // for j
  533. } // for y
  534. return 0;
  535. } /* oledLoadBMP() */
  536. #endif // FUTURE
  537. //
  538. // Draw a string of normal (8x8), small (6x8) or large (16x32) characters
  539. // At the given col+row
  540. // For AVR systems (very little RAM), the text gets drawn directly on the display
  541. // For non-AVR systems, the text is written to the back buffer and transparent text
  542. // can be enabled by setting the background color to -1
  543. //
  544. void ssd1327WriteString(uint8_t x, uint8_t y, char *szMsg, uint8_t iSize, int ucFG, int ucBG)
  545. {
  546. int i, iFontOff;
  547. int tx, ty;
  548. unsigned char uc, ucMask;
  549. unsigned char c, *s, *d, ucTemp2[8];
  550. #ifndef USE_BACKBUFFER
  551. unsigned char ucTemp[40];
  552. #endif
  553. int iBG;
  554. #ifdef __AVR__
  555. if (ucBG == -1) ucBG = 0; // no transparent text allowed
  556. #endif
  557. if (iSize == FONT_NORMAL || iSize == FONT_SMALL) // 8x8 and 6x8 font
  558. {
  559. uint8_t cx = (iSize == FONT_NORMAL) ? 8:6;
  560. uint8_t *pFont = (iSize == FONT_NORMAL) ? (uint8_t*)ucFont:(uint8_t*)ucSmallFont;
  561. i = 0;
  562. if (y > iMaxY-7) return; // will write past the bottom
  563. while (x < iMaxX-7 && szMsg[i] != 0)
  564. {
  565. ssd1327SetPosition(x, y, cx, 8);
  566. c = (unsigned char)szMsg[i];
  567. iFontOff = (int)(c-32) * cx;
  568. // we can't directly use the pointer to FLASH memory, so copy to a local buffer
  569. memcpy_P(ucTemp2, &pFont[iFontOff], cx);
  570. s = ucTemp2;
  571. #ifdef USE_BACKBUFFER
  572. d = &ucBackbuffer[(y*iPitch) + (x/2)];
  573. #else
  574. d = &ucTemp[0];
  575. *d++ = 0x40; // data introducer
  576. #endif
  577. ucMask = 1;
  578. for (ty=0; ty<8; ty++)
  579. {
  580. if (ucBG == -1) // transparent text
  581. {
  582. for (tx=0; tx<cx; tx+=2)
  583. {
  584. if (s[tx] & ucMask) // foreground
  585. {
  586. d[0] &= 0xf;
  587. d[0] |= (ucFG << 4);
  588. }
  589. if (s[tx+1] & ucMask)
  590. {
  591. d[0] &= 0xf0;
  592. d[0] |= ucFG;
  593. }
  594. d++;
  595. } // for tx
  596. }
  597. else // regular text
  598. {
  599. for (tx=0; tx<cx; tx+=2)
  600. {
  601. if (s[tx] & ucMask)
  602. uc = ucFG << 4;
  603. else
  604. uc = ucBG << 4;
  605. if (s[tx+1] & ucMask)
  606. uc |= ucFG;
  607. else
  608. uc |= ucBG;
  609. *d++ = uc; // store pixel pair
  610. } // for tx
  611. }
  612. ucMask <<= 1;
  613. #ifdef USE_BACKBUFFER
  614. d -= cx/2;
  615. d += iPitch; // move to next line
  616. #endif
  617. } // for ty
  618. #ifndef USE_BACKBUFFER
  619. oledWrite(ucTemp, 1+(cx*4)); // write character pattern
  620. #endif
  621. x += cx;
  622. i++;
  623. }
  624. }
  625. else if (iSize == FONT_LARGE) // 16x16 font
  626. {
  627. if (y > iMaxY-15) return; // will write past the bottom
  628. i = 0;
  629. while (x < iMaxX-15 && szMsg[i] != 0)
  630. {
  631. // stretch the 'normal' font instead of using the big font
  632. int tx, ty;
  633. c = szMsg[i] - 32;
  634. unsigned char ucMask;
  635. s = (unsigned char *)&ucFont[(int)c*8];
  636. memcpy_P(ucTemp2, s, 8);
  637. iBG = ucBG;
  638. ucFG |= (ucFG << 4); // 2 pixels at a time
  639. ucBG |= (ucBG << 4);
  640. // Stretch the font to double width + double height
  641. ucMask = 1;
  642. #ifdef USE_BACKBUFFER
  643. d = &ucBackbuffer[(y*iPitch)+(x/2)];
  644. #else
  645. ssd1327SetPosition(x, y, 16, 16);
  646. ucTemp[0] = 0x40; // start of data (write one row at a time)
  647. #endif
  648. for (ty=0; ty<8; ty++)
  649. {
  650. for (tx=0; tx<8; tx++)
  651. {
  652. #ifdef USE_BACKBUFFER
  653. if (ucTemp2[tx] & ucMask) // pixel set
  654. d[tx] = d[tx+iPitch] = ucFG;
  655. else if (ucBG != -1)
  656. d[tx] = d[tx+iPitch] = ucBG;
  657. #else
  658. if (ucTemp2[tx] & ucMask) // pixel set
  659. c = ucFG;
  660. else
  661. c = ucBG;
  662. ucTemp[1+tx] = ucTemp[1+tx+8] = c; // double it vertically
  663. #endif
  664. }
  665. #ifdef USE_BACKBUFFER
  666. d += iPitch*2; // move to next line
  667. #else
  668. oledWrite(ucTemp, 17); // write 2 rows of the character
  669. #endif
  670. ucMask <<= 1;
  671. }
  672. x += 16;
  673. i++;
  674. }
  675. }
  676. } /* ssd1327WriteString() */
  677. #ifdef USE_BACKBUFFER
  678. //
  679. // For drawing ellipses, a circle is drawn and the x and y pixels are scaled by a 16-bit integer fraction
  680. // This function draws a single pixel and scales its position based on the x/y fraction of the ellipse
  681. //
  682. void DrawScaledPixel(int32_t iCX, int32_t iCY, int32_t x, int32_t y, int32_t iXFrac, int32_t iYFrac, uint8_t ucColor)
  683. {
  684. uint8_t *d;
  685. if (iXFrac != 0x10000) x = (x * iXFrac) >> 16;
  686. if (iYFrac != 0x10000) y = (y * iYFrac) >> 16;
  687. x += iCX; y += iCY;
  688. if (x < 0 || x >= iMaxX || y < 0 || y >= iMaxY)
  689. return; // off the screen
  690. d = &ucBackbuffer[(y * iPitch) + x/2];
  691. if (x & 1)
  692. {
  693. d[0] &= 0xf0;
  694. d[0] |= ucColor;
  695. }
  696. else
  697. {
  698. d[0] &= 0xf;
  699. d[0] |= (ucColor << 4);
  700. }
  701. } /* DrawScaledPixel() */
  702. void DrawScaledLine(int32_t iCX, int32_t iCY, int32_t x, int32_t y, int32_t iXFrac, int32_t iYFrac, uint8_t ucColor)
  703. {
  704. int32_t iLen, x2;
  705. uint8_t *d;
  706. if (iXFrac != 0x10000) x = (x * iXFrac) >> 16;
  707. if (iYFrac != 0x10000) y = (y * iYFrac) >> 16;
  708. iLen = x * 2;
  709. x = iCX - x; y += iCY;
  710. x2 = x + iLen;
  711. if (y < 0 || y >= iMaxY)
  712. return; // completely off the screen
  713. if (x < 0) x = 0;
  714. if (x2 >= iMaxX) x2 = iMaxX-1;
  715. iLen = x2 - x + 1; // new length
  716. d = &ucBackbuffer[(y * iPitch) + x/2];
  717. uint8_t c = ucColor | (ucColor << 4);
  718. if (x & 1) // starting on odd pixel
  719. {
  720. d[0] &= 0xf0;
  721. d[0] |= ucColor;
  722. d++;
  723. iLen--;
  724. }
  725. while (iLen >= 2) // middle part
  726. {
  727. *d++ = c;
  728. iLen -= 2;
  729. }
  730. if (iLen) // last odd pixel
  731. {
  732. d[0] &= 0xf;
  733. d[0] |= (ucColor << 4);
  734. }
  735. } /* DrawScaledLine() */
  736. //
  737. // Draw the 8 pixels around the Bresenham circle
  738. // (scaled to make an ellipse)
  739. //
  740. void BresenhamCircle(int32_t iCX, int32_t iCY, int32_t x, int32_t y, int32_t iXFrac, int32_t iYFrac, uint8_t iColor, int bFill)
  741. {
  742. if (bFill) // draw a filled ellipse
  743. {
  744. // for a filled ellipse, draw 4 lines instead of 8 pixels
  745. DrawScaledLine(iCX, iCY, x, y, iXFrac, iYFrac, iColor);
  746. DrawScaledLine(iCX, iCY, x, -y, iXFrac, iYFrac, iColor);
  747. DrawScaledLine(iCX, iCY, y, x, iXFrac, iYFrac, iColor);
  748. DrawScaledLine(iCX, iCY, y, -x, iXFrac, iYFrac, iColor);
  749. }
  750. else // draw 8 pixels around the edges
  751. {
  752. DrawScaledPixel(iCX, iCY, x, y, iXFrac, iYFrac, iColor);
  753. DrawScaledPixel(iCX, iCY, -x, y, iXFrac, iYFrac, iColor);
  754. DrawScaledPixel(iCX, iCY, x, -y, iXFrac, iYFrac, iColor);
  755. DrawScaledPixel(iCX, iCY, -x, -y, iXFrac, iYFrac, iColor);
  756. DrawScaledPixel(iCX, iCY, y, x, iXFrac, iYFrac, iColor);
  757. DrawScaledPixel(iCX, iCY, -y, x, iXFrac, iYFrac, iColor);
  758. DrawScaledPixel(iCX, iCY, y, -x, iXFrac, iYFrac, iColor);
  759. DrawScaledPixel(iCX, iCY, -y, -x, iXFrac, iYFrac, iColor);
  760. }
  761. } /* BresenhamCircle() */
  762. void ssd1327Ellipse(int32_t iCenterX, int32_t iCenterY, int32_t iRadiusX, int32_t iRadiusY, uint8_t ucColor, int bFilled)
  763. {
  764. int32_t iRadius, iXFrac, iYFrac;
  765. int32_t iDelta, x, y;
  766. if (iRadiusX > iRadiusY) // use X as the primary radius
  767. {
  768. iRadius = iRadiusX;
  769. iXFrac = 65536;
  770. iYFrac = (iRadiusY * 65536) / iRadiusX;
  771. }
  772. else
  773. {
  774. iRadius = iRadiusY;
  775. iXFrac = (iRadiusX * 65536) / iRadiusY;
  776. iYFrac = 65536;
  777. }
  778. // set up a buffer with the widest possible run of pixels to dump in 1 shot
  779. iDelta = 3 - (2 * iRadius);
  780. x = 0; y = iRadius;
  781. while (x <= y)
  782. {
  783. BresenhamCircle(iCenterX, iCenterY, x, y, iXFrac, iYFrac, ucColor, bFilled);
  784. x++;
  785. if (iDelta < 0)
  786. {
  787. iDelta += (4*x) + 6;
  788. }
  789. else
  790. {
  791. iDelta += 4 * (x-y) + 10;
  792. y--;
  793. }
  794. }
  795. } /* ssd1327Ellipse() */
  796. //
  797. // Draw a 1-bpp pattern into the backbuffer with the given color
  798. // 1 bits are drawn as color, 0 are transparent
  799. //
  800. void ssd1327DrawPattern(uint8_t *pPattern, int iSrcPitch, int iDestX, int iDestY, int iCX, int iCY, uint8_t ucColor)
  801. {
  802. int x, y;
  803. uint8_t *s, uc, ucPixel, ucMask;
  804. uint8_t *d;
  805. if (iDestX + iCX > iMaxX || iDestY + iCY > iMaxY || iDestX < 0 || iDestY < 0 || iCX < 0 || iCY < 0)
  806. return; // invalid parameter
  807. if (iDestX+iCX > iMaxX) // trim to fit on display
  808. iCX = (iMaxX - iDestX);
  809. if (iDestY+iCY > iMaxY)
  810. iCY = (iMaxY - iDestY);
  811. if (pPattern == NULL || iDestX < 0 || iDestY < 0 || iCX <=0 || iCY <= 0)
  812. return;
  813. for (y=0; y<iCY; y++)
  814. {
  815. d = &ucBackbuffer[((iDestY+y)*iPitch) + (iDestX/2)];
  816. s = &pPattern[y * iSrcPitch];
  817. ucMask = uc = 0;
  818. for (x=0; x<iCX; x+=2)
  819. {
  820. ucMask >>= 1;
  821. ucPixel = d[0];
  822. if (ucMask == 0)
  823. {
  824. ucMask = 0x80;
  825. uc = *s++;
  826. }
  827. if (uc & ucMask) // active pixel
  828. {
  829. ucPixel &= 0xf;
  830. ucPixel |= (ucColor << 4);
  831. }
  832. ucMask >>= 1;
  833. if (uc & ucMask) // odd pixel
  834. {
  835. ucPixel &= 0xf0;
  836. ucPixel |= ucColor;
  837. }
  838. *d++ = ucPixel; // write modified pixel pair
  839. } // for x
  840. } // for y
  841. } /* ssd1327DrawPattern() */
  842. //
  843. // Display part or whole of the backbuffer or a custom bitmap to the visible display
  844. // Pass a NULL pointer to display the backbuffer
  845. //
  846. void ssd1327ShowBitmap(uint8_t *pBuffer, int iLocalPitch, int x, int y, int w, int h)
  847. {
  848. int ty;
  849. uint8_t *s;
  850. if (x < 0 || y < 0 || x >= iMaxX || y >= iMaxY || (x+w) > iMaxX || (y+h) > iMaxY)
  851. return; // invalid coordinates
  852. if (pBuffer == NULL)
  853. {
  854. pBuffer = &ucBackbuffer[(y*iPitch)+(x/2)]; // starting point also
  855. iLocalPitch = iPitch;
  856. }
  857. ssd1327SetPosition(x, y, w, h);
  858. for (ty=0; ty<h; ty++)
  859. {
  860. s = &pBuffer[iLocalPitch * (y+ty)];
  861. ssd1327WriteDataBlock(&s[x/2], w/2);
  862. } // for y
  863. } /* ssd1327ShowBitmap() */
  864. // Set an individual pixel to a specific color
  865. // Only affects the backbuffer and must be explicitly
  866. // displayed later with ssd1327ShowBuffer()
  867. void ssd1327SetPixel(int x, int y, unsigned char ucColor)
  868. {
  869. uint8_t c, *d;
  870. d = &ucBackbuffer[(y*iPitch)+x/2];
  871. c = d[0];
  872. if (x & 1)
  873. {
  874. c &= 0xf0; // right pixel
  875. c |= ucColor;
  876. }
  877. else
  878. {
  879. c &= 0xf;
  880. c |= (ucColor << 4);
  881. }
  882. d[0] = c;
  883. } /* ssd1327SetPixel() */
  884. #endif // USE_BACKBUFFER
  885. //
  886. // Sets the brightness (0=off, 255=brightest)
  887. //
  888. void ssd1327SetContrast(unsigned char ucContrast)
  889. {
  890. ssd1327WriteCommand2(0x81, ucContrast);
  891. } /* ssd1327SetContrast() */
  892. //
  893. // Fill the frame buffer with a byte pattern
  894. // e.g. all off (0x00) or all on (0xff)
  895. //
  896. void ssd1327Fill(unsigned char ucColor)
  897. {
  898. uint8_t x, y;
  899. unsigned char temp[16];
  900. ucColor |= (ucColor << 4); // set pixel pair color
  901. memset(temp, ucColor, 16);
  902. #ifdef USE_BACKBUFFER
  903. memset(ucBackbuffer, ucColor, sizeof(ucBackbuffer));
  904. #endif
  905. ssd1327SetPosition(0,0,iMaxX,iMaxY);
  906. for (y=0; y<iMaxY; y++)
  907. {
  908. for (x=0; x<iMaxX/32; x++)
  909. {
  910. ssd1327WriteDataBlock(temp, 16);
  911. } // for x
  912. } // for y
  913. } /* ssd1327Fill() */
  914. #ifdef USE_BACKBUFFER
  915. void ssd1327Rectangle(int x, int y, int w, int h, uint8_t ucColor, int bFill)
  916. {
  917. int i;
  918. uint8_t *d;
  919. ucColor |= (ucColor << 4); // left/right in a byte
  920. // check bounds
  921. if (x < 0 || x >= iMaxX || x+w > iMaxX)
  922. return; // out of bounds
  923. if (y < 0 || y >= iMaxY || y+h > iMaxY)
  924. return;
  925. if (bFill)
  926. {
  927. for (i=0; i<h; i++)
  928. {
  929. d = &ucBackbuffer[(y+i)*iPitch + (x/2)];
  930. memset(d, ucColor, w/2);
  931. } // for i
  932. } // filled
  933. else // outline
  934. {
  935. // draw top/bottom
  936. d = &ucBackbuffer[y*iPitch + (x/2)];
  937. memset(d, ucColor, w/2);
  938. d = &ucBackbuffer[(y+h-1)*iPitch + (x/2)];
  939. memset(d, ucColor, w/2);
  940. // draw left/right
  941. ucColor &= 0xf;
  942. d = &ucBackbuffer[(y*iPitch)+(x/2)];
  943. for (i=0; i<h; i++)
  944. {
  945. d[0] &= 0xf; // set left pixel
  946. d[0] |= (ucColor << 4);
  947. d[w/2-1] &= 0xf0; // set right pixel
  948. d[w/2-1] |= ucColor;
  949. d += iPitch;
  950. }
  951. } // outline
  952. } /* ssd1327Rectangle() */
  953. uint8_t *ssd1327GetBackbuffer(void)
  954. {
  955. return ucBackbuffer;
  956. }
  957. //
  958. // Table of sine values for 0-360 degrees expressed as a signed 16-bit value
  959. // from -32768 (-1) to 32767 (1)
  960. //
  961. int16_t i16SineTable[] = {0,572, 1144, 1715, 2286, 2856, 3425, 3993, 4560, 5126, // 0-9
  962. 5690, 6252, 6813, 7371, 7927, 8481, 9032, 9580, 10126, 10668, // 10-19
  963. 11207, 11743, 12275, 12803, 13328, 13848, 14365, 14876, 15384, 15886,// 20-29
  964. 16384, 16877, 17364, 17847, 18324, 18795, 19261, 19720, 20174, 20622,// 30-39
  965. 21063, 21498, 21926, 22348, 22763, 23170, 23571, 23965, 24351, 24730,// 40-49
  966. 25102, 25466, 25822, 26170, 26510, 26842, 27166, 27482, 27789, 28088,// 50-59
  967. 28378, 28660, 28932, 29197, 29452, 29698, 29935, 30163, 30382, 30592,// 60-69
  968. 30792, 30983, 31164, 31336, 31499, 31651, 31795, 31928, 32052, 32166,// 70-79
  969. 32270, 32365, 32440, 32524, 32599, 32643, 32688, 32723, 32748, 32763,// 80-89
  970. 32767, 32763, 32748, 32723, 32688, 32643, 32588, 32524, 32449, 32365,// 90-99
  971. 32270, 32166, 32052, 31928, 31795, 31651, 31499, 31336, 31164, 30983,// 100-109
  972. 30792, 30592, 30382, 30163, 29935, 29698, 29452, 29197, 28932, 28660,// 110-119
  973. 28378, 28088, 27789, 27482, 27166, 26842, 26510, 26170, 25822, 25466,// 120-129
  974. 25102, 24730, 24351, 23965, 23571, 23170, 22763, 22348, 21926, 21498,// 130-139
  975. 21063, 20622, 20174, 19720, 19261, 18795, 18324, 17847, 17364, 16877,// 140-149
  976. 16384, 15886, 15384, 14876, 14365, 13848, 13328, 12803, 12275, 11743,// 150-159
  977. 11207, 10668, 10126, 9580, 9032, 8481, 7927, 7371, 6813, 6252,// 160-169
  978. 5690, 5126, 4560, 3993, 3425, 2856, 2286, 1715, 1144, 572,// 170-179
  979. 0, -572, -1144, -1715, -2286, -2856, -3425, -3993, -4560, -5126,// 180-189
  980. -5690, -6252, -6813, -7371, -7927, -8481, -9032, -9580, -10126, -10668,// 190-199
  981. -11207, -11743, -12275, -12803, -13328, -13848, -14365, -14876, -15384, -15886,// 200-209
  982. -16384, -16877, -17364, -17847, -18324, -18795, -19261, -19720, -20174, -20622,// 210-219
  983. -21063, -21498, -21926, -22348, -22763, -23170, -23571, -23965, -24351, -24730, // 220-229
  984. -25102, -25466, -25822, -26170, -26510, -26842, -27166, -27482, -27789, -28088, // 230-239
  985. -28378, -28660, -28932, -29196, -29452, -29698, -29935, -30163, -30382, -30592, // 240-249
  986. -30792, -30983, -31164, -31336, -31499, -31651, -31795, -31928, -32052, -32166, // 250-259
  987. -32270, -32365, -32449, -32524, -32588, -32643, -32688, -32723, -32748, -32763, // 260-269
  988. -32768, -32763, -32748, -32723, -32688, -32643, -32588, -32524, -32449, -32365, // 270-279
  989. -32270, -32166, -32052, -31928, -31795, -31651, -31499, -31336, -31164, -30983, // 280-289
  990. -30792, -30592, -30382, -30163, -29935, -29698, -29452, -29196, -28932, -28660, // 290-299
  991. -28378, -28088, -27789, -27482, -27166, -26842, -26510, -26170, -25822, -25466, // 300-309
  992. -25102, -24730, -24351, -23965, -23571, -23170, -22763, -22348, -21926, -21498, // 310-319
  993. -21063, -20622, -20174, -19720, -19261, -18795, -18234, -17847, -17364, -16877, // 320-329
  994. -16384, -15886, -15384, -14876, -14365, -13848, -13328, -12803, -12275, -11743, // 330-339
  995. -11207, -10668, -10126, -9580, -9032, -8481, -7927, -7371, -6813, -6252,// 340-349
  996. -5690, -5126, -4560, -3993, -3425, -2856, -2286, -1715, -1144, -572, // 350-359
  997. // an extra 90 degrees to simulate the cosine function
  998. 0,572, 1144, 1715, 2286, 2856, 3425, 3993, 4560, 5126,// 0-9
  999. 5690, 6252, 6813, 7371, 7927, 8481, 9032, 9580, 10126, 10668,// 10-19
  1000. 11207, 11743, 12275, 12803, 13328, 13848, 14365, 14876, 15384, 15886,// 20-29
  1001. 16384, 16877, 17364, 17847, 18324, 18795, 19261, 19720, 20174, 20622,// 30-39
  1002. 21063, 21498, 21926, 22348, 22763, 23170, 23571, 23965, 24351, 24730,// 40-49
  1003. 25102, 25466, 25822, 26170, 26510, 26842, 27166, 27482, 27789, 28088,// 50-59
  1004. 28378, 28660, 28932, 29197, 29452, 29698, 29935, 30163, 30382, 30592,// 60-69
  1005. 30792, 30983, 31164, 31336, 31499, 31651, 31795, 31928, 32052, 32166,// 70-79
  1006. 32270, 32365, 32440, 32524, 32599, 32643, 32688, 32723, 32748, 32763}; // 80-89
  1007. //
  1008. // Rotate a 1-bpp or 4-bpp image around a given center point
  1009. // valid angles are 0-359
  1010. //
  1011. void ssd1327RotateBitmap(uint8_t *pSrc, uint8_t *pDest, int iBpp, int iWidth, int iHeight, int iPitch, int iCenterX, int iCenterY, int iAngle)
  1012. {
  1013. int32_t i, j, x, y;
  1014. int16_t pre_sin[512], pre_cos[512], *pSin, *pCos;
  1015. int32_t tx, ty, sa, ca;
  1016. uint8_t *s, *d, uc, ucMask;
  1017. if (pSrc == NULL || pDest == NULL || iWidth < 2 || iHeight < 1 || iPitch < 1 || iAngle < 0 || iAngle > 359 || iCenterX < 0 || iCenterX >= iWidth || iCenterY < 0 || iCenterY >= iHeight || (iBpp != 1 && iBpp != 4))
  1018. return;
  1019. // since we're rotating from dest back to source, reverse the angle
  1020. iAngle = 360 - iAngle;
  1021. if (iAngle == 360) // just copy src to dest
  1022. {
  1023. memcpy(pDest, pSrc, iHeight * iPitch);
  1024. return;
  1025. }
  1026. // Create a quicker lookup table for sin/cos pre-multiplied at the given angle
  1027. sa = (int32_t)i16SineTable[iAngle]; // sine of given angle
  1028. ca = (int32_t)i16SineTable[iAngle+90]; // cosine of given angle
  1029. for (i=-256; i<256; i++) // create the pre-calc tables
  1030. {
  1031. pre_sin[i+256] = (sa * i) >> 15; // sin * x
  1032. pre_cos[i+256] = (ca * i) >> 15;
  1033. }
  1034. pSin = &pre_sin[256]; pCos = &pre_cos[256]; // point to 0 points in tables
  1035. for (y=0; y<iHeight; y++)
  1036. {
  1037. int16_t siny = pSin[y-iCenterY];
  1038. int16_t cosy = pCos[y-iCenterY];
  1039. int iShift;
  1040. uint8_t ucPixel;
  1041. d = &pDest[y * iPitch];
  1042. ucMask = 0x80; // for 1-bpp
  1043. iShift = 0; // for 4-bpp
  1044. uc = ucPixel = 0;
  1045. for (x=0; x<iWidth; x++)
  1046. {
  1047. // Rotate from the destination pixel back to the source to not have gaps
  1048. // x' = cos*x - sin*y, y' = sin*x + cos*y
  1049. tx = iCenterX + pCos[x-iCenterX] - siny;
  1050. ty = iCenterY + pSin[x-iCenterX] + cosy;
  1051. if (iBpp == 1)
  1052. {
  1053. if (tx > 0 && ty > 0 && tx < iWidth && ty < iHeight) // check source pixel
  1054. {
  1055. s = &pSrc[(ty*iPitch)+(tx>>3)];
  1056. if (s[0] & (0x80 >> (tx & 7)))
  1057. uc |= ucMask; // set destination pixel
  1058. }
  1059. ucMask >>= 1;
  1060. if (ucMask == 0) // write the byte into the destination bitmap
  1061. {
  1062. ucMask = 0x80;
  1063. *d++ = uc;
  1064. uc = 0;
  1065. }
  1066. }
  1067. else // 4-bpp
  1068. {
  1069. if (tx > 0 && ty > 0 && tx < iWidth && ty < iHeight) // check source pixel
  1070. {
  1071. uc = pSrc[(ty*iPitch)+(tx/2)];
  1072. if (tx & 1)
  1073. uc >>= 4;
  1074. else
  1075. uc &= 0xf; // isolate the pixel in the lower 4 bits
  1076. ucPixel |= (uc << iShift);
  1077. iShift = 4-iShift;
  1078. if (iShift == 0) // time to store the pair
  1079. {
  1080. *d++ = ucPixel;
  1081. ucPixel = 0;
  1082. }
  1083. }
  1084. }
  1085. }
  1086. if (iBpp == 1 && ucMask != 0x80) // store partial byte
  1087. *d++ = uc;
  1088. if (iBpp == 4 && iShift == 4) // store partial byte
  1089. *d++ = ucPixel;
  1090. } // for y
  1091. } /* ssd1327RotateBitmap() */
  1092. void ssd1327DrawLine(int x1, int y1, int x2, int y2, uint8_t ucColor)
  1093. {
  1094. int temp;
  1095. int dx = x2 - x1;
  1096. int dy = y2 - y1;
  1097. int error;
  1098. uint8_t *p;
  1099. int xinc, yinc, shift;
  1100. int y, x;
  1101. if (x1 < 0 || x2 < 0 || y1 < 0 || y2 < 0 || x1 >= iMaxX || x2 >= iMaxX || y1 >= iMaxY || y2 >= iMaxY)
  1102. return;
  1103. if(abs(dx) > abs(dy)) {
  1104. // X major case
  1105. if(x2 < x1) {
  1106. dx = -dx;
  1107. temp = x1;
  1108. x1 = x2;
  1109. x2 = temp;
  1110. temp = y1;
  1111. y1 = y2;
  1112. y2 = temp;
  1113. }
  1114. y = y1;
  1115. dy = (y2 - y1);
  1116. error = dx >> 1;
  1117. yinc = 1;
  1118. if (dy < 0)
  1119. {
  1120. dy = -dy;
  1121. yinc = -1;
  1122. }
  1123. p = &ucBackbuffer[(x1/2) + (y1 * iPitch)]; // point to current spot in back buffer
  1124. shift = (x1 & 1) ? 0:4; // current bit offset
  1125. for(x=x1; x1 <= x2; x1++) {
  1126. *p &= (0xf0 >> shift);
  1127. *p |= (ucColor << shift);
  1128. shift = 4-shift;
  1129. if (shift == 4) // time to increment pointer
  1130. p++;
  1131. error -= dy;
  1132. if (error < 0)
  1133. {
  1134. error += dx;
  1135. if (yinc > 0)
  1136. p += iPitch;
  1137. else
  1138. p -= iPitch;
  1139. y += yinc;
  1140. }
  1141. } // for x1
  1142. }
  1143. else {
  1144. // Y major case
  1145. if(y1 > y2) {
  1146. dy = -dy;
  1147. temp = x1;
  1148. x1 = x2;
  1149. x2 = temp;
  1150. temp = y1;
  1151. y1 = y2;
  1152. y2 = temp;
  1153. }
  1154. p = &ucBackbuffer[(x1/2) + (y1 * iPitch)]; // point to current spot in back buffer
  1155. shift = (x1 & 1) ? 0:4; // current bit offset
  1156. dx = (x2 - x1);
  1157. error = dy >> 1;
  1158. xinc = 1;
  1159. if (dx < 0)
  1160. {
  1161. dx = -dx;
  1162. xinc = -1;
  1163. }
  1164. for(x = x1; y1 <= y2; y1++) {
  1165. *p &= (0xf0 >> shift); // set the pixel
  1166. *p |= (ucColor << shift);
  1167. error -= dx;
  1168. p += iPitch; // y1++
  1169. if (error < 0)
  1170. {
  1171. error += dy;
  1172. x += xinc;
  1173. shift = 4-shift;
  1174. if (xinc == 1)
  1175. {
  1176. if (shift == 4) // time to increment pointer
  1177. p++;
  1178. }
  1179. else
  1180. {
  1181. if (shift == 0)
  1182. p--;
  1183. }
  1184. }
  1185. } // for y
  1186. } // y major case
  1187. } /* ssd1327DrawLine() */
  1188. #endif // USE_BACKBUFFER