-
Notifications
You must be signed in to change notification settings - Fork 29
/
DrawChar.ino
138 lines (111 loc) · 4.36 KB
/
DrawChar.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Iterates through all the characters in the default font. See:
* https://makeabilitylab.github.io/physcomp/advancedio/oled.html#drawing-text
*
* This code has been tested on:
* - Adafruit Huzzah32 ESP32
* - Arduino Leonardo
*
* For our OLED tutorials, see:
* https://makeabilitylab.github.io/physcomp/advancedio/oled.html
*
* Adafruit Gfx Library:
* https://learn.adafruit.com/adafruit-gfx-graphics-library/overview
*
* Adafruit OLED tutorials:
* https://learn.adafruit.com/monochrome-oled-breakouts
*
* By Jon E. Froehlich
* @jonfroehlich
* http://makeabilitylab.io
*
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 _display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int NEXT_CHAR_BUTTON_INPUT_PIN = 4;
int _lastButtonVal = HIGH;
int _currentCharIndex = 1; // start with frowney face
const int MAX_CHARS = 256;
void setup() {
Serial.begin(9600);
pinMode(NEXT_CHAR_BUTTON_INPUT_PIN, INPUT_PULLUP);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!_display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
_display.clearDisplay();
_display.setTextSize(3);
_display.setTextColor(SSD1306_WHITE); // Draw white text
_display.setCursor(0, 0); // Start at top-left corner
// Set cp437 to true to use full 256 characters. According to the Adafruit_GFX.h docs
// There was an error in glcdfont.c for the longest time -- one character (#176, the 'light shade' block)
// was missing -- this threw off the index of every character that followed it. But a TON of code has
// been written with the erroneous character indices. By default, the library uses the original
// 'wrong' behavior and old sketches will still work. Pass 'true' to this function to use correct
// CP437 character values in your code.
// See: https://github.com/adafruit/Adafruit-GFX-Library/blob/master/Adafruit_GFX.h
//
// This is generally *NOT* important but only if we are going to iterate through all chars
_display.cp437(true);
showAllChars();
delay(1000);
_display.clearDisplay();
drawChar(_currentCharIndex);
_display.display(); // show the graphics buffer to screen
}
void loop() {
int buttonVal = digitalRead(NEXT_CHAR_BUTTON_INPUT_PIN);
// Iterate through
if(_lastButtonVal != buttonVal && buttonVal == LOW){
_display.clearDisplay();
_currentCharIndex++;
if(_currentCharIndex > MAX_CHARS){
_currentCharIndex = 0;
}
drawChar(_currentCharIndex);
_display.display(); // show the graphics buffer to screen
delay(200);
}
_lastButtonVal = buttonVal;
Serial.println(buttonVal);
}
void showAllChars(){
_display.setTextSize(1); // Normal 1:1 pixel scale
_display.setTextColor(SSD1306_WHITE); // Draw white text
_display.setCursor(0, 0); // Start at top-left corner
// Not all the characters will fit on the display. This is normal.
// Library will draw what it can and the rest will be clipped.
// Notice that we are using .write here rather than .print!
for(int16_t i=0; i<256; i++) {
if(i == '\n') _display.write(' ');
else _display.write(i);
}
_display.display(); // show the graphics buffer to screen
}
void drawChar(int charIndex){
int16_t x1, y1;
uint16_t textWidth, textHeight;
_display.setTextSize(3);
_display.getTextBounds("X", 0, 0, &x1, &y1, &textWidth, &textHeight);
uint16_t yText = _display.height() / 2 - textHeight + 4;
uint16_t xText = _display.width() / 2 - textWidth / 2;
_display.setCursor(xText, yText);
_display.write(_currentCharIndex);
yText += textHeight + 4;
// We could have also done this:
// _display.drawChar(xText, yText, (char)charIndex, SSD1306_WHITE, SSD1306_BLACK, 3);
_display.setTextSize(1);
String strLbl = (String)"Char: " + _currentCharIndex;
_display.getTextBounds(strLbl, 0, 0, &x1, &y1, &textWidth, &textHeight);
xText = _display.width() / 2 - textWidth / 2;
_display.setCursor(xText, yText);
_display.print(strLbl);
}