@@ -29,54 +29,50 @@ Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
2929#include < Wire.h>
3030
3131
32- SSD1306::SSD1306 (int i2cAddress, int sda, int sdc)
33- {
32+ SSD1306::SSD1306 (int i2cAddress, int sda, int sdc) {
3433 myI2cAddress = i2cAddress;
3534 mySda = sda;
3635 mySdc = sdc;
37-
3836}
3937
4038void SSD1306::init () {
4139 Wire.begin (mySda, mySdc);
42- Wire.setClock (400000 );
40+ Wire.setClock (400000 );
4341 sendInitCommands ();
4442 resetDisplay ();
4543}
4644
47- void SSD1306::resetDisplay (void )
48- {
45+ void SSD1306::resetDisplay (void ) {
4946 displayOff ();
5047 clear ();
5148 display ();
5249 displayOn ();
5350}
5451
5552void SSD1306::reconnect () {
56- Wire.begin (mySda, mySdc);
53+ Wire.begin (mySda, mySdc);
5754}
5855
59- void SSD1306::displayOn (void )
60- {
56+ void SSD1306::displayOn (void ) {
6157 sendCommand (0xaf ); // display on
6258}
6359
64- void SSD1306::displayOff (void )
65- {
66- sendCommand (0xae ); // display off
60+ void SSD1306::displayOff (void ) {
61+ sendCommand (0xae ); // display off
6762}
6863
6964void SSD1306::setContrast (char contrast) {
7065 sendCommand (0x81 );
71- sendCommand (contrast);
66+ sendCommand (contrast);
7267}
68+
7369void SSD1306::flipScreenVertically () {
7470 sendCommand (0xA0 | 0x1 ); // SEGREMAP //Rotate screen 180 deg
75-
7671 sendCommand (0xC8 ); // COMSCANDEC Rotate screen 180 Deg
7772}
73+
7874void SSD1306::clear (void ) {
79- memset (buffer, 0 , (128 * 64 / 8 ));
75+ memset (buffer, 0 , (128 * 64 / 8 ));
8076}
8177
8278void SSD1306::display (void ) {
@@ -88,10 +84,8 @@ void SSD1306::display(void) {
8884 sendCommand (0x0 );
8985 sendCommand (0x7 );
9086
91-
9287 for (uint16_t i=0 ; i<(128 *64 /8 ); i++) {
9388 // send a bunch of data in one xmission
94- // Wire.begin(mySda, mySdc);
9589 Wire.beginTransmission (myI2cAddress);
9690 Wire.write (0x40 );
9791 for (uint8_t x=0 ; x<16 ; x++) {
@@ -102,18 +96,17 @@ void SSD1306::display(void) {
10296 yield ();
10397 Wire.endTransmission ();
10498 }
105-
10699
107100
108101}
109102
110103void SSD1306::setPixel (int x, int y) {
111104 if (x >= 0 && x < 128 && y >= 0 && y < 64 ) {
112-
105+
113106 switch (myColor) {
114- case WHITE: buffer[x+ (y/8 )*128 ] |= (1 << (y&7 )); break ;
115- case BLACK: buffer[x+ (y/8 )*128 ] &= ~(1 << (y&7 )); break ;
116- case INVERSE: buffer[x+ (y/8 )*128 ] ^= (1 << (y&7 )); break ;
107+ case WHITE: buffer[x + (y/8 )*128 ] |= (1 << (y&7 )); break ;
108+ case BLACK: buffer[x + (y/8 )*128 ] &= ~(1 << (y&7 )); break ;
109+ case INVERSE: buffer[x + (y/8 )*128 ] ^= (1 << (y&7 )); break ;
117110 }
118111 }
119112}
@@ -122,44 +115,11 @@ void SSD1306::setChar(int x, int y, unsigned char data) {
122115 for (int i = 0 ; i < 8 ; i++) {
123116 if (bitRead (data, i)) {
124117 setPixel (x,y + i);
125- }
126- }
127- }
128-
129- // Code form http://playground.arduino.cc/Main/Utf8ascii
130- byte SSD1306::utf8ascii (byte ascii) {
131- if ( ascii<128 ) // Standard ASCII-set 0..0x7F handling
132- { lastChar=0 ;
133- return ( ascii );
134- }
135-
136- // get previous input
137- byte last = lastChar; // get last char
138- lastChar=ascii; // remember actual character
139-
140- switch (last) // conversion depnding on first UTF8-character
141- { case 0xC2 : return (ascii); break ;
142- case 0xC3 : return (ascii | 0xC0 ); break ;
143- case 0x82 : if (ascii==0xAC ) return (0x80 ); // special case Euro-symbol
144118 }
145-
146- return (0 ); // otherwise: return zero, if character has to be ignored
147- }
148-
149- // Code form http://playground.arduino.cc/Main/Utf8ascii
150- String SSD1306::utf8ascii (String s) {
151- String r= " " ;
152- char c;
153- for (int i=0 ; i<s.length (); i++)
154- {
155- c = utf8ascii (s.charAt (i));
156- if (c!=0 ) r+=c;
157- }
158- return r;
119+ }
159120}
160121
161122void SSD1306::drawString (int x, int y, String text) {
162- text = utf8ascii (text);
163123 unsigned char currentByte;
164124 int charX, charY;
165125 int currentBitCount;
@@ -192,7 +152,7 @@ void SSD1306::drawString(int x, int y, String text) {
192152 currentCharWidth = pgm_read_byte (myFontData + CHAR_WIDTH_START_POS + charCode);
193153 // Jump to font data beginning
194154 currentCharStartPos = CHAR_WIDTH_START_POS + numberOfChars;
195-
155+
196156 for (int m = 0 ; m < charCode; m++) {
197157
198158 currentCharStartPos += pgm_read_byte (myFontData + CHAR_WIDTH_START_POS + m) * charHeight / 8 + 1 ;
@@ -201,26 +161,24 @@ void SSD1306::drawString(int x, int y, String text) {
201161 currentCharByteNum = ((charHeight * currentCharWidth) / 8 ) + 1 ;
202162 // iterate over all bytes of character
203163 for (int i = 0 ; i < currentCharByteNum; i++) {
204-
164+
205165 currentByte = pgm_read_byte (myFontData + currentCharStartPos + i);
206166 // Serial.println(String(charCode) + ", " + String(currentCharWidth) + ", " + String(currentByte));
207167 // iterate over all bytes of character
208168 for (int bit = 0 ; bit < 8 ; bit++) {
209169 // int currentBit = bitRead(currentByte, bit);
210-
170+
211171 currentBitCount = i * 8 + bit;
212172
213173 charX = currentBitCount % currentCharWidth;
214174 charY = currentBitCount / currentCharWidth;
215175
216176 if (bitRead (currentByte, bit)) {
217- // Serial.println(String(charX) + ", " + String(charY));
218- setPixel (startX + cursorX + charX, startY + charY);
219- // setPixel(charX, charY);
177+ setPixel (startX + cursorX + charX, startY + charY);
220178 }
221179
222- }
223- yield ();
180+ }
181+ yield ();
224182 }
225183 cursorX += currentCharWidth;
226184
@@ -248,13 +206,12 @@ void SSD1306::drawStringMaxWidth(int x, int y, int maxLineWidth, String text) {
248206 startsAt = endsAt + 1 ;
249207 }
250208 }
251-
209+
252210 }
253211 drawString (x, y + lineNumber * lineHeight, text.substring (startsAt));
254212}
255213
256214int SSD1306::getStringWidth (String text) {
257- text = utf8ascii (text);
258215 int stringWidth = 0 ;
259216 char charCode;
260217 for (int j=0 ; j < text.length (); j++) {
@@ -275,34 +232,34 @@ void SSD1306::setFont(const char *fontData) {
275232void SSD1306::drawBitmap (int x, int y, int width, int height, const char *bitmap) {
276233 for (int i = 0 ; i < width * height / 8 ; i++ ){
277234 unsigned char charColumn = 255 - pgm_read_byte (bitmap + i);
278- for (int j = 0 ; j < 8 ; j++) {
235+ for (int j = 0 ; j < 8 ; j++) {
279236 int targetX = i % width + x;
280237 int targetY = (i / (width)) * 8 + j + y;
281238 if (bitRead (charColumn, j)) {
282- setPixel (targetX, targetY);
239+ setPixel (targetX, targetY);
283240 }
284241 }
285- }
242+ }
286243}
287244
288245void SSD1306::setColor (int color) {
289- myColor = color;
246+ myColor = color;
290247}
291248
292249void SSD1306::drawRect (int x, int y, int width, int height) {
293250 for (int i = x; i < x + width; i++) {
294251 setPixel (i, y);
295- setPixel (i, y + height);
252+ setPixel (i, y + height);
296253 }
297254 for (int i = y; i < y + height; i++) {
298255 setPixel (x, i);
299- setPixel (x + width, i);
256+ setPixel (x + width, i);
300257 }
301258}
302259
303260void SSD1306::fillRect (int x, int y, int width, int height) {
304261 for (int i = x; i < x + width; i++) {
305- for (int j = y ; j < y + height; j++) {
262+ for (int j = 0 ; j < y + height; j++) {
306263 setPixel (i, j);
307264 }
308265 }
@@ -314,28 +271,24 @@ void SSD1306::drawXbm(int x, int y, int width, int height, const char *xbm) {
314271 }
315272 for (int i = 0 ; i < width * height / 8 ; i++ ){
316273 unsigned char charColumn = pgm_read_byte (xbm + i);
317- for (int j = 0 ; j < 8 ; j++) {
274+ for (int j = 0 ; j < 8 ; j++) {
318275 int targetX = (i * 8 + j) % width + x;
319276 int targetY = (8 * i / (width)) + y;
320277 if (bitRead (charColumn, j)) {
321- setPixel (targetX, targetY);
278+ setPixel (targetX, targetY);
322279 }
323280 }
324- }
281+ }
325282}
326283
327- void SSD1306::sendCommand (unsigned char com)
328- {
329- // Wire.begin(mySda, mySdc);
284+ void SSD1306::sendCommand (unsigned char com) {
330285 Wire.beginTransmission (myI2cAddress); // begin transmitting
331286 Wire.write (0x80 ); // command mode
332287 Wire.write (com);
333288 Wire.endTransmission (); // stop transmitting
334289}
335290
336- void SSD1306::sendInitCommands (void )
337- {
338-
291+ void SSD1306::sendInitCommands (void ) {
339292 sendCommand (DISPLAYOFF);
340293 sendCommand (NORMALDISPLAY);
341294 sendCommand (SETDISPLAYCLOCKDIV);
@@ -363,53 +316,4 @@ void SSD1306::sendInitCommands(void)
363316 sendCommand (NORMALDISPLAY);
364317 sendCommand (0x2e ); // stop scroll
365318 sendCommand (DISPLAYON);
366-
367- }
368-
369- void SSD1306::nextFrameTick () {
370- myFrameTick++;
371- if (myFrameTick==myFrameWaitTicks && myFrameState == 0 || myFrameTick==myFrameTransitionTicks && myFrameState == 1 ) {
372- myFrameState = (myFrameState + 1 ) % 2 ;
373- if (myFrameState==FRAME_STATE_FIX) {
374- myCurrentFrame = (myCurrentFrame + 1 ) % myFrameCount;
375- }
376- myFrameTick = 0 ;
377- }
378- drawIndicators (myFrameCount, myCurrentFrame);
379-
380- switch (myFrameState) {
381- case 0 :
382- (*myFrameCallbacks[myCurrentFrame])(0 , 0 );
383- break ;
384- case 1 :
385- (*myFrameCallbacks[myCurrentFrame])(-128 * myFrameTick / myFrameTransitionTicks, 0 );
386- (*myFrameCallbacks[(myCurrentFrame + 1 ) % myFrameCount])(-128 * myFrameTick / myFrameTransitionTicks + 128 , 0 );
387- break ;
388- }
389-
390- }
391- void SSD1306::drawIndicators (int frameCount, int activeFrame) {
392- for (int i = 0 ; i < frameCount; i++) {
393- const char *xbm;
394- if (activeFrame == i) {
395- xbm = active_bits;
396- } else {
397- xbm = inactive_bits;
398- }
399- drawXbm (64 - (12 * frameCount / 2 ) + 12 * i,56 , 8 , 8 , xbm);
400- }
401- }
402- void SSD1306::setFrameCallbacks (int frameCount, void (*frameCallbacks[])(int x, int y)) {
403- myFrameCount = frameCount;
404- myFrameCallbacks = frameCallbacks;
405- }
406-
407- void SSD1306::setFrameWaitTicks (int frameWaitTicks) {
408- myFrameWaitTicks = frameWaitTicks;
409- }
410- void SSD1306::setFrameTransitionTicks (int frameTransitionTicks) {
411- myFrameTransitionTicks = frameTransitionTicks;
412- }
413- int SSD1306::getFrameState () {
414- return myFrameState;
415319}
0 commit comments