手持ちのOLEDの使い道に悩んだ挙句、また、しょうもないものを作ってしまった。(笑)
0.96インチ 128×64ドット有機ELディスプレイ(OLED)
MCUはATtiny85を選択。プログラム領域がかなり厳しそうなのはわかっていたのでブートローダー領域が必要なArduinoではなくAtmel Studioを利用したがEEPROM領域も全て使い切ったうえでプログラム領域の残りが、な~~~~んと90バイト弱しかない。(笑)
回路はこんな感じ。意図したわけではなかったがPB1(MISO)にブザー回路が接続されているのでプログラム書き込み時に音が出る。プログラム書き込みする人以外にはわからないけど以外にいいかもしんない。(笑)
HWはメイン基板とAVR-ISPと接続するためのアダプター基板の2種類を作成。
AVR-ISPに接続するときはこんな感じでアダプター基板を接続し、
プログラムを動作させるときはOLED基板を接続する。
ケースはいつものようにハメ込み式で作成。
最初にOLEDをセット。基板とOLED間のスペーサが省略できるようOLEDの片側(画像上部)を固定するためのスリットを作ってある。
メイン基板をセットし裏ブタを閉めれば完成!!
プログラム書き込み時の様子。なんだか昔懐かしいモデムの通信音みたいで面白い。
電源ONでストップウォッチが動作開始するのでPCと接続していればPCの稼働時間が分かるので便利?かも。ストップウォッチとタイマーは各独立して動作し画面を切り替えて操作する仕様となっている。ちなみにストップウォッチは最大49日まで計測可能。って、それに何か意味あるのか?(笑)
タイマー時間設定はこんな感じで行う。タイマー時間の初期値はカップ麺仕様となっている。(笑) が、カップ麺仕様といいつつバッテリー仕様でないところが痛い。でも、省電力化のためのコードは入りそうもないし。(-_-;)
【ソース・コード1】
main.cpp/buzzer.cpp/buzzer.h/timer.cpp/timer/hは新規作成。
その他のコードは今までに公開したArduino用のコードをAtmel Studio用に手直ししているので同名だからといって同じものではないことに注意すべし。
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
/* * main.cpp * * Created: 2018/12/01 * Author : Sasapea's Lab */ #include <stdint.h> #include <stdbool.h> #include <avr/io.h> #include <avr/interrupt.h> #include <avr/wdt.h> #include "timer.h" #include "buzzer.h" #include "PinEdge.h" #include "USITWI.h" #include "SSD1306.h" #define CLOCK_MODE 0 #define TIMER_MODE 1 #define BUZZER_START 100 #define BUZZER_CLICK 20 #define BUZZER_FINISH 1000 #define BUTTON1 0x01, PINB & _BV(PB3) #define BUTTON2 0x02, PINB & _BV(PB4) #define OLED_ADDR 0x3C #define TWI_BUS USITWI<USITWI_400K> TWI_BUS bus; SSD1306<TWI_BUS, bus, OLED_ADDR> lcd; PinEdge<> btn; uint8_t display_mode; uint32_t clock_time; uint8_t clock_mode = 1; // Stop Watch Power on Run uint8_t clock_anim = 8; uint32_t timer_save = (uint32_t)3 * 60 * 1000; uint32_t timer_time; uint8_t timer_mode; uint8_t timer_edit; uint32_t pushed_time; uint32_t timer_start; static const uint16_t EDIT_PLACE[] = {600, 60, 10, 1}; bool coron(uint32_t ms) { return !(ms / 500 & 1); } void Timer(uint32_t ms) { static const uint8_t CURSOR[] = {0, 23, 56, 79}; uint8_t m = ms / 60000 % 100; uint8_t s = ms / 1000 % 60; uint8_t t = ms / 10 % 100; bool c = ((display_mode == CLOCK_MODE ? clock_mode : timer_mode) & 1 ? coron(ms) : true); bool e = ((display_mode == TIMER_MODE) && (timer_mode & 2) ? coron(millis()) : false); lcd.beginPaint(); do { lcd.drawText(0, 0, display_mode == CLOCK_MODE ? "[Stop Watch]" : "[Timer Mode]"); // day lcd.fillRect(0, 16, 16, 8, OPT_INVERT); // minute lcd.drawDigit( 0, 32, m / 10, 2); lcd.drawDigit(23, 32, m % 10, 2); // colon if (c) lcd.drawcColon(46, 32, 1, 2); // second lcd.drawDigit(56, 32, s / 10, 2); lcd.drawDigit(79, 32, s % 10, 2); // milli seconds lcd.drawDigit(102, 40, t / 10, 1); lcd.drawDigit(117, 40, t % 10, 1); // edit cursor lcd.fillRect(0, 58, 100, 4, OPT_INVERT); if (e) lcd.fillRect(CURSOR[timer_edit], 58, 20, 4); } while (!lcd.endPaint()); } void StopWatch(uint32_t ms) { static uint8_t hour, minute, second, interval; uint8_t d = ms / 86400000 % 100; uint8_t h = ms / 3600000 % 24; uint8_t m = ms / 60000 % 60; uint8_t s = ms / 1000 % 60; bool c = (clock_mode & 1 ? coron(ms) : true); // animation if (clock_anim >= 8) { clock_anim = 0; hour = h; minute = m; second = s; } else if (clock_anim) { if ((uint8_t)(ms - interval) >= 33) { interval += 33; ++clock_anim; } } else if ((h != hour) || (m != minute) || (s != second)) { interval = ms; clock_anim = 1; } // lcd.beginPaint(); do { lcd.drawText(0, 0, "[Stop Watch]"); // day lcd.drawDigit(0, 16, d / 10, 0); lcd.drawDigit(8, 16, d % 10, 0); // hour lcd.drawDigitAnimation( 0, 32, hour / 10, h / 10, clock_anim, 2); lcd.drawDigitAnimation(23, 32, hour % 10, h % 10, clock_anim, 2); // colon if (c) lcd.drawcColon(46, 32, 0, 2); // minute lcd.drawDigitAnimation(56, 32, minute / 10, m / 10, clock_anim, 2); lcd.drawDigitAnimation(79, 32, minute % 10, m % 10, clock_anim, 2); // second lcd.drawDigitAnimation(102, 40, second / 10, s / 10, clock_anim, 1); lcd.drawDigitAnimation(117, 40, second % 10, s % 10, clock_anim, 1); // edit cursor lcd.fillRect(0, 58, 100, 4, OPT_INVERT); } while (!lcd.endPaint()); } void setup() { PORTB |= _BV(PB3); // BUTTON1 PULL-UP PORTB |= _BV(PB4); // BUTTON2 PULL-UP btn.init(BUTTON1, HIGH); btn.init(BUTTON2, HIGH); buzzer_init(); buzzer_start(BUZZER_START); bus.begin(); lcd.begin(); timer_time = timer_save; timer_start = millis(); wdt_enable(WDTO_2S); } void loop() { // // Reset/Edit // if (pushed_time) { if (millis() - pushed_time >= 3000) { pushed_time = 0; buzzer_start(BUZZER_CLICK); if (btn.state(BUTTON1) == LOW) { btn.init(BUTTON1, HIGH); if (display_mode == CLOCK_MODE) clock_mode ^= 2; else { timer_time = timer_save; timer_mode = (timer_mode & 2) ^ 2; } } if (btn.state(BUTTON2) == LOW) { btn.init(BUTTON2, HIGH); if (display_mode == CLOCK_MODE) { clock_anim = 8; clock_time = 0; clock_mode &= ~1; } else { timer_time = timer_save; timer_mode = 0; } } } } // // Display Mode // switch (btn.detect(BUTTON1)) { case HIGH: pushed_time = 0; buzzer_start(BUZZER_CLICK); if (display_mode == CLOCK_MODE) display_mode = TIMER_MODE; else if (timer_mode & 2) { uint32_t place = (uint32_t)EDIT_PLACE[timer_edit] * 1000; uint8_t base = timer_edit == 2 ? 6 : 10; uint8_t digit = timer_time / place % base; if (digit < base - 1) timer_time += place; else timer_time -= digit * place; timer_time -= timer_time % 1000; timer_save = timer_time; } else display_mode = CLOCK_MODE; break; case LOW: pushed_time = millis(); break; } // // Start/Stop // switch (btn.detect(BUTTON2)) { case HIGH: pushed_time = 0; buzzer_start(BUZZER_CLICK); if (display_mode == CLOCK_MODE) { clock_mode ^= 1; if (!(clock_mode & 1)) clock_anim = 8; } else if (timer_mode & 2) { if (++timer_edit >= sizeof(EDIT_PLACE)/sizeof(EDIT_PLACE[0])) timer_edit = 0; } else if (timer_time) timer_mode ^= 1; break; case LOW: pushed_time = millis(); break; } // uint32_t t = millis() - timer_start; if (t) { timer_start += t; if (clock_mode & 1) clock_time += t; if (timer_mode & 1) { if (timer_time > t) timer_time -= t; else { timer_time = timer_save; timer_mode = 0; display_mode = TIMER_MODE; buzzer_start(BUZZER_FINISH); } } } // if (display_mode == CLOCK_MODE) { if (clock_mode & 2) Timer(clock_time); else StopWatch(clock_time); } else Timer(timer_time); lcd.flush(); // buzzer_handle(); // wdt_reset(); } int main(void) { timer_init(); sei(); setup(); while (1) loop(); } |
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 |
/* * timer.cpp * * Created: 2018/12/01 * Author: Sasapea's Lab */ #include <avr/io.h> #include <avr/interrupt.h> static volatile uint32_t _micro; static volatile uint32_t _milli; ISR(TIM0_COMPA_vect) { _micro += 1000; _milli += 1; } void timer_init() { GTCCR |= _BV(TSM) | _BV(PSR0); // Timer Stop TCNT0 = 0; OCR0A = 250 - 1; // 1 ms TCCR0B = _BV(CS01) | _BV(CS00); // 1/64 TCCR0A = _BV(WGM01); // CTC Mode TIFR |= _BV(OCF0A); TIMSK |= _BV(OCIE0A); GTCCR &= ~_BV(TSM); // Timer Start } uint32_t micros() { uint32_t c; uint8_t t; do { t = TCNT0; c = _micro; } while (t > TCNT0); return c + ((uint16_t)t << 2); } uint32_t millis() { uint32_t c; uint8_t t; do { t = TCNT0; c = _milli; } while (t > TCNT0); return c; } void delayMicroseconds(uint32_t us) { uint32_t t = micros(); while (micros() - t < us) continue; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* * timer.h * * Created: 2018/12/01 * Author: Sasapea's Lab */ #ifndef TIMER_H_ #define TIMER_H_ void timer_init(); uint32_t micros(); uint32_t millis(); void delayMicroseconds(uint32_t us); #define delay(ms) delayMicroseconds((uint32_t)(ms) * 1000); #endif /* TIMER_H_ */ |
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 |
/* * buzzer.cpp * * Created: 2018/12/01 * Author: Sasapea's Lab */ #include <avr/io.h> #include "timer.h" #include "buzzer.h" static uint16_t _start; static uint16_t _length; void buzzer_init() { PORTB &= ~_BV(PB1); DDRB |= _BV(PB1); TCCR1 = 0; // PWM Stop TCNT1 = 0; OCR1A = 125; // Duty 50% OCR1C = 250 - 1; // 2KHz TCCR1 = _BV(PWM1A) | _BV(CS12) | _BV(CS11); // 1/32 } void buzzer_start(uint16_t ms) { _start = millis(); _length = ms; TCCR1 |= _BV(COM1A1); } void buzzer_stop() { TCCR1 &= ~_BV(COM1A1); PORTB &= ~_BV(PB1); } void buzzer_handle() { if (TCCR1 & _BV(COM1A1)) { if ((uint16_t)millis() - _start >= _length) buzzer_stop(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* * buzzer.h * * Created: 2018/12/01 * Author: Sasapea's Lab */ #ifndef BUZZER_H_ #define BUZZER_H_ void buzzer_init(); void buzzer_start(uint16_t ms); void buzzer_stop(); void buzzer_handle(); #endif /* BUZZER_H_ */ |
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 |
/* * PinEdge.h * * Created: 2018/12/01 * Author: Sasapea's Lab */ #ifndef _PINEDGE_H #define _PINEDGE_H #include <stdint.h> #include <stdbool.h> #include <avr/io.h> #include "timer.h" #define PIN_UNCHANGED 0xFF #define LOW 0 #define HIGH 1 template <uint16_t CHATTERING = 20> class PinEdge { private: uint8_t _flags; uint8_t _state; uint8_t _check; uint16_t _timer; public: PinEdge() : _flags(0) , _state(0) , _check(0) , _timer(0) { } void init(uint8_t mask, uint8_t data, uint8_t inival = PIN_UNCHANGED) { _check &= ~mask; if (data) { _flags |= mask; _state |= mask; } else { _flags &= ~mask; _state &= ~mask; } if (inival == HIGH) _state |= mask; else if (inival == LOW) _state &= ~mask; } uint8_t state(uint8_t mask, uint8_t data) { return _state & mask ? HIGH : LOW; } uint8_t detect(uint8_t mask, uint8_t data) { uint16_t now = (uint16_t)millis(); if ((_flags ^ (data ? mask : 0)) & mask) { _flags ^= mask; _check |= mask; _timer = now; } else if ((_check & mask) && ((now - _timer) >= CHATTERING)) { _check &= ~mask; if ((_state ^ _flags) & mask) return (_state ^= mask) & mask ? HIGH : LOW; } return PIN_UNCHANGED; } }; #endif // _PINEDGE_H |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
/* * USIPORTS.h * * Created: 2018/12/01 * Author: Sasapea's Lab */ #ifndef _USIPORTS_H #define _USIPORTS_H #define USI_PORT_HIGH(p) (USI_PORT(p##_X) |= _BV(p##_N)) #define USI_PORT_LOW(p) (USI_PORT(p##_X) &= ~_BV(p##_N)) #define USI_PORT_INV(p) (USI_PIN(p##_X) |= _BV(p##_N)) #define USI_PORT_IN(p) (USI_PIN(p##_X) & _BV(p##_N)) #define USI_DDR_OUT(p) (USI_DDR(p##_X) |= _BV(p##_N)) #define USI_DDR_IN(p) (USI_DDR(p##_X) &= ~_BV(p##_N)) #define USI_PORT(p) USI_PORT_REG(p) #define USI_PIN(p) USI_PIN_REG(p) #define USI_DDR(p) USI_DDR_REG(p) #define USI_PORT_REG(p) PORT##p #define USI_PIN_REG(p) PIN##p #define USI_DDR_REG(p) DDR##p #define USI_SDA_X USI_DI_X #define USI_SDA_N USI_DI_N #define USI_SCL_X USI_USCK_X #define USI_SCL_N USI_USCK_N #ifndef USI_PIN_POSITION #define USI_PIN_POSITION 0 #endif #if defined(__AVR_ATmega165__ ) || defined(__AVR_ATmega165A__ ) || defined(__AVR_ATmega165P__ ) || defined(__AVR_ATmega165PA__ ) || \ defined(__AVR_ATmega169__ ) || defined(__AVR_ATmega169A__ ) || defined(__AVR_ATmega169P__ ) || defined(__AVR_ATmega169PA__ ) || \ defined(__AVR_ATmega325__ ) || defined(__AVR_ATmega325A__ ) || defined(__AVR_ATmega325P__ ) || defined(__AVR_ATmega325PA__ ) || \ defined(__AVR_ATmega3250__) || defined(__AVR_ATmega3250A__) || defined(__AVR_ATmega3250P__) || defined(__AVR_ATmega3250PA__) || \ defined(__AVR_ATmega329__ ) || defined(__AVR_ATmega329A__ ) || defined(__AVR_ATmega329P__ ) || defined(__AVR_ATmega329PA__ ) || \ defined(__AVR_ATmega3290__) || defined(__AVR_ATmega3290A__) || defined(__AVR_ATmega3290P__) || defined(__AVR_ATmega3290PA__) || \ defined(__AVR_ATmega645__ ) || defined(__AVR_ATmega645A__ ) || defined(__AVR_ATmega645P__ ) || \ defined(__AVR_ATmega6450__) || defined(__AVR_ATmega6450A__) || defined(__AVR_ATmega6450P__) || \ defined(__AVR_ATmega649__ ) || defined(__AVR_ATmega649A__ ) || defined(__AVR_ATmega649P__ ) || \ defined(__AVR_ATmega6490__) || defined(__AVR_ATmega6490A__) || defined(__AVR_ATmega6490P__) #define USI_DI_X E #define USI_DI_N 5 #define USI_DO_X E #define USI_DO_N 6 #define USI_USCK_X E #define USI_USCK_N 4 #define USI_OVF_vect USI_OVERFLOW_vect #elif defined(__AVR_ATtiny2313__) || defined(__AVR_ATtiny2313A__) || defined(__AVR_ATtiny4313__) #define USI_DI_X B #define USI_DI_N 5 #define USI_DO_X B #define USI_DO_N 6 #define USI_USCK_X B #define USI_USCK_N 7 #define USI_OVF_vect USI_OVERFLOW_vect #elif defined(__AVR_ATtiny24A__) || defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__) #define USI_DI_X A #define USI_DI_N 6 #define USI_DO_X A #define USI_DO_N 5 #define USI_USCK_X A #define USI_USCK_N 4 #define USI_START_vect USI_STR_vect #elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) #define USI_DI_X A #define USI_DI_N 6 #define USI_DO_X A #define USI_DO_N 5 #define USI_USCK_X A #define USI_USCK_N 4 #elif defined(__AVR_ATtiny1634__) #define USI_DI_X B #define USI_DI_N 1 #define USI_DO_X B #define USI_DO_N 2 #define USI_USCK_X C #define USI_USCK_N 1 #elif defined(__AVR_ATtiny43U__) #define USI_DI_X B #define USI_DI_N 4 #define USI_DO_X B #define USI_DO_N 5 #define USI_USCK_X B #define USI_USCK_N 6 #elif defined(__AVR_ATtiny26__) #define USI_DI_X B #define USI_DI_N 0 #define USI_DO_X B #define USI_DO_N 1 #define USI_USCK_X B #define USI_USCK_N 2 #define USI_START_vect USI_STRT_vect #elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) #define USI_DI_X B #define USI_DI_N 0 #define USI_DO_X B #define USI_DO_N 1 #define USI_USCK_X B #define USI_USCK_N 2 #elif defined(__AVR_ATtiny261__) || defined(__AVR_ATtiny461__) || defined(__AVR_ATtiny861__) #if (USI_PIN_POSITION == 0) #define USI_DI_X B #define USI_DI_N 0 #define USI_DO_X B #define USI_DO_N 1 #define USI_USCK_X B #define USI_USCK_N 2 #else #define USI_DI_X A #define USI_DI_N 0 #define USI_DO_X A #define USI_DO_N 1 #define USI_USCK_X A #define USI_USCK_N 2 #endif #elif defined(__AVR_ATtiny87__ ) || defined(__AVR_ATtiny167__) || \ defined(__AVR_ATA6616C__ ) || defined(__AVR_ATA6617C__ ) || defined(__AVR_ATA664251__) #if (USI_PIN_POSITION == 0) #define USI_DI_X B #define USI_DI_N 0 #define USI_DO_X B #define USI_DO_N 1 #define USI_USCK_X B #define USI_USCK_N 2 #else #define USI_DI_X A #define USI_DI_N 4 #define USI_DO_X A #define USI_DO_N 2 #define USI_USCK_X A #define USI_USCK_N 5 #endif #endif #endif //_USIPORTS_H |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
/* * USITWI.h * * Created: 2018/12/01 * Author: Sasapea's Lab */ #ifndef _USITWI_H #define _USITWI_H #include <stdint.h> #include <stdbool.h> #include <util/delay_basic.h> #include <avr/io.h> #include "USIPORTS.h" #define USITWI_100K 0 #define USITWI_400K 1 #define USITWI_SCL_WAIT() while (!USI_PORT_IN(USI_SCL)) #define USITWI_CLK_LOW() USICR |= _BV(USITC) | _BV(USICLK) #define USITWI_CLK_HIGH() USICR |= _BV(USITC) #define USITWI_CLR_IRQ() USISR = _BV(USISIF) | _BV(USIOIF) | _BV(USIPF) #define USITWI_DELAY(t) do { if (t) _delay_loop_1((uint8_t)(((t) * (F_CPU / 1000000) * 256 + 767) / 768)); } while (0) #define USITWI_DELAY_THDSTA(m) USITWI_DELAY((m) ? 0.4 : 4.0) // 0.6/4.0 #define USITWI_DELAY_TLOW(m) USITWI_DELAY((m) ? 1.0 : 4.7) // 1.3/4.7 #define USITWI_DELAY_THIGH(m) USITWI_DELAY((m) ? 0.0 : 4.0) // 0.6/4.0 #define USITWI_DELAY_TSUSTA(m) USITWI_DELAY((m) ? 0.0 : 4.7) // 0.6/4.7 #define USITWI_DELAY_TSUSTO(m) USITWI_DELAY((m) ? 0.0 : 4.0) // 0.6/4.0 #define USITWI_DELAY_TSUDAT(m) USITWI_DELAY((m) ? 0.1 : 0.25) // 0.1/0.25 #define USITWI_DELAY_TBUF(m) USITWI_DELAY((m) ? 1.0 : 4.7) // 1.3/4.7 typedef void (*USITWI_CALLBACK)(uint8_t *buf, uint8_t len); extern volatile bool USITWI_usisif; extern void USITWI_init(uint8_t addr, uint8_t *buf, uint8_t len); extern void USITWI_setCallback(USITWI_CALLBACK func); extern void USITWI_setReadBytes(uint8_t *buf, uint8_t len); extern void USITWI_handle(); template<uint8_t TWI_CLK = USITWI_400K, uint8_t ADDR = 0x00, uint8_t SIZE = 16> class USITWI { private: uint8_t _buffer[SIZE]; bool _usidc; protected: void start() { _usidc = false; // // wait bus free // do { handle(); USICR &= ~_BV(USISIE); } while (USITWI_usisif || !USI_PORT_IN(USI_SDA) || !USI_PORT_IN(USI_SCL)); // // start confition // USITWI_usisif = true; USIDR = 0x00; // SDA LOW USITWI_DELAY_THDSTA(TWI_CLK); USI_PORT_LOW(USI_SCL); USICR |= _BV(USICS1); USITWI_CLR_IRQ(); if (TWI_CLK == USITWI_100K) USITWI_DELAY_TLOW(TWI_CLK); } void stop(bool restart) { USICR &= ~_BV(USICS1); // // setup for repeated start condition // if (restart) { USIDR = 0xFF; // SDA HIGH; USITWI_DELAY_TSUDAT(TWI_CLK); USI_PORT_HIGH(USI_SCL); USITWI_SCL_WAIT(); USITWI_DELAY_TSUSTA(TWI_CLK); } // // stop condition // else { USIDR = 0x00; // SDA LOW USITWI_DELAY_TSUDAT(TWI_CLK); USI_PORT_HIGH(USI_SCL); USITWI_SCL_WAIT(); USITWI_DELAY_TSUSTO(TWI_CLK); USIDR = 0xFF; // SDA HIGH; USITWI_DELAY_TBUF(TWI_CLK); } USITWI_usisif = false; USICR |= _BV(USISIE); } uint8_t transfer(uint8_t data, uint8_t dc) { USIDR = data; USISR = _BV(USIOIF); while (1) { USITWI_CLK_HIGH(); USITWI_SCL_WAIT(); // // data collision ? // if (USISR & dc) { _usidc = true; USICR &= ~_BV(USICS1); USIDR = 0xFF; break; } USITWI_DELAY_THIGH(TWI_CLK); USITWI_CLK_LOW(); if (TWI_CLK == USITWI_100K) { USITWI_DELAY_TLOW(TWI_CLK); if (USISR & _BV(USIOIF)) break; } else { if (USISR & _BV(USIOIF)) break; USITWI_DELAY_TLOW(TWI_CLK); } } USISR = _BV(USIOIF); return USIDR; } bool ack() { USI_PORT_HIGH(USI_SCL); USITWI_SCL_WAIT(); USITWI_DELAY_THIGH(TWI_CLK); USI_PORT_LOW(USI_SCL); if (TWI_CLK == USITWI_100K) USITWI_DELAY_TLOW(TWI_CLK); return !(USIDR & 0x01); } public: USITWI() : _usidc(0) { } void begin() { USITWI_init(ADDR, _buffer, sizeof(_buffer)); USITWI_CLR_IRQ(); USIDR = 0xFF; USICR = _BV(USISIE) | _BV(USIWM1) | _BV(USIWM0); USI_PORT_HIGH(USI_SDA); USI_PORT_HIGH(USI_SCL); USI_DDR_OUT(USI_SDA); USI_DDR_OUT(USI_SCL); } void end() { USI_DDR_IN(USI_SDA); USI_DDR_IN(USI_SCL); USI_PORT_LOW(USI_SDA); USI_PORT_LOW(USI_SCL); USICR = 0; } uint8_t writeAndRead(uint8_t *wbuf, uint8_t wlen, uint8_t *rbuf, uint8_t rlen, uint8_t addr) { return write(wbuf, wlen, addr, true) == wlen ? read(rbuf, rlen, addr) : 0; } uint8_t write(const uint8_t *buf, uint8_t len, uint8_t addr, bool restart = false) { uint8_t cnt; addr <<= 1; do { cnt = 0; // // start condition // start(); // // transmit slave address // transfer(addr, _BV(USIDC)); // // loop // while (!_usidc) { // // receive ack // USIDR = 0xFF; // // ack // if (!ack()) break; // // end of transfer ? // if (cnt >= len) break; // // transmit data // transfer(buf[cnt++], _BV(USIDC)); } } while (_usidc); // // stop or repeated start condition // stop(restart && (cnt == len)); return cnt; } uint8_t read(uint8_t *buf, uint8_t len, uint8_t addr, bool restart = false) { uint8_t cnt = 0; if (len) { addr = (addr << 1) | 0x01; do { cnt = 0; // // start condition // start(); // // transmit slave address // transfer(addr, _BV(USIDC)); // // loop // while (!_usidc) { // // transmit ack // USIDR = ((cnt > 0) && (cnt < len) ? 0x7F : 0xFF); // // ack // if (!ack()) break; // // ack collision ? // if (cnt >= len) { _usidc = true; USICR &= ~_BV(USICS1); USIDR = 0xFF; break; } // // receive data // buf[cnt++] = transfer(0xFF, 0); } } while (_usidc); // // stop or repeated start condition // stop(restart && (cnt == len)); } return cnt; } void handle() { return USITWI_handle(); } void setCallback(USITWI_CALLBACK func) { USITWI_setCallback(func); } void setReadBytes(uint8_t *buf, uint8_t len) { USITWI_setReadBytes(buf, len); } }; #endif // _USITWI_H |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 |
/* * USITWI.cpp * * Created: 2018/12/01 * Author: Sasapea's Lab */ #include <avr/interrupt.h> #include "USITWI.h" #define END() do { USICR &= ~(_BV(USICS1) | _BV(USIOIE)); USIDR = 0xFF; return 0x00; } while (0) typedef uint8_t (*USITWI_DISPATCH)(); volatile bool USITWI_usisif; static uint8_t _myaddr; static uint8_t *_write_buffer; static uint8_t _write_length; static uint8_t _write_count; static uint8_t *_read_buffer; static uint8_t _read_length; static uint8_t _read_count; static USITWI_CALLBACK _write_cb; static USITWI_DISPATCH _dispatch; static uint8_t read_next(); static uint8_t write_next(); static uint8_t read_start() { if (USIDR & 0x01) END(); _dispatch = read_next; USIDR = _read_buffer[_read_count]; return 0x00; } static uint8_t read_next() { if (++_read_count >= _read_length) END(); _dispatch = read_start; USIDR = 0xFF; return 0x0E; } static uint8_t write_start() { _dispatch = write_next; USIDR = 0xFF; return 0x00; } static uint8_t write_next() { _write_buffer[_write_count] = USIDR; if (++_write_count >= _write_length) END(); _dispatch = write_start; USIDR = 0x7F; return 0x0E; } static void callback() { if (_write_count) { _read_length = 0; if (_write_cb) (*_write_cb)(_write_buffer, _write_count); _write_count = 0; } } static uint8_t slave_address() { uint8_t addr = USIDR; if ((addr == 0) || (_myaddr == (addr & ~0x01))) { callback(); if (addr & 0x01) { if (_read_length != 0) { _dispatch = read_start; USIDR = 0x7F; return 0x0E; } } else { if (_write_length != 0) { _dispatch = write_start; USIDR = 0x7F; return 0x0E; } } } END(); } ISR(USI_OVF_vect) { uint8_t cnt = (*_dispatch)(); USITWI_DELAY_TSUDAT(USITWI_100K); USISR = _BV(USIOIF) | cnt; } ISR(USI_START_vect) { USITWI_usisif = true; _dispatch = slave_address; while (USI_PORT_IN(USI_SCL)) ; USIDR = 0xFF; USICR |= (_BV(USICS1) | _BV(USIOIE)); USITWI_CLR_IRQ(); } void USITWI_handle() { USICR &= ~_BV(USISIE); if (USITWI_usisif && (USISR & _BV(USIPF))) { USITWI_usisif = false; USICR &= ~(_BV(USICS1) | _BV(USIOIE)); USIDR = 0xFF; callback(); } USICR |= _BV(USISIE); } void USITWI_init(uint8_t addr, uint8_t *buf, uint8_t len) { _myaddr = addr << 1; _write_buffer = buf; _write_length = len; _write_count = 0; } void USITWI_setCallback(USITWI_CALLBACK func) { _write_cb = func; } void USITWI_setReadBytes(uint8_t *buf, uint8_t len) { _read_buffer = buf; _read_length = len; _read_count = 0; } |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
/* * SSD1306.h * * Created: 2018/12/01 * Author: Sasapea's Lab */ #ifndef _SSD1306_H #define _SSD1306_H #include <stdint.h> #include <stdbool.h> #include <string.h> #include <avr/io.h> #include <avr/pgmspace.h> #include <avr/eeprom.h> #include "timer.h" #define OPT_DATA_PTR 0 #define OPT_CODE_PTR 1 #define OPT_EROM_PTR 2 #define OPT_FONT_PTR 3 #define OPT_INVERT 0x80 #define OPT_FONT_LARGE_DIGIT 1 #define OPT_FONT_MEDIUM_DIGIT 1 #define OPT_FONT_LOWER_CASE 1 #define OPT_FONT_EEPROM_SIZE 512 #define FONT_START_CHAR 0x20 #define FONT_END_CHAR 0x7E #define SSD1306_MAX_WIDTH 128 #define SSD1306_MAX_HEIGHT 64 #define SSD1306_REPT 0x00 #define SSD1306_WORD 0x80 #define SSD1306_DATA 0x40 //+-------------------+--------+ //| SSD1306 | SH1106 | //+-------------------+--------+ //#define SH1106_SET_PUMP_VOLTAGE_VALUE 0x30 //| (none) | | //#define SH1106_DC_DC_OFF 0x8A //| (none) | | //#define SH1106_DC_DC_ON 0x8B //| (none) | | //#define SH1106_DC_DC_CONTROL_MODE_SET 0xAD //| (none) | | //#define SH1106_READ_MODIFY_WRITE_START 0xE0 //| (none) | | //#define SH1106_READ_MODIFY_WRITE_END 0xEE //| (none) | | //+-------------------+--------+ #define SSD1306_SET_LOWER_COLUMN_START_ADDRESS 0x00 //| | | #define SSD1306_SET_HIGHER_COLUMN_START_ADDRESS 0x10 //| | | #define SSD1306_SET_MEMORY_ADDRESSING_MODE 0x20 //| 02 | (none) | #define SSD1306_SET_COLUMN_ADDRESS 0x21 //| 00:7F | (none) | #define SSD1306_SET_PAGE_ADDRESS 0x22 //| 00:07 | (none) | #define SSD1306_RIGHT_SCROLL 0x26 //| 00:07:07:07:00:FF | (none) | #define SSD1306_LEFT_SCROLL 0x27 //| 00:07:07:07:00:FF | (none) | #define SSD1306_VERTICAL_AND_RIGHT_SCROLL 0x29 //| 00:07:07:07:00 | (none) | #define SSD1306_VERTICAL_AND_LEFT_SCROLL 0x2A //| 00:07:07:07:00 | (none) | #define SSD1306_DEACTIVATE_SCROLL 0x2E //| | (none) | #define SSD1306_ACTIVATE_SCROLL 0x2F //| | (none) | #define SSD1306_SET_DISPLAY_START_LINE 0x40 //| | | #define SSD1306_SET_CONTRAST_CONTROL 0x81 //| 7F | FF | #define SSD1306_CHARGE_PUMP_SETTING 0x8D //| 10 | (none) | #define SSD1306_SET_SEGMENT_NORMAL 0xA0 //| | | #define SSD1306_SET_SEGMENT_REMAP 0xA1 //| | | #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 //| 3F:7F | (none) | #define SSD1306_ENTIRE_DISPLAY_OFF 0xA4 //| | | #define SSD1306_ENTIRE_DISPLAY_ON 0xA5 //| | | #define SSD1306_SET_NORMAL_DISPLAY 0xA6 //| | | #define SSD1306_SET_INVERSE_DISPLAY 0xA7 //| | | #define SSD1306_SET_MULTIPLEX_RATIO 0xA8 //| | | #define SSD1306_SET_DISPLAY_OFF 0xAE //| | | #define SSD1306_SET_DISPLAY_ON 0xAF //| | | #define SSD1306_SET_PAGE_START_ADDRESS 0xB0 //| B0-B7 | B0-BF | #define SSD1306_SET_COM_OUTPUT_SCAN_NORMAL 0xC0 //| | | #define SSD1306_SET_COM_OUTPUT_SCAN_REMAP 0xC8 //| | | #define SSD1306_SET_DISPLAY_OFFSET 0xD3 //| | | #define SSD1306_SET_CLOCK_DIVIDE_OSCILLATOR 0xD5 //| 80 | 50 | #define SSD1306_SET_PRECHARGE_PERIOD 0xD9 //| | | #define SSD1306_SET_COM_PINS_HARDWARE_CONFIGURATION 0xDA //| | | #define SSD1306_SET_VCOMH_DESELECT_LEVEL 0xDB //| 20 | 35 | #define SSD1306_NOP 0xE3 //| | | #define SSD1306_GDDRAM 0xFF //| | | //+-------------------+--------+ extern PROGMEM const uint8_t SSD1306_FONT[][8]; #if OPT_FONT_LARGE_DIGIT extern PROGMEM const uint8_t FONT_LARGE_DIGIT[][19 * 3]; extern PROGMEM const uint8_t FONT_LARGE_COLON[][6 * 3]; #endif #if OPT_FONT_MEDIUM_DIGIT extern PROGMEM const uint8_t FONT_MEDIUM_DIGIT[][11 * 2]; #endif template<typename IOBUS_T, IOBUS_T& IOBUS, uint8_t ADDR = 0xFF, uint8_t SIZE = 32> class SSD1306 { private: typedef struct { int left, top, right, bottom; } RECT; typedef struct { int x, y; } POINT; uint8_t _g_buf[32]; RECT _g_cur; POINT _g_org; bool _g_wrt; uint8_t _buffer[SIZE]; uint8_t _count; uint8_t _ctrlb; uint8_t _lines; uint8_t _y_off; uint8_t _y_pos; uint8_t _x_pos; protected: void ctrl(uint8_t cmd, uint8_t len) { if (cmd != SSD1306_GDDRAM) { if ((_ctrlb != SSD1306_WORD) || (_count + len + 2 >= (uint8_t)sizeof(_buffer))) flush(); _buffer[_count++] = _ctrlb = (len ? SSD1306_REPT : SSD1306_WORD); _buffer[_count++] = cmd; } else if ((_count == 0) || (_ctrlb != SSD1306_DATA)) { if ((_ctrlb != SSD1306_WORD) || (_count + len + 1 >= (uint8_t)sizeof(_buffer))) flush(); _buffer[_count++] = _ctrlb = SSD1306_DATA; } } void data(uint8_t val) { if (_count == 0) _buffer[_count++] = _ctrlb; _buffer[_count] = val; if (++_count >= sizeof(_buffer)) flush(); } uint8_t read_byte(const uint8_t *address, uint8_t opt) { uint8_t rv; switch (opt & ~OPT_INVERT) { default: rv = *address; break; case OPT_CODE_PTR: rv = pgm_read_byte_near(address); break; case OPT_EROM_PTR: rv = eeprom_read_byte(address); break; #if OPT_FONT_EEPROM_SIZE case OPT_FONT_PTR: if (address >= SSD1306_FONT[0] + OPT_FONT_EEPROM_SIZE) rv = pgm_read_byte_near(address - OPT_FONT_EEPROM_SIZE); else rv = eeprom_read_byte((const uint8_t *)(address - SSD1306_FONT[0])); break; #endif } return (opt & OPT_INVERT ? ~rv : rv); } uint8_t *glyph(uint8_t c) { if ((c < FONT_START_CHAR) || (c > FONT_END_CHAR)) c = 0; else { #if OPT_FONT_LOWER_CASE c -= FONT_START_CHAR; #else if ((c >= 'a') && (c <= 'z')) c -= FONT_START_CHAR + 'a' - 'A'; else if (c > 'z') c -= FONT_START_CHAR + 'z' - 'a' + 1; else c -= FONT_START_CHAR; #endif } return (uint8_t *)SSD1306_FONT[c]; } void charge_pump(bool enable) { ctrl(SSD1306_CHARGE_PUMP_SETTING, 1); data(enable ? 0x14 : 0x10); } void multiplex(uint8_t nlines) { if ((nlines >= 2) && (nlines <= (SSD1306_MAX_HEIGHT >> 3)) && (nlines != _lines)) { _lines = nlines; ctrl(SSD1306_SET_MULTIPLEX_RATIO, 1); data((nlines << 3) - 1); } } void address(uint8_t x, uint8_t y) { // // set column start address // ctrl(SSD1306_SET_LOWER_COLUMN_START_ADDRESS | ((x >> 0) & 0x0F), 0); ctrl(SSD1306_SET_HIGHER_COLUMN_START_ADDRESS | ((x >> 4) & 0x0F), 0); // // set page start address // ctrl(SSD1306_SET_PAGE_START_ADDRESS | ((y + _y_off) & 0x07), 0); } bool ptInVisible(int x, int y) { return (x >= _g_cur.left) && (x < _g_cur.right) && (y >= _g_cur.top) && (y < _g_cur.bottom); } bool hasIntersectClipRect(int x0, int y0, int x1, int y1) { return (x0 < x1 ? (x0 >= _g_cur.right ) || (x1 <= _g_cur.left) : (x0 < _g_cur.left) || (x1 >= _g_cur.right )) || (y0 < y1 ? (y0 >= _g_cur.bottom) || (y1 <= _g_cur.top ) : (y0 < _g_cur.top ) || (y1 >= _g_cur.bottom)) ? false : true; } bool intersectClipRect(RECT *rc) { if ((rc->left >= rc->right) || (rc->top >= rc->bottom)) return false; if ((rc->left >= _g_cur.right) || (rc->right <= _g_cur.left)) return false; if ((rc->top >= _g_cur.bottom) || (rc->bottom <= _g_cur.top)) return false; if (rc->left < _g_cur.left) rc->left = _g_cur.left; if (rc->top < _g_cur.top) rc->top = _g_cur.top; if (rc->right > _g_cur.right) rc->right = _g_cur.right; if (rc->bottom > _g_cur.bottom) rc->bottom = _g_cur.bottom; return true; } public: SSD1306() : _ctrlb(SSD1306_WORD) , _lines(SSD1306_MAX_HEIGHT >> 3) , _y_off(0) , _y_pos(0) , _x_pos(0) { } void begin(uint8_t nlines = 8) { // // enable charge pump // charge_pump(true); // // set multiplex // multiplex(nlines); // // display clear // clear(); // // display on // display(true); // // flush // flush(); // // delay for SH1106 // delay(150); } void flush() { if (_count) { IOBUS.write(_buffer, _count, ADDR); _count = 0; } } uint8_t status() { uint8_t rv = 0xFF; IOBUS.read(&rv, sizeof(rv), ADDR); return rv; } void clear() { for (uint8_t y = 0; y < _lines; ++y) { cursor(0, y); ctrl(SSD1306_GDDRAM, 1); for (uint8_t x = 0; x < SSD1306_MAX_WIDTH; ++x) data(0); } cursor(0, 0); } void display(bool enable) { ctrl(enable ? SSD1306_SET_DISPLAY_ON : SSD1306_SET_DISPLAY_OFF, 0); } void flip(bool enable) { ctrl(enable ? SSD1306_SET_COM_OUTPUT_SCAN_REMAP : SSD1306_SET_COM_OUTPUT_SCAN_NORMAL, 0); ctrl(enable ? SSD1306_SET_SEGMENT_REMAP : SSD1306_SET_SEGMENT_NORMAL, 0); } void white(bool enable) { ctrl(enable ? SSD1306_ENTIRE_DISPLAY_ON : SSD1306_ENTIRE_DISPLAY_OFF, 0); } void invert(bool enable) { ctrl(enable ? SSD1306_SET_INVERSE_DISPLAY : SSD1306_SET_NORMAL_DISPLAY, 0); } void contrast(uint8_t val) { ctrl(SSD1306_SET_CONTRAST_CONTROL, 1); data(val); } void cursor(uint8_t x, uint8_t y) { if ((x < (SSD1306_MAX_WIDTH >> 3)) && (y < _lines)) { _x_pos = x; _y_pos = y; address(x << 3, y); } } void scroll() { _y_off = (_y_off + 1) & 0x07; ctrl(SSD1306_SET_DISPLAY_START_LINE | (_y_off << 3), 0); address(0, _lines - 1); ctrl(SSD1306_GDDRAM, 1); for (uint8_t x = 0; x < SSD1306_MAX_WIDTH; ++x) data(0); } void newline() { _x_pos = 0; if (_y_pos + 1 < _lines) ++_y_pos; else scroll(); cursor(_x_pos, _y_pos); } void putChar(char c) { switch (c) { // // back space // case '\b': if (_x_pos) cursor(--_x_pos, _y_pos); break; // // caridge return // case '\r': if (_x_pos) cursor(_x_pos = 0, _y_pos); break; // // line feed // case '\n': newline(); break; // // character // default: if ((c >= FONT_START_CHAR) && (c <= FONT_END_CHAR)) { ctrl(SSD1306_GDDRAM, 1); const uint8_t *p = glyph(c); for (uint8_t i = 0; i < 8; ++i) data(read_byte(p++, OPT_FONT_PTR)); if (++_x_pos >= (SSD1306_MAX_WIDTH >> 3)) newline(); } break; } } void printStr(const char *str) { char c; while ((c = *str++) != 0) putChar(c); } template <typename T> bool printInt(T val) { char buf[12]; int8_t len = sizeof(buf); int8_t neg = (val < 0); buf[--len] = 0; if (neg) val = -val; do { if (--len < neg) return false; buf[len] = (char)(val % 10) | '0'; } while ((val /= 10) >= 1); if (neg) buf[--len] = '-'; printStr(&buf[len]); return true; } void beginPaint() { _g_cur.left = 0; _g_cur.right = sizeof(_g_buf); _g_cur.top = 0; _g_cur.bottom = 8; _g_wrt = false; memset(_g_buf, 0, sizeof(_g_buf)); } bool endPaint() { // // flush page // if (_g_wrt) { address(_g_cur.left, _g_cur.top >> 3); ctrl(SSD1306_GDDRAM, 1); uint8_t *p = _g_buf; for (int8_t i = sizeof(_g_buf); i; --i) data(*p++); _g_wrt = false; memset(_g_buf, 0, sizeof(_g_buf)); } // // select next page // if (_g_cur.right < SSD1306_MAX_WIDTH) { _g_cur.left += sizeof(_g_buf); _g_cur.right += sizeof(_g_buf); } else { if (_g_cur.bottom >= (_lines << 3)) { address(_x_pos << 3, _y_pos); return true; } _g_cur.left = 0; _g_cur.right = sizeof(_g_buf); _g_cur.top += 8; _g_cur.bottom += 8; } return false; } bool pixel(int x, int y) { if (ptInVisible(x, y)) { _g_buf[x - _g_cur.left] |= _BV(y & 7); return _g_wrt = true; } return false; } void moveto(int x, int y) { _g_org.x = x; _g_org.y = y; } void lineto(int x, int y) { int x0 = _g_org.x; int y0 = _g_org.y; int dx = x >= x0 ? x - x0 : x0 - x; int dy = y >= y0 ? y - y0 : y0 - y; int sx = (x0 < x ? 1 : -1); int sy = (y0 < y ? 1 : -1); int er = dx - dy; while (dx >= dy ? x0 != x : y0 != y) { if (!pixel(x0, y0)) { if (!hasIntersectClipRect(x0, y0, x, y)) break; } int e2 = er + er; if (e2 > -dy) { er -= dy; x0 += sx; } if (e2 < dx) { er += dx; y0 += sy; } } _g_org.x = x; _g_org.y = y; } void circle(int x, int y, int radius) { RECT rc = {x - radius + 1, y - radius + 1, x + radius, y + radius}; if (intersectClipRect(&rc)) { int x0 = radius - 1; int y0 = 0; int dx = 1; int dy = 1; radius <<= 1; int er = dx - radius + 4; // 4 = Correction value for small circle while (x0 >= y0) { pixel(x + x0, y + y0); pixel(x + y0, y + x0); pixel(x - y0, y + x0); pixel(x - x0, y + y0); pixel(x - x0, y - y0); pixel(x - y0, y - x0); pixel(x + y0, y - x0); pixel(x + x0, y - y0); if (er <= 0) { er += dy; dy += 2; ++y0; } if (er > 0) { dx += 2; er += dx - radius; --x0; } } } } void rect(int x, int y, int w, int h) { if ((w > 0) && (h > 0)) { moveto(x, y); if (w == 1) lineto(x, y + h); else if (h == 1) lineto(x + w, y); else { --w; --h; lineto(x + w, y); lineto(x + w, y + h); lineto(x, y + h); lineto(x, y); } } } void fillRect(int x, int y, int w, int h, uint8_t opt = 0) { RECT rc = {x, y, x + w, y + h}; if (intersectClipRect(&rc)) { uint8_t fill = ~(_BV(rc.top & 7) - 1); if (rc.bottom & 7) fill &= (_BV(rc.bottom & 7) - 1); do { if (opt & OPT_INVERT) _g_buf[rc.left - _g_cur.left] &= ~fill; else _g_buf[rc.left - _g_cur.left] |= fill; } while (++rc.left < rc.right); _g_wrt = true; } } void drawImage(int x, int y, int w, int h, uint8_t *image, uint8_t opt = OPT_DATA_PTR, uint8_t row = 0) { RECT rc = {x, y, x + w, y + h}; if (intersectClipRect(&rc)) { y -= row; uint8_t b_off = rc.left - _g_cur.left; uint8_t i_off = rc.left - x + ((rc.top - y) >> 3) * w; uint8_t r_off = y & 7; uint8_t mask = (_BV(rc.top & 7) - 1); if (rc.bottom & 7) mask |= ~(_BV(rc.bottom & 7) - 1); do { uint8_t b = read_byte(&image[i_off], opt); if (r_off) { if (y == rc.top) b <<= r_off; else { b >>= 8 - r_off; if ((rc.bottom & 7) == 0) b |= read_byte(&image[i_off + w], opt) << r_off; } } _g_buf[b_off] |= b & ~mask; ++b_off; ++i_off; } while (++rc.left < rc.right); _g_wrt = true; } } void drawDigitAnimation(int x, int y, uint8_t num1, uint8_t num2, uint8_t anime, uint8_t size, uint8_t opt = 0) { uint8_t w, h, *f, *n; if ((num1 < 10) && (num2 < 10)) { opt &= OPT_INVERT; switch (size) { default: w = 8; h = 8; f = (uint8_t *)SSD1306_FONT[num1 + ('0' - FONT_START_CHAR)]; n = (uint8_t *)SSD1306_FONT[num2 + ('0' - FONT_START_CHAR)]; opt |= OPT_FONT_PTR; break; #if OPT_FONT_MEDIUM_DIGIT case 1: w = 11; h = 16; f = (uint8_t *)FONT_MEDIUM_DIGIT[num1]; n = (uint8_t *)FONT_MEDIUM_DIGIT[num2]; anime += anime; opt |= OPT_CODE_PTR; break; #endif #if OPT_FONT_LARGE_DIGIT case 2: w = 19; h = 24; f = (uint8_t *)FONT_LARGE_DIGIT[num1]; n = (uint8_t *)FONT_LARGE_DIGIT[num2]; anime += anime + anime; opt |= OPT_CODE_PTR; break; #endif } if ((num1 == num2) || (anime == 0)) drawImage(x, y, w, h, f, opt); else { h -= anime; drawImage(x, y, w, anime, n, opt, h); drawImage(x, y + anime, w, h, f, opt); } } } void drawDigit(int x, int y, uint8_t num, uint8_t size, uint8_t opt = 0) { uint8_t w, h, *f; if (num < 10) { opt &= OPT_INVERT; switch (size) { default: w = 8; h = 8; f = (uint8_t *)SSD1306_FONT[num + ('0' - FONT_START_CHAR)]; opt |= OPT_FONT_PTR; break; #if OPT_FONT_MEDIUM_DIGIT case 1: w = 11; h = 16; f = (uint8_t *)FONT_MEDIUM_DIGIT[num]; opt |= OPT_CODE_PTR; break; #endif #if OPT_FONT_LARGE_DIGIT case 2: w = 19; h = 24; f = (uint8_t *)FONT_LARGE_DIGIT[num]; opt |= OPT_CODE_PTR; break; #endif } drawImage(x, y, w, h, f, opt); } } void drawcColon(int x, int y, uint8_t c, uint8_t size, uint8_t opt = 0) { uint8_t w, h, *f; if (c <= 1) { opt &= OPT_INVERT; switch (size) { default: w = 8; h = 8; f = (uint8_t *)SSD1306_FONT[(c ? '.' : ':') - FONT_START_CHAR]; opt |= OPT_FONT_PTR; break; #if OPT_FONT_LARGE_DIGIT case 2: w = 6; h = 24; f = (uint8_t *)FONT_LARGE_COLON[c]; opt |= OPT_CODE_PTR; break; #endif } drawImage(x, y, w, h, f, opt); } } void drawText(int x, int y, const char *str, uint8_t opt = 0) { RECT rc = {x, y, x + (int)(strlen(str) << 3), y + 8}; if (intersectClipRect(&rc)) { uint8_t b_off = rc.left - _g_cur.left; uint8_t i_off = rc.left - x; uint8_t r_off = y & 7; uint8_t mask = _BV(rc.top & 7) - 1; if (rc.bottom & 7) mask |= ~(_BV(rc.bottom & 7) - 1); opt = (opt & OPT_INVERT) | OPT_FONT_PTR; do { uint8_t b = str[i_off >> 3]; b = read_byte(glyph(b) + (i_off & 7), opt); if (r_off) { if (y == rc.top) b <<= r_off; else b >>= 8 - r_off; } _g_buf[b_off] |= b & ~mask; ++b_off; ++i_off; } while (++rc.left < rc.right); _g_wrt = true; } } }; #endif // _SSD1306_H |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
/* * SSD1306.cpp * * Created: 2018/12/01 * Author: Sasapea's Lab */ #include "SSD1306.h" PROGMEM const uint8_t SSD1306_FONT[][8] = { #if !OPT_FONT_EEPROM_SIZE {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* */ {0x00,0x00,0x00,0x5f,0x00,0x00,0x00,0x00}, /* ! */ {0x06,0x03,0x01,0x06,0x03,0x01,0x00,0x00}, /* ″ */ {0x20,0x62,0x3e,0x63,0x3e,0x23,0x02,0x00}, /* # */ {0x00,0x24,0x2a,0x7a,0x2f,0x2a,0x12,0x00}, /* $ */ {0x42,0x25,0x12,0x08,0x24,0x52,0x21,0x00}, /* % */ {0x20,0x56,0x49,0x55,0x22,0x58,0x40,0x00}, /* & */ {0x06,0x03,0x01,0x00,0x00,0x00,0x00,0x00}, /* ′ */ {0x00,0x00,0x00,0x00,0x1c,0x22,0x41,0x00}, /* ( */ {0x41,0x22,0x1c,0x00,0x00,0x00,0x00,0x00}, /* ) */ {0x00,0x22,0x14,0x7f,0x14,0x22,0x00,0x00}, /* * */ {0x08,0x08,0x08,0x7f,0x08,0x08,0x08,0x00}, /* + */ {0x50,0x30,0x00,0x00,0x00,0x00,0x00,0x00}, /* , */ {0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00}, /* - */ {0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00}, /* . */ {0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00}, /* / */ {0x00,0x3e,0x41,0x41,0x41,0x41,0x3e,0x00}, /* 0 */ {0x00,0x00,0x42,0x7f,0x40,0x00,0x00,0x00}, /* 1 */ {0x00,0x62,0x51,0x51,0x49,0x49,0x46,0x00}, /* 2 */ {0x00,0x22,0x41,0x49,0x49,0x49,0x36,0x00}, /* 3 */ {0x00,0x30,0x28,0x24,0x22,0x7f,0x20,0x00}, /* 4 */ {0x00,0x2f,0x45,0x45,0x45,0x45,0x39,0x00}, /* 5 */ {0x00,0x3e,0x49,0x49,0x49,0x49,0x32,0x00}, /* 6 */ {0x00,0x01,0x01,0x61,0x19,0x05,0x03,0x00}, /* 7 */ {0x00,0x36,0x49,0x49,0x49,0x49,0x36,0x00}, /* 8 */ {0x00,0x26,0x49,0x49,0x49,0x49,0x3e,0x00}, /* 9 */ {0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00}, /* : */ {0x00,0x00,0x56,0x36,0x00,0x00,0x00,0x00}, /* ; */ {0x00,0x00,0x00,0x08,0x14,0x22,0x41,0x00}, /* 〈 */ {0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x00}, /* = */ {0x41,0x22,0x14,0x08,0x00,0x00,0x00,0x00}, /* 〉 */ {0x00,0x02,0x01,0x51,0x09,0x09,0x06,0x00}, /* ? */ {0x1c,0x22,0x59,0x55,0x4d,0x12,0x0c,0x00}, /* @ */ {0x60,0x18,0x16,0x11,0x16,0x18,0x60,0x00}, /* A */ {0x00,0x7f,0x49,0x49,0x49,0x49,0x36,0x00}, /* B */ {0x00,0x1c,0x22,0x41,0x41,0x41,0x22,0x00}, /* C */ {0x00,0x7f,0x41,0x41,0x41,0x22,0x1c,0x00}, /* D */ {0x00,0x7f,0x49,0x49,0x49,0x49,0x41,0x00}, /* E */ {0x00,0x7f,0x09,0x09,0x09,0x09,0x01,0x00}, /* F */ {0x00,0x1c,0x22,0x41,0x49,0x49,0x3a,0x00}, /* G */ {0x00,0x7f,0x08,0x08,0x08,0x08,0x7f,0x00}, /* H */ {0x00,0x00,0x41,0x7f,0x41,0x00,0x00,0x00}, /* I */ {0x00,0x20,0x40,0x40,0x40,0x40,0x3f,0x00}, /* J */ {0x00,0x7f,0x10,0x08,0x14,0x22,0x41,0x00}, /* K */ {0x00,0x7f,0x40,0x40,0x40,0x40,0x40,0x00}, /* L */ {0x7f,0x02,0x0c,0x30,0x0c,0x02,0x7f,0x00}, /* M */ {0x00,0x7f,0x02,0x04,0x08,0x10,0x7f,0x00}, /* N */ {0x00,0x1c,0x22,0x41,0x41,0x22,0x1c,0x00}, /* O */ {0x00,0x7f,0x09,0x09,0x09,0x09,0x06,0x00}, /* P */ {0x00,0x1c,0x22,0x41,0x51,0x22,0x5c,0x00}, /* Q */ {0x00,0x7f,0x09,0x09,0x19,0x29,0x46,0x00}, /* R */ {0x00,0x26,0x49,0x49,0x49,0x49,0x32,0x00}, /* S */ {0x01,0x01,0x01,0x7f,0x01,0x01,0x01,0x00}, /* T */ {0x00,0x3f,0x40,0x40,0x40,0x40,0x3f,0x00}, /* U */ {0x03,0x0c,0x30,0x40,0x30,0x0c,0x03,0x00}, /* V */ {0x1f,0x60,0x18,0x06,0x18,0x60,0x1f,0x00}, /* W */ {0x41,0x22,0x14,0x08,0x14,0x22,0x41,0x00}, /* X */ {0x01,0x02,0x04,0x78,0x04,0x02,0x01,0x00}, /* Y */ {0x00,0x41,0x61,0x51,0x49,0x45,0x43,0x00}, /* Z */ {0x00,0x00,0x00,0x00,0x7f,0x41,0x41,0x00}, /* [ */ {0x00,0x2b,0x2c,0x78,0x2c,0x2b,0x00,0x00}, /* ¥ */ {0x41,0x41,0x7f,0x00,0x00,0x00,0x00,0x00}, /* ] */ {0x00,0x00,0x02,0x01,0x02,0x00,0x00,0x00}, /* ^ */ {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00}, /* _ */ #endif {0x00,0x00,0x01,0x02,0x00,0x00,0x00,0x00}, /* ` */ #if OPT_FONT_LOWER_CASE {0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00}, /* a */ {0x00,0x7f,0x48,0x44,0x44,0x38,0x00,0x00}, /* b */ {0x00,0x38,0x44,0x44,0x44,0x28,0x00,0x00}, /* c */ {0x00,0x38,0x44,0x44,0x48,0x7f,0x00,0x00}, /* d */ {0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00}, /* e */ {0x00,0x00,0x04,0x7e,0x05,0x01,0x00,0x00}, /* f */ {0x00,0x08,0x54,0x54,0x54,0x3c,0x00,0x00}, /* g */ {0x00,0x7f,0x08,0x04,0x04,0x78,0x00,0x00}, /* h */ {0x00,0x00,0x00,0x7d,0x00,0x00,0x00,0x00}, /* i */ {0x00,0x20,0x40,0x40,0x3d,0x00,0x00,0x00}, /* j */ {0x00,0x00,0x7f,0x10,0x28,0x44,0x00,0x00}, /* k */ {0x00,0x00,0x01,0x7f,0x00,0x00,0x00,0x00}, /* l */ {0x00,0x7c,0x04,0x78,0x04,0x78,0x00,0x00}, /* m */ {0x00,0x7c,0x08,0x04,0x04,0x78,0x00,0x00}, /* n */ {0x00,0x38,0x44,0x44,0x44,0x38,0x00,0x00}, /* o */ {0x00,0x7c,0x14,0x14,0x14,0x08,0x00,0x00}, /* p */ {0x00,0x08,0x14,0x14,0x14,0x7c,0x00,0x00}, /* q */ {0x00,0x7c,0x08,0x04,0x04,0x08,0x00,0x00}, /* r */ {0x00,0x48,0x54,0x54,0x54,0x24,0x00,0x00}, /* s */ {0x00,0x04,0x3e,0x44,0x44,0x20,0x00,0x00}, /* t */ {0x00,0x3c,0x40,0x40,0x20,0x7c,0x00,0x00}, /* u */ {0x00,0x0c,0x30,0x40,0x30,0x0c,0x00,0x00}, /* v */ {0x00,0x1c,0x60,0x18,0x60,0x1c,0x00,0x00}, /* w */ {0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00}, /* x */ {0x00,0x44,0x58,0x20,0x18,0x04,0x00,0x00}, /* y */ {0x00,0x44,0x64,0x54,0x4c,0x44,0x00,0x00}, /* z */ #endif {0x00,0x00,0x00,0x08,0x36,0x41,0x41,0x00}, /* { */ {0x00,0x00,0x00,0xff,0x00,0x00,0x00,0x00}, /* │ */ {0x41,0x41,0x36,0x08,0x00,0x00,0x00,0x00}, /* } */ {0x00,0x00,0x02,0x01,0x02,0x01,0x00,0x00}, /* ~ */ }; #if OPT_FONT_LARGE_DIGIT PROGMEM const uint8_t FONT_LARGE_DIGIT[][19 * 3] = { { // 0 0xFE,0xFE,0xFD,0xFD,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x03,0x03,0xFD,0xFD,0xFE,0xFE, 0xF7,0xF7,0xE3,0xE3,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE3,0xE3,0xF7,0xF7, 0x7F,0x7F,0xBF,0xBF,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xBF,0xBF,0x7F,0x7F, }, { // 1 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0xFC,0xFE,0xFE, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE3,0xE3,0xF7,0xF7, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x3F,0x7F,0x7F, }, { // 2 0x00,0x00,0x01,0x01,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x03,0x03,0xFD,0xFD,0xFE,0xFE, 0xF0,0xF0,0xE8,0xE8,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x0B,0x0B,0x07,0x07, 0x7F,0x7F,0xBF,0xBF,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0x80,0x80,0x00,0x00, }, { // 3 0x00,0x00,0x01,0x01,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x03,0x03,0xFD,0xFD,0xFE,0xFE, 0x00,0x00,0x08,0x08,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0xEB,0xEB,0xF7,0xF7, 0x00,0x00,0x80,0x80,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xBF,0xBF,0x7F,0x7F, }, { // 4 0xFE,0xFE,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0xFC,0xFE,0xFE, 0x07,0x07,0x0B,0x0B,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0xEB,0xEB,0xF7,0xF7, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x3F,0x7F,0x7F, }, { // 5 0xFE,0xFE,0xFD,0xFD,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x03,0x03,0x01,0x01,0x00,0x00, 0x07,0x07,0x0B,0x0B,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0xE8,0xE8,0xF0,0xF0, 0x00,0x00,0x80,0x80,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xBF,0xBF,0x7F,0x7F, }, { // 6 0xFE,0xFE,0xFD,0xFD,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x03,0x03,0x01,0x01,0x00,0x00, 0xF7,0xF7,0xEB,0xEB,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0xE8,0xE8,0xF0,0xF0, 0x7F,0x7F,0xBF,0xBF,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xBF,0xBF,0x7F,0x7F, }, { // 7 0x00,0x00,0x01,0x01,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x03,0x03,0xFD,0xFD,0xFE,0xFE, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE3,0xE3,0xF7,0xF7, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x3F,0x7F,0x7F, }, { // 8 0xFE,0xFE,0xFD,0xFD,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x03,0x03,0xFD,0xFD,0xFE,0xFE, 0xF7,0xF7,0xEB,0xEB,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0xEB,0xEB,0xF7,0xF7, 0x7F,0x7F,0xBF,0xBF,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xBF,0xBF,0x7F,0x7F, }, { // 9 0xFE,0xFE,0xFD,0xFD,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x03,0x03,0xFD,0xFD,0xFE,0xFE, 0x07,0x07,0x0B,0x0B,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0xEB,0xEB,0xF7,0xF7, 0x00,0x00,0x80,0x80,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xBF,0xBF,0x7F,0x7F, }, }; PROGMEM const uint8_t FONT_LARGE_COLON[][6 * 3] = { { // : 0x00,0xE0,0xF0,0xF0,0xE0,0x00, 0x00,0x00,0x81,0x81,0x00,0x00, 0x00,0x07,0x0F,0x0F,0x07,0x00, }, { // . 0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x38,0x7C,0x7C,0x38,0x00, }, }; #endif #if OPT_FONT_MEDIUM_DIGIT PROGMEM const uint8_t FONT_MEDIUM_DIGIT[][11 * 2] = { { // 0 0xFE,0x7D,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x7D,0xFE, 0x7F,0xBE,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xBE,0x7F, }, { // 1 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0xFE, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x7F, }, { // 2 0x00,0x01,0x83,0x83,0x83,0x83,0x83,0x83,0x83,0x7D,0xFE, 0x7F,0xBE,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0x80,0x00, }, { // 3 0x00,0x01,0x83,0x83,0x83,0x83,0x83,0x83,0x83,0x7D,0xFE, 0x00,0x80,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xBE,0x7F, }, { // 4 0xFE,0x7C,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7C,0xFE, 0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x3E,0x7F, }, { // 5 0xFE,0x7D,0x83,0x83,0x83,0x83,0x83,0x83,0x83,0x01,0x00, 0x00,0x80,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xBE,0x7F, }, { // 6 0xFE,0x7D,0x83,0x83,0x83,0x83,0x83,0x83,0x83,0x01,0x00, 0x7F,0xBE,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xBE,0x7F, }, { // 7 0x00,0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x7D,0xFE, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x7F, }, { // 8 0xFE,0x7D,0x83,0x83,0x83,0x83,0x83,0x83,0x83,0x7D,0xFE, 0x7F,0xBE,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xBE,0x7F, }, { // 9 0xFE,0x7D,0x83,0x83,0x83,0x83,0x83,0x83,0x83,0x7D,0xFE, 0x00,0x80,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xC1,0xBE,0x7F, } }; #endif |
【ソース・コード2】
プログラムメモリ領域を空けるためにフォントデータをEEPROMに移動している。このプログラムを実行するとEEPROMにフォントデータを書き込んでくれる。注意点としてはAtmel Studioの規定設定だとプログラム書き込み時にEEPROMも消去してしまうのでAtmel Studio にてEEPROMメモリ内容をHEX形式で保存しておき、そのHEXデータからいつでも再書き込みできるように設定しておくと便利。
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 |
/* * main.cpp * * Created: 2018/12/01 * Author : Sasapea's Lab */ #include <stdint.h> #include <stdbool.h> #include <avr/eeprom.h> #include <avr/pgmspace.h> PROGMEM const uint8_t SSD1306_FONT[][8] = { {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* */ {0x00,0x00,0x00,0x5f,0x00,0x00,0x00,0x00}, /* ! */ {0x06,0x03,0x01,0x06,0x03,0x01,0x00,0x00}, /* ″ */ {0x20,0x62,0x3e,0x63,0x3e,0x23,0x02,0x00}, /* # */ {0x00,0x24,0x2a,0x7a,0x2f,0x2a,0x12,0x00}, /* $ */ {0x42,0x25,0x12,0x08,0x24,0x52,0x21,0x00}, /* % */ {0x20,0x56,0x49,0x55,0x22,0x58,0x40,0x00}, /* & */ {0x06,0x03,0x01,0x00,0x00,0x00,0x00,0x00}, /* ′ */ {0x00,0x00,0x00,0x00,0x1c,0x22,0x41,0x00}, /* ( */ {0x41,0x22,0x1c,0x00,0x00,0x00,0x00,0x00}, /* ) */ {0x00,0x22,0x14,0x7f,0x14,0x22,0x00,0x00}, /* * */ {0x08,0x08,0x08,0x7f,0x08,0x08,0x08,0x00}, /* + */ {0x50,0x30,0x00,0x00,0x00,0x00,0x00,0x00}, /* , */ {0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00}, /* - */ {0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00}, /* . */ {0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00}, /* / */ {0x00,0x3e,0x41,0x41,0x41,0x41,0x3e,0x00}, /* 0 */ {0x00,0x00,0x42,0x7f,0x40,0x00,0x00,0x00}, /* 1 */ {0x00,0x62,0x51,0x51,0x49,0x49,0x46,0x00}, /* 2 */ {0x00,0x22,0x41,0x49,0x49,0x49,0x36,0x00}, /* 3 */ {0x00,0x30,0x28,0x24,0x22,0x7f,0x20,0x00}, /* 4 */ {0x00,0x2f,0x45,0x45,0x45,0x45,0x39,0x00}, /* 5 */ {0x00,0x3e,0x49,0x49,0x49,0x49,0x32,0x00}, /* 6 */ {0x00,0x01,0x01,0x61,0x19,0x05,0x03,0x00}, /* 7 */ {0x00,0x36,0x49,0x49,0x49,0x49,0x36,0x00}, /* 8 */ {0x00,0x26,0x49,0x49,0x49,0x49,0x3e,0x00}, /* 9 */ {0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00}, /* : */ {0x00,0x00,0x56,0x36,0x00,0x00,0x00,0x00}, /* ; */ {0x00,0x00,0x00,0x08,0x14,0x22,0x41,0x00}, /* 〈 */ {0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x00}, /* = */ {0x41,0x22,0x14,0x08,0x00,0x00,0x00,0x00}, /* 〉 */ {0x00,0x02,0x01,0x51,0x09,0x09,0x06,0x00}, /* ? */ {0x1c,0x22,0x59,0x55,0x4d,0x12,0x0c,0x00}, /* @ */ {0x60,0x18,0x16,0x11,0x16,0x18,0x60,0x00}, /* A */ {0x00,0x7f,0x49,0x49,0x49,0x49,0x36,0x00}, /* B */ {0x00,0x1c,0x22,0x41,0x41,0x41,0x22,0x00}, /* C */ {0x00,0x7f,0x41,0x41,0x41,0x22,0x1c,0x00}, /* D */ {0x00,0x7f,0x49,0x49,0x49,0x49,0x41,0x00}, /* E */ {0x00,0x7f,0x09,0x09,0x09,0x09,0x01,0x00}, /* F */ {0x00,0x1c,0x22,0x41,0x49,0x49,0x3a,0x00}, /* G */ {0x00,0x7f,0x08,0x08,0x08,0x08,0x7f,0x00}, /* H */ {0x00,0x00,0x41,0x7f,0x41,0x00,0x00,0x00}, /* I */ {0x00,0x20,0x40,0x40,0x40,0x40,0x3f,0x00}, /* J */ {0x00,0x7f,0x10,0x08,0x14,0x22,0x41,0x00}, /* K */ {0x00,0x7f,0x40,0x40,0x40,0x40,0x40,0x00}, /* L */ {0x7f,0x02,0x0c,0x30,0x0c,0x02,0x7f,0x00}, /* M */ {0x00,0x7f,0x02,0x04,0x08,0x10,0x7f,0x00}, /* N */ {0x00,0x1c,0x22,0x41,0x41,0x22,0x1c,0x00}, /* O */ {0x00,0x7f,0x09,0x09,0x09,0x09,0x06,0x00}, /* P */ {0x00,0x1c,0x22,0x41,0x51,0x22,0x5c,0x00}, /* Q */ {0x00,0x7f,0x09,0x09,0x19,0x29,0x46,0x00}, /* R */ {0x00,0x26,0x49,0x49,0x49,0x49,0x32,0x00}, /* S */ {0x01,0x01,0x01,0x7f,0x01,0x01,0x01,0x00}, /* T */ {0x00,0x3f,0x40,0x40,0x40,0x40,0x3f,0x00}, /* U */ {0x03,0x0c,0x30,0x40,0x30,0x0c,0x03,0x00}, /* V */ {0x1f,0x60,0x18,0x06,0x18,0x60,0x1f,0x00}, /* W */ {0x41,0x22,0x14,0x08,0x14,0x22,0x41,0x00}, /* X */ {0x01,0x02,0x04,0x78,0x04,0x02,0x01,0x00}, /* Y */ {0x00,0x41,0x61,0x51,0x49,0x45,0x43,0x00}, /* Z */ {0x00,0x00,0x00,0x00,0x7f,0x41,0x41,0x00}, /* [ */ {0x00,0x2b,0x2c,0x78,0x2c,0x2b,0x00,0x00}, /* ¥ */ {0x41,0x41,0x7f,0x00,0x00,0x00,0x00,0x00}, /* ] */ {0x00,0x00,0x02,0x01,0x02,0x00,0x00,0x00}, /* ^ */ {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00}, /* _ */ }; typedef struct { const char *name; const uint8_t *addr; const uint16_t size; } menu_t; const menu_t menus[] = { { "SSD1306_FONT", SSD1306_FONT[0], sizeof(SSD1306_FONT)}, }; int main() { uint16_t addr = E2END + 1; uint8_t i, temp[128]; for (i = 0; i < sizeof(menus) / sizeof(menus[0]); ++i) addr -= menus[i].size; for (i = 0; i < sizeof(menus) / sizeof(menus[0]); ++i) { uint8_t *data = (uint8_t *)menus[i].addr; uint16_t size = menus[i].size; while (size) { uint16_t len = (sizeof(temp) < size ? sizeof(temp) : size); memcpy_P(temp, data, len); eeprom_busy_wait(); eeprom_write_block(temp, (void *)addr, len); data += len; addr += len; size -= len; } } while (1) continue; } |