egoShield
screen.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: screen.h *
3 * Version: 1.1.1 *
4 * Date: April 1st, 2020 *
5 * Author: Mogens Groth Nicolaisen *
6 * *
7 *********************************************************************************************
8 * (C) 2020 *
9 * *
10 * uStepper ApS *
11 * www.ustepper.com *
12 * administration@ustepper.com *
13 * *
14 * The code contained in this file is released under the following open source license: *
15 * *
16 * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International *
17 * *
18 * The code in this file is provided without warranty of any kind - use at own risk! *
19 * neither uStepper ApS nor the author, can be held responsible for any damage *
20 * caused by the use of the code contained in this file ! *
21 * *
22 ********************************************************************************************/
29 #ifndef _SCREEN_H_
30 #define _SCREEN_H_
31 
32 #include <inttypes.h>
33 #include <avr/io.h>
34 #include <stdlib.h>
35 #include "Arduino.h"
36 #include <avr/pgmspace.h>
37 #include <EEPROM.h>
38 
39 #define SSD1306_ADDR 0x3C
40 
41 #define LEFT 0
42 #define RIGHT 9999
43 #define CENTER 9998
44 
45 #define SLEEP_ON true
46 #define SLEEP_OFF false
47 
48 #define SSD1306_COMMAND 0x00
49 #define SSD1306_DATA 0xC0
50 #define SSD1306_DATA_CONTINUE 0x40
51 
52 // Parameters for 128x64 displays
53 #define SSD1306_128X64 0x40
54 #define H64_MULTIPLEX_RATIO 0x3F
55 #define H64_COM_PINS 0x12
56 #define H64_BUFFER_SIZE 0x0400
57 #define H64_DISPLAY_HEIGHT 0x40
58 
59 // SSD1306 Commandset
60 // ------------------
61 // Fundamental Commands
62 #define SSD1306_SET_CONTRAST_CONTROL 0x81
63 #define SSD1306_DISPLAY_ALL_ON_RESUME 0xA4
64 #define SSD1306_DISPLAY_ALL_ON 0xA5
65 #define SSD1306_NORMAL_DISPLAY 0xA6
66 #define SSD1306_INVERT_DISPLAY 0xA7
67 #define SSD1306_DISPLAY_OFF 0xAE
68 #define SSD1306_DISPLAY_ON 0xAF
69 #define SSD1306_NOP 0xE3
70 // Scrolling Commands
71 #define SSD1306_HORIZONTAL_SCROLL_RIGHT 0x26
72 #define SSD1306_HORIZONTAL_SCROLL_LEFT 0x27
73 #define SSD1306_HORIZONTAL_SCROLL_VERTICAL_AND_RIGHT 0x29
74 #define SSD1306_HORIZONTAL_SCROLL_VERTICAL_AND_LEFT 0x2A
75 #define SSD1306_DEACTIVATE_SCROLL 0x2E
76 #define SSD1306_ACTIVATE_SCROLL 0x2F
77 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
78 // Addressing Setting Commands
79 #define SSD1306_SET_LOWER_COLUMN 0x00
80 #define SSD1306_SET_HIGHER_COLUMN 0x10
81 #define SSD1306_MEMORY_ADDR_MODE 0x20
82 #define SSD1306_SET_COLUMN_ADDR 0x21
83 #define SSD1306_SET_PAGE_ADDR 0x22
84 // Hardware Configuration Commands
85 #define SSD1306_SET_START_LINE 0x40
86 #define SSD1306_SET_SEGMENT_REMAP 0xA0
87 #define SSD1306_SET_MULTIPLEX_RATIO 0xA8
88 #define SSD1306_COM_SCAN_DIR_INC 0xC0
89 #define SSD1306_COM_SCAN_DIR_DEC 0xC8
90 #define SSD1306_SET_DISPLAY_OFFSET 0xD3
91 #define SSD1306_SET_COM_PINS 0xDA
92 #define SSD1306_CHARGE_PUMP 0x8D
93 // Timing & Driving Scheme Setting Commands
94 #define SSD1306_SET_DISPLAY_CLOCK_DIV_RATIO 0xD5
95 #define SSD1306_SET_PRECHARGE_PERIOD 0xD9
96 #define SSD1306_SET_VCOM_DESELECT 0xDB
97 
98 #define SSD1306_SEND_CMD(x) this->cmd(x);\
99  if(this->busFailure)\
100  {\
101  sei();\
102  return;\
103  }\
104 
105 const uint8_t font[] PROGMEM =
106 {
107 0x06, 0x08, 0x20, 0x5f,
108 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // sp
109 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, // !
110 0x00, 0x00, 0x07, 0x00, 0x07, 0x00, // "
111 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14, // #
112 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12, // $
113 0x00, 0x23, 0x13, 0x08, 0x64, 0x62, // %
114 0x00, 0x36, 0x49, 0x55, 0x22, 0x50, // &
115 0x00, 0x00, 0x05, 0x03, 0x00, 0x00, // '
116 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00, // (
117 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00, // )
118 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14, // *
119 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08, // +
120 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00, // ,
121 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, // -
122 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, // .
123 0x00, 0x20, 0x10, 0x08, 0x04, 0x02, // /
124 
125 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
126 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00, // 1
127 0x00, 0x42, 0x61, 0x51, 0x49, 0x46, // 2
128 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31, // 3
129 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10, // 4
130 0x00, 0x27, 0x45, 0x45, 0x45, 0x39, // 5
131 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30, // 6
132 0x00, 0x01, 0x71, 0x09, 0x05, 0x03, // 7
133 0x00, 0x36, 0x49, 0x49, 0x49, 0x36, // 8
134 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E, // 9
135 0x00, 0x00, 0x36, 0x36, 0x00, 0x00, // :
136 0x00, 0x00, 0x56, 0x36, 0x00, 0x00, // ;
137 0x00, 0x08, 0x14, 0x22, 0x41, 0x00, // <
138 0x00, 0x14, 0x14, 0x14, 0x14, 0x14, // =
139 0x00, 0x00, 0x41, 0x22, 0x14, 0x08, // >
140 0x00, 0x02, 0x01, 0x51, 0x09, 0x06, // ?
141 
142 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E, // @
143 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C, // A
144 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36, // B
145 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22, // C
146 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C, // D
147 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41, // E
148 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01, // F
149 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A, // G
150 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F, // H
151 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00, // I
152 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01, // J
153 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41, // K
154 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40, // L
155 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F, // M
156 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F, // N
157 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E, // O
158 
159 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06, // P
160 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E, // Q
161 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46, // R
162 0x00, 0x46, 0x49, 0x49, 0x49, 0x31, // S
163 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01, // T
164 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F, // U
165 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F, // V
166 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F, // W
167 0x00, 0x63, 0x14, 0x08, 0x14, 0x63, // X
168 0x00, 0x07, 0x08, 0x70, 0x08, 0x07, // Y
169 0x00, 0x61, 0x51, 0x49, 0x45, 0x43, // Z
170 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00, // [
171 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, // Backslash (Checker pattern)
172 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00, // ]
173 0x00, 0x04, 0x02, 0x01, 0x02, 0x04, // ^
174 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, // _
175 
176 0x00, 0x00, 0x03, 0x05, 0x00, 0x00, // `
177 0x00, 0x20, 0x54, 0x54, 0x54, 0x78, // a
178 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38, // b
179 0x00, 0x38, 0x44, 0x44, 0x44, 0x20, // c
180 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F, // d
181 0x00, 0x38, 0x54, 0x54, 0x54, 0x18, // e
182 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02, // f
183 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C, // g
184 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78, // h
185 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00, // i
186 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00, // j
187 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00, // k
188 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00, // l
189 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78, // m
190 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78, // n
191 0x00, 0x38, 0x44, 0x44, 0x44, 0x38, // o
192 
193 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18, // p
194 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC, // q
195 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08, // r
196 0x00, 0x48, 0x54, 0x54, 0x54, 0x20, // s
197 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20, // t
198 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C, // u
199 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C, // v
200 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C, // w
201 0x00, 0x44, 0x28, 0x10, 0x28, 0x44, // x
202 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C, // y
203 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44, // z
204 0x00, 0x00, 0x10, 0x7C, 0x82, 0x00, // {
205 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, // |
206 0x00, 0x00, 0x82, 0x7C, 0x10, 0x00, // }
207 0x00, 0x00, 0x06, 0x09, 0x09, 0x06 // ~ (Degrees)
208 };
209 
210 //Created using dot2pic.com
211 const uint8_t repeatBmp[] PROGMEM ={
212  0x00,0x08,0x18,0x3e,0x1a,0x4a,0x42,0x42,0x42,0x42,0x52,0x58,0x7c,0x18,0x10,0x00};
213 
214 //Created using dot2pic.com
215 const uint8_t playPauseBmp[] PROGMEM ={
216  0x00,0x00,0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x00,0x00,0x3f,0x3f,0x00,0x00,0x00,
217  0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0x00,0x00,0xfc,0xfc,0x00,0x00,0x00};
218 
219 //Created using dot2pic.com
220 const uint8_t fastForwardBmp[] PROGMEM={
221  0x00,0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x00,0x00,
222  0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0x00,0x00};
223 
224 //Created using dot2pic.com
225 const uint8_t fastRewindBmp[] PROGMEM ={
226  0x00,0x00,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00,0x00,
227  0x00,0x00,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00};
228 
229 //Created using dot2pic.com
230 const uint8_t playBmp[] PROGMEM ={
231  0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x00,0x00,0x00,0x00,0x00,
232  0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0x00,0x00,0x00,0x00,0x00};
233 
234 //Created using dot2pic.com
235 const uint8_t pauseBmp[] PROGMEM = {
236  0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0x00,
237  0x00,0x00,0x00,0xf8,0xf8,0xf8,0xf8,0x00,0x00,0xf8,0xf8,0xf8,0xf8,0x00,0x00,0x00};
238 
239 //Created using dot2pic.com
240 const uint8_t stopBmp[] PROGMEM = {
241  0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0x00,
242  0x00,0x00,0x00,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0xf8,0x00,0x00,0x00};
243 
244 //Created using dot2pic.com
245 const uint8_t recordBmp[] PROGMEM ={
246  0x00,0x00,0x00,0x03,0x0f,0x0f,0x1f,0x1f,0x1f,0x1f,0x0f,0x0f,0x03,0x00,0x00,0x00,
247  0x00,0x00,0x00,0xc0,0xf0,0xf0,0xf8,0xf8,0xf8,0xf8,0xf0,0xf0,0xc0,0x00,0x00,0x00};
248 
249 //Created using dot2pic.com
250 const uint8_t logoBmp[] PROGMEM ={
251  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
252  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
253  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
254  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
255  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x00,
256  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
257  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
258  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
259  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x3e,0x62,0x42,0x42,
260  0x7c,0x00,0x01,0x3e,0x40,0x00,0x0c,0x4a,0x4a,0x4a,0x6c,0x00,0x00,0x3e,0x40,0x00,
261  0x3e,0x42,0x02,0x07,0x3e,0x40,0x00,0x38,0x68,0x44,0x45,0x47,0x40,0x00,0x00,0x00,
262  0x00,0x1c,0x22,0x42,0x42,0x7c,0x00,0x00,0x1e,0xe2,0x42,0x42,0x7c,0x00,0x00,0x04,
263  0x44,0x44,0x45,0x7e,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
264  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
265  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
266  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
267  0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,
268  0x03,0x03,0x03,0x83,0x03,0x01,0x00,0x80,0x80,0x80,0x80,0x00,0x03,0x03,0x03,0x03,
269  0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,
270  0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
271  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
272  0x00,0x00,0x00,0x00,0x06,0x1f,0x3f,0x3f,0x7f,0x7f,0x67,0x63,0x73,0x71,0x3c,0x3c,
273  0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x03,0x63,0x7f,0x7f,0x7f,0x7f,0x60,0x03,
274  0x17,0x37,0x77,0x76,0x77,0x7f,0x7f,0x3f,0x1e,0x0f,0x1f,0x3f,0x7f,0x79,0x71,0x77,
275  0xff,0xff,0xff,0xfd,0x80,0x0f,0x1f,0x3f,0x7f,0x79,0x71,0x77,0xff,0xff,0xff,0xfd,
276  0x80,0x03,0x17,0x37,0x77,0x76,0x77,0x7f,0x7f,0x3f,0x1c,0x00,0x30,0x30,0x7f,0x7f,
277  0x7f,0x7f,0x3c,0x00,0x06,0x1f,0x3f,0x3f,0x7f,0x7f,0x67,0x63,0x73,0x71,0x3c,0x3c,
278  0x00,0x23,0x3f,0x7f,0x7f,0x7c,0x30,0x21,0x7f,0x7f,0x7f,0x3e,0x00,0x00,0x00,0x00,
279  0x00,0x00,0x00,0x00,0x38,0x38,0x98,0x8c,0xcc,0xfc,0xfc,0xf8,0xf8,0xf0,0xc0,0x00,
280  0x00,0x00,0x00,0x00,0x00,0xc0,0xc0,0xc0,0x80,0xc0,0xc0,0xc0,0x80,0x80,0x00,0x00,
281  0x80,0xc0,0xc0,0xc0,0xc0,0x80,0x80,0x00,0x00,0x80,0xc0,0xc0,0xc0,0xc0,0x80,0xc0,
282  0xc0,0xc0,0x80,0x80,0x00,0x80,0xc0,0xc0,0xc0,0xc0,0x80,0xc0,0xc0,0xc0,0x80,0x80,
283  0x00,0x00,0x80,0xc0,0xc0,0xc0,0xc0,0x80,0x80,0x00,0x00,0xc0,0xc0,0xf0,0xf0,0xf0,
284  0xe0,0xe0,0xc0,0x00,0x38,0x38,0x98,0x8c,0xcc,0xfc,0xfc,0xf8,0xf8,0xf0,0xc0,0x00,
285  0x00,0xc0,0xc0,0xc0,0xc0,0x80,0x00,0xc0,0xc0,0xc0,0xc0,0x80,0x00,0x00,0x00,0x00,
286  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
287  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
288  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
289  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
290  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
291  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
292  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
293 
294 class Screen
295 {
296 public:
297  Screen(bool i2cChannel);
298  void init();
299  void clrScreen();
300  void drawRect(int x1, int y1, int x2, int y2, bool color);
301  void drawImage(const uint8_t *string, uint8_t x, uint8_t y, uint8_t width, uint8_t height, bool invert = 0);
302  void printString(const uint8_t *string, uint8_t x, uint8_t y, bool invert = 0);
303  void* operator new(size_t size);
304  bool busFailure;
305 protected:
306  bool waitForAck();
307  void cmd(uint8_t cmd);
308  volatile uint8_t twsr;
309  volatile uint8_t twbr;
310  volatile uint8_t twdr;
311  volatile uint8_t twcr;
312 
313 };
314 #endif
Screen
Definition: screen.h:294