ssd1306_128x64_i2c.ino 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /**************************************************************************
  2. This is an example for our Monochrome OLEDs based on SSD1306 drivers
  3. Pick one up today in the adafruit shop!
  4. ------> http://www.adafruit.com/category/63_98
  5. This example is for a 128x64 pixel display using I2C to communicate
  6. 3 pins are required to interface (two I2C and one reset).
  7. Adafruit invests time and resources providing this open
  8. source code, please support Adafruit and open-source
  9. hardware by purchasing products from Adafruit!
  10. Written by Limor Fried/Ladyada for Adafruit Industries,
  11. with contributions from the open source community.
  12. BSD license, check license.txt for more information
  13. All text above, and the splash screen below must be
  14. included in any redistribution.
  15. **************************************************************************/
  16. #include <SPI.h>
  17. #include <Wire.h>
  18. #include <Adafruit_GFX.h>
  19. #include <Adafruit_SSD1306.h>
  20. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  21. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  22. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  23. // The pins for I2C are defined by the Wire-library.
  24. // On an arduino UNO: A4(SDA), A5(SCL)
  25. // On an arduino MEGA 2560: 20(SDA), 21(SCL)
  26. // On an arduino LEONARDO: 2(SDA), 3(SCL), ...
  27. #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  28. #define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  29. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  30. #define NUMFLAKES 10 // Number of snowflakes in the animation example
  31. #define LOGO_HEIGHT 16
  32. #define LOGO_WIDTH 16
  33. static const unsigned char PROGMEM logo_bmp[] =
  34. { 0b00000000, 0b11000000,
  35. 0b00000001, 0b11000000,
  36. 0b00000001, 0b11000000,
  37. 0b00000011, 0b11100000,
  38. 0b11110011, 0b11100000,
  39. 0b11111110, 0b11111000,
  40. 0b01111110, 0b11111111,
  41. 0b00110011, 0b10011111,
  42. 0b00011111, 0b11111100,
  43. 0b00001101, 0b01110000,
  44. 0b00011011, 0b10100000,
  45. 0b00111111, 0b11100000,
  46. 0b00111111, 0b11110000,
  47. 0b01111100, 0b11110000,
  48. 0b01110000, 0b01110000,
  49. 0b00000000, 0b00110000 };
  50. void setup() {
  51. Serial.begin(9600);
  52. // Wait for display
  53. delay(500);
  54. // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  55. if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  56. Serial.println(F("SSD1306 allocation failed"));
  57. for(;;); // Don't proceed, loop forever
  58. }
  59. // Show initial display buffer contents on the screen --
  60. // the library initializes this with an Adafruit splash screen.
  61. display.display();
  62. delay(2000); // Pause for 2 seconds
  63. // Clear the buffer
  64. display.clearDisplay();
  65. // Draw a single pixel in white
  66. display.drawPixel(10, 10, SSD1306_WHITE);
  67. // Show the display buffer on the screen. You MUST call display() after
  68. // drawing commands to make them visible on screen!
  69. display.display();
  70. delay(2000);
  71. // display.display() is NOT necessary after every single drawing command,
  72. // unless that's what you want...rather, you can batch up a bunch of
  73. // drawing operations and then update the screen all at once by calling
  74. // display.display(). These examples demonstrate both approaches...
  75. testdrawline(); // Draw many lines
  76. testdrawrect(); // Draw rectangles (outlines)
  77. testfillrect(); // Draw rectangles (filled)
  78. testdrawcircle(); // Draw circles (outlines)
  79. testfillcircle(); // Draw circles (filled)
  80. testdrawroundrect(); // Draw rounded rectangles (outlines)
  81. testfillroundrect(); // Draw rounded rectangles (filled)
  82. testdrawtriangle(); // Draw triangles (outlines)
  83. testfilltriangle(); // Draw triangles (filled)
  84. testdrawchar(); // Draw characters of the default font
  85. testdrawstyles(); // Draw 'stylized' characters
  86. testscrolltext(); // Draw scrolling text
  87. testdrawbitmap(); // Draw a small bitmap image
  88. // Invert and restore display, pausing in-between
  89. display.invertDisplay(true);
  90. delay(1000);
  91. display.invertDisplay(false);
  92. delay(1000);
  93. testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); // Animate bitmaps
  94. }
  95. void loop() {
  96. }
  97. void testdrawline() {
  98. int16_t i;
  99. display.clearDisplay(); // Clear display buffer
  100. for(i=0; i<display.width(); i+=4) {
  101. display.drawLine(0, 0, i, display.height()-1, SSD1306_WHITE);
  102. display.display(); // Update screen with each newly-drawn line
  103. delay(1);
  104. }
  105. for(i=0; i<display.height(); i+=4) {
  106. display.drawLine(0, 0, display.width()-1, i, SSD1306_WHITE);
  107. display.display();
  108. delay(1);
  109. }
  110. delay(250);
  111. display.clearDisplay();
  112. for(i=0; i<display.width(); i+=4) {
  113. display.drawLine(0, display.height()-1, i, 0, SSD1306_WHITE);
  114. display.display();
  115. delay(1);
  116. }
  117. for(i=display.height()-1; i>=0; i-=4) {
  118. display.drawLine(0, display.height()-1, display.width()-1, i, SSD1306_WHITE);
  119. display.display();
  120. delay(1);
  121. }
  122. delay(250);
  123. display.clearDisplay();
  124. for(i=display.width()-1; i>=0; i-=4) {
  125. display.drawLine(display.width()-1, display.height()-1, i, 0, SSD1306_WHITE);
  126. display.display();
  127. delay(1);
  128. }
  129. for(i=display.height()-1; i>=0; i-=4) {
  130. display.drawLine(display.width()-1, display.height()-1, 0, i, SSD1306_WHITE);
  131. display.display();
  132. delay(1);
  133. }
  134. delay(250);
  135. display.clearDisplay();
  136. for(i=0; i<display.height(); i+=4) {
  137. display.drawLine(display.width()-1, 0, 0, i, SSD1306_WHITE);
  138. display.display();
  139. delay(1);
  140. }
  141. for(i=0; i<display.width(); i+=4) {
  142. display.drawLine(display.width()-1, 0, i, display.height()-1, SSD1306_WHITE);
  143. display.display();
  144. delay(1);
  145. }
  146. delay(2000); // Pause for 2 seconds
  147. }
  148. void testdrawrect(void) {
  149. display.clearDisplay();
  150. for(int16_t i=0; i<display.height()/2; i+=2) {
  151. display.drawRect(i, i, display.width()-2*i, display.height()-2*i, SSD1306_WHITE);
  152. display.display(); // Update screen with each newly-drawn rectangle
  153. delay(1);
  154. }
  155. delay(2000);
  156. }
  157. void testfillrect(void) {
  158. display.clearDisplay();
  159. for(int16_t i=0; i<display.height()/2; i+=3) {
  160. // The INVERSE color is used so rectangles alternate white/black
  161. display.fillRect(i, i, display.width()-i*2, display.height()-i*2, SSD1306_INVERSE);
  162. display.display(); // Update screen with each newly-drawn rectangle
  163. delay(1);
  164. }
  165. delay(2000);
  166. }
  167. void testdrawcircle(void) {
  168. display.clearDisplay();
  169. for(int16_t i=0; i<max(display.width(),display.height())/2; i+=2) {
  170. display.drawCircle(display.width()/2, display.height()/2, i, SSD1306_WHITE);
  171. display.display();
  172. delay(1);
  173. }
  174. delay(2000);
  175. }
  176. void testfillcircle(void) {
  177. display.clearDisplay();
  178. for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) {
  179. // The INVERSE color is used so circles alternate white/black
  180. display.fillCircle(display.width() / 2, display.height() / 2, i, SSD1306_INVERSE);
  181. display.display(); // Update screen with each newly-drawn circle
  182. delay(1);
  183. }
  184. delay(2000);
  185. }
  186. void testdrawroundrect(void) {
  187. display.clearDisplay();
  188. for(int16_t i=0; i<display.height()/2-2; i+=2) {
  189. display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i,
  190. display.height()/4, SSD1306_WHITE);
  191. display.display();
  192. delay(1);
  193. }
  194. delay(2000);
  195. }
  196. void testfillroundrect(void) {
  197. display.clearDisplay();
  198. for(int16_t i=0; i<display.height()/2-2; i+=2) {
  199. // The INVERSE color is used so round-rects alternate white/black
  200. display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i,
  201. display.height()/4, SSD1306_INVERSE);
  202. display.display();
  203. delay(1);
  204. }
  205. delay(2000);
  206. }
  207. void testdrawtriangle(void) {
  208. display.clearDisplay();
  209. for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) {
  210. display.drawTriangle(
  211. display.width()/2 , display.height()/2-i,
  212. display.width()/2-i, display.height()/2+i,
  213. display.width()/2+i, display.height()/2+i, SSD1306_WHITE);
  214. display.display();
  215. delay(1);
  216. }
  217. delay(2000);
  218. }
  219. void testfilltriangle(void) {
  220. display.clearDisplay();
  221. for(int16_t i=max(display.width(),display.height())/2; i>0; i-=5) {
  222. // The INVERSE color is used so triangles alternate white/black
  223. display.fillTriangle(
  224. display.width()/2 , display.height()/2-i,
  225. display.width()/2-i, display.height()/2+i,
  226. display.width()/2+i, display.height()/2+i, SSD1306_INVERSE);
  227. display.display();
  228. delay(1);
  229. }
  230. delay(2000);
  231. }
  232. void testdrawchar(void) {
  233. display.clearDisplay();
  234. display.setTextSize(1); // Normal 1:1 pixel scale
  235. display.setTextColor(SSD1306_WHITE); // Draw white text
  236. display.setCursor(0, 0); // Start at top-left corner
  237. display.cp437(true); // Use full 256 char 'Code Page 437' font
  238. // Not all the characters will fit on the display. This is normal.
  239. // Library will draw what it can and the rest will be clipped.
  240. for(int16_t i=0; i<256; i++) {
  241. if(i == '\n') display.write(' ');
  242. else display.write(i);
  243. }
  244. display.display();
  245. delay(2000);
  246. }
  247. void testdrawstyles(void) {
  248. display.clearDisplay();
  249. display.setTextSize(1); // Normal 1:1 pixel scale
  250. display.setTextColor(SSD1306_WHITE); // Draw white text
  251. display.setCursor(0,0); // Start at top-left corner
  252. display.println(F("Hello, world!"));
  253. display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  254. display.println(3.141592);
  255. display.setTextSize(2); // Draw 2X-scale text
  256. display.setTextColor(SSD1306_WHITE);
  257. display.print(F("0x")); display.println(0xDEADBEEF, HEX);
  258. display.display();
  259. delay(2000);
  260. }
  261. void testscrolltext(void) {
  262. display.clearDisplay();
  263. display.setTextSize(2); // Draw 2X-scale text
  264. display.setTextColor(SSD1306_WHITE);
  265. display.setCursor(10, 0);
  266. display.println(F("scroll"));
  267. display.display(); // Show initial text
  268. delay(100);
  269. // Scroll in various directions, pausing in-between:
  270. display.startscrollright(0x00, 0x0F);
  271. delay(2000);
  272. display.stopscroll();
  273. delay(1000);
  274. display.startscrollleft(0x00, 0x0F);
  275. delay(2000);
  276. display.stopscroll();
  277. delay(1000);
  278. display.startscrolldiagright(0x00, 0x07);
  279. delay(2000);
  280. display.startscrolldiagleft(0x00, 0x07);
  281. delay(2000);
  282. display.stopscroll();
  283. delay(1000);
  284. }
  285. void testdrawbitmap(void) {
  286. display.clearDisplay();
  287. display.drawBitmap(
  288. (display.width() - LOGO_WIDTH ) / 2,
  289. (display.height() - LOGO_HEIGHT) / 2,
  290. logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
  291. display.display();
  292. delay(1000);
  293. }
  294. #define XPOS 0 // Indexes into the 'icons' array in function below
  295. #define YPOS 1
  296. #define DELTAY 2
  297. void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  298. int8_t f, icons[NUMFLAKES][3];
  299. // Initialize 'snowflake' positions
  300. for(f=0; f< NUMFLAKES; f++) {
  301. icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width());
  302. icons[f][YPOS] = -LOGO_HEIGHT;
  303. icons[f][DELTAY] = random(1, 6);
  304. Serial.print(F("x: "));
  305. Serial.print(icons[f][XPOS], DEC);
  306. Serial.print(F(" y: "));
  307. Serial.print(icons[f][YPOS], DEC);
  308. Serial.print(F(" dy: "));
  309. Serial.println(icons[f][DELTAY], DEC);
  310. }
  311. for(;;) { // Loop forever...
  312. display.clearDisplay(); // Clear the display buffer
  313. // Draw each snowflake:
  314. for(f=0; f< NUMFLAKES; f++) {
  315. display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SSD1306_WHITE);
  316. }
  317. display.display(); // Show the display buffer on the screen
  318. delay(200); // Pause for 1/10 second
  319. // Then update coordinates of each flake...
  320. for(f=0; f< NUMFLAKES; f++) {
  321. icons[f][YPOS] += icons[f][DELTAY];
  322. // If snowflake is off the bottom of the screen...
  323. if (icons[f][YPOS] >= display.height()) {
  324. // Reinitialize to a random position, just off the top
  325. icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width());
  326. icons[f][YPOS] = -LOGO_HEIGHT;
  327. icons[f][DELTAY] = random(1, 6);
  328. }
  329. }
  330. }
  331. }