-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare_sketch.ino
562 lines (527 loc) · 18.5 KB
/
square_sketch.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
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
//==============================================================================
// File Name: square_sketch.cpp
// Description: Main Application Loop Square Sketch
//==============================================================================
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono12pt7b.h>
#include <string.h>
#include "square_sketch.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)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
uint16_t bufsize = SCREEN_WIDTH * ((SCREEN_HEIGHT + 7) / 8);
SquareSketchEEPROM myeeprom;
SquareSketchPicture myimage;
uint8_t cursor_count = 0;
bool cleardisplay = true;
bool draw_initialized = false;
bool load_buffer = false;
uint8_t *imagebuffer;
/*******************************************************************************
function name: gfx_prepare
function desc: initializes graphics for text display
inputs:
outputs:
returns:
*******************************************************************************/
void gfx_prepare(void) {
display.setFont();
display.setTextSize(0.5); // Normal 1:1 pixel scale
display.setTextColor(1);
display.setCursor(0,0); // Start at top-left corner
}
/*******************************************************************************
function name: draw
function desc: main draw routine for all machine states
inputs:
outputs:
returns:
*******************************************************************************/
void draw() {
if (cleardisplay) {
display.clearDisplay();
cleardisplay = false;
delay(100);
}
char textbuf[22];
char textoutput[22];
switch (get_machinestate()) {
case DRAWMODE :
{
if (load_buffer) {
display.clearDisplay();
load_sketch();
load_buffer = false;
}
point current = myimage.get_current_loc();
if (cursor_count % (cursor_modulus[0])) {
display.drawPixel(current.x, current.y, SSD1306_WHITE);
}
else {
display.drawPixel(current.x, current.y, SSD1306_BLACK);
}
}
break;
case MAINMENU :
{
gfx_prepare();
for( uint8_t i = 0; i < MAX_MAIN_MENU; i++ ) {
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(MainMenu[i])));
if ( i == get_menu_current() ) {
display.setTextColor(0, 1);
}
else {
display.setTextColor(1, 0);
}
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
}
}
break;
case ABOUTMODE :
{
gfx_prepare();
for (uint8_t i=0; i<7; i++) {
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(about_table[i])));
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
}
}
break;
case SPLASH :
gfx_prepare();
display.drawBitmap((SCREEN_WIDTH - logo_width) / 2, (SCREEN_HEIGHT - logo_height) / 2, logo_data, logo_width, logo_height, 1);
for (uint8_t i=0; i < 6; i++) {
display.println();
}
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(splash_table[0])));
display.println(textbuf);
break;
case CLEARSCR :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[0])));
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
display.println();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[5])));
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
draw_initialized = false;
}
break;
case SAVEEEPROM :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[7])));
display.println(textbuf);
display.display();
write_to_eeprom();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[1])));
display.println(textbuf);
display.println();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[5])));
display.println(textbuf);
}
break;
case LOADEEPROM :
{
uint16_t tmpclick;
tmpclick = get_clicks_per_move_option();
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[8])));
display.println(textbuf);
display.display();
read_from_eeprom();
setCurrentPixel(64*tmpclick,32*tmpclick);
myimage.reset_pen();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[2])));
display.println(textbuf);
display.println();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[5])));
display.println(textbuf);
draw_initialized = true;
}
break;
case CLEAREEPROM :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[9])));
display.println(textbuf);
display.display();
clear_eeprom();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[3])));
display.println(textbuf);
display.println();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[5])));
display.println(textbuf);
}
break;
case OPTIONSMODE :
{
gfx_prepare();
for (uint8_t i=0; i<MAX_OPTIONS_MENU; i++) {
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(options_table[i])));
if ( i == get_options_menu_current() ) {
display.setTextColor(0, 1);
}
else {
display.setTextColor(1, 0);
}
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
}
}
break;
case CLICKSMODE :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(options_choice[0])));
char str[4];
sprintf(str, "%d", get_clicks_per_move_option());
strcat(textbuf, (const char*)&str);
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
}
break;
case DEBOUNCEMODE :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(options_choice[1])));
char str[4];
sprintf(str, "%d", get_debounce_time_option());
strcat(textbuf, (const char*)&str);
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
}
break;
case LONGCLICKMODE :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(options_choice[2])));
char str[4];
sprintf(str, "%d", get_longpress_time_option());
strcat(textbuf, (const char*)&str);
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
}
break;
case RESETMODE :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[4])));
display.println(textbuf);
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[5])));
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
}
break;
case ERRORMODE :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[6])));
display.println(textbuf);
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(modes_table[5])));
sprintf(textoutput, "%-21s", textbuf);
display.println(textoutput);
}
break;
case CLEARSCROK :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[2])));
display.println(textbuf);
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[1])));
display.println(textbuf);
display.println();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[0])));
display.println(textbuf);
}
break;
case SAVEEEPROMOK :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[3])));
display.println(textbuf);
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[1])));
display.println(textbuf);
display.println();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[0])));
display.println(textbuf);
}
break;
case LOADEEPROMOK :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[4])));
display.println(textbuf);
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[1])));
display.println(textbuf);
display.println();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[0])));
display.println(textbuf);
}
break;
case CLEAREEPROMOK :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[5])));
display.println(textbuf);
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[1])));
display.println(textbuf);
display.println();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[0])));
display.println(textbuf);
}
break;
case RESETMODEOK :
{
gfx_prepare();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[6])));
display.println(textbuf);
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[1])));
display.println(textbuf);
display.println();
strcpy_P(textbuf, (PGM_P)pgm_read_word(&(confirm_table[0])));
display.println(textbuf);
}
break;
}
display.display();
}
/*******************************************************************************
function name: setup
function desc: main setup function
inputs:
outputs:
returns:
*******************************************************************************/
void setup() {
Serial.begin(115200);
#ifdef SQSK_DEBUG
Serial.println("setup();");
#endif
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
imagebuffer = (uint8_t *)malloc(bufsize);
// Clear the buffer
display.clearDisplay();
display.setFont(&FreeMono12pt7b);
display.setCursor(0, 0); // Back to top-left corner
display.setTextWrap(false); // Allow spaces to go off right edge
myeeprom.begin(bufsize);
axisencoder_setup(64,32, &myimage, &myeeprom);
pinMode(PEN_LED, OUTPUT);
}
/*******************************************************************************
function name: loop
function desc: main loop
inputs:
outputs:
returns:
*******************************************************************************/
void loop() {
draw();
machine_state macstate = get_machinestate();
switch (macstate) {
case SPLASH :
delay(SPLASHDELAY);
display.clearDisplay();
set_machinestate(MAINMENU);
break;
case DRAWMODE :
cursor_count++;
if (cursor_count >= (PENDOWN*4)) {
cursor_count = 0;
}
switch(myimage.what_pen()) {
case 0:
digitalWrite(PEN_LED, LOW);
break;
case 1:
digitalWrite(PEN_LED, HIGH);
break;
case 2:
if (cursor_count > (PENDOWN*2)) {
digitalWrite(PEN_LED, HIGH);
}
else {
digitalWrite(PEN_LED, LOW);
}
break;
default:
break;
}
break;
case CLEARSCR :
{
clear_save_buffer();
myimage.reset_pen();
axisencoder_setup_again(64,32);
uint8_t cpm = get_clicks_per_move_option();
setCurrentPixel(64*cpm,32*cpm);
}
break;
case RESETMODE :
{
point cur;
cur = myimage.get_current_loc();
myeeprom.set_option(CLICKS, DEF_CHOICE_CLICKS);
myeeprom.set_option(DEBOUNCE, DEF_CHOICE_DEBOUNCE);
myeeprom.set_option(LONG, DEF_CHOICE_LONGCLICKS);
set_clicks_per_move_option(DEF_CHOICE_CLICKS);
set_debounce_time_option(DEF_CHOICE_DEBOUNCE);
set_longpress_time_option(DEF_CHOICE_LONGCLICKS);
axisencoder_setup_again(cur.x,cur.y);
set_options_menu_count(3 * DEF_CHOICE_CLICKS);
}
break;
default:
digitalWrite(PEN_LED, LOW);
break;
}
}
/*******************************************************************************
function name: drawPixelWithPen
function desc: draw the pixel with current pen state
inputs:
outputs:
returns:
*******************************************************************************/
void drawPixelWithPen(point pt) {
uint8_t current_pen = myimage.what_pen();
#ifdef SQSK_DEBUG
Serial.print("Pen: ");
Serial.println(current_pen);
Serial.print("drawPixelWithPen ");
Serial.println(current_pen);
#endif
if (current_pen == 1) {
display.drawPixel(pt.x, pt.y, SSD1306_WHITE);
}
else if (current_pen == 2) {
display.drawPixel(pt.x, pt.y, SSD1306_BLACK);
}
}
/*******************************************************************************
function name: drawPixelPrev
function desc: draw pixel to what it was originally
inputs:
outputs:
returns:
*******************************************************************************/
void drawPixelPrev(point pt) {
#ifdef SQSK_DEBUG
Serial.print("Prev Pixel: ");
Serial.println(pt.is_drawn);
Serial.print("drawPixelPrev is_drawn:");
Serial.println(pt.is_drawn );
#endif
if (pt.is_drawn == 1) {
display.drawPixel(pt.x, pt.y, SSD1306_WHITE);
}
else {
display.drawPixel(pt.x, pt.y, SSD1306_BLACK);
}
}
/*******************************************************************************
function name: write_to_eeprom
function desc: copy buffer to eeprom
inputs:
outputs:
returns:
*******************************************************************************/
void write_to_eeprom(void) {
myeeprom.Buffer_to_EEPROM(imagebuffer);
}
/*******************************************************************************
function name: read_from_eeprom
function desc: copy eeprom to buffer
inputs:
outputs:
returns:
*******************************************************************************/
void read_from_eeprom(void) {
myeeprom.EEPROM_to_Buffer(imagebuffer);
}
/*******************************************************************************
function name: clear_eeprom
function desc: erase eeprom image
inputs:
outputs:
returns:
*******************************************************************************/
void clear_eeprom(void) {
myeeprom.Reset_EEPROM();
}
/*******************************************************************************
function name: save_sketch
function desc: save framebuffer to memory
inputs:
outputs:
returns:
*******************************************************************************/
void save_sketch(void) {
memcpy(imagebuffer, display.getBuffer(), bufsize);
}
/*******************************************************************************
function name: load_sketch
function desc: load framebuffer from memory
inputs:
outputs:
returns:
*******************************************************************************/
void load_sketch(void) {
memcpy(display.getBuffer(), imagebuffer, bufsize);
}
/*******************************************************************************
function name: clear_save_buffer
function desc: erase save buffer
inputs:
outputs:
returns:
*******************************************************************************/
void clear_save_buffer(void) {
memset(imagebuffer, 0, bufsize);
}
/*******************************************************************************
function name: setCurrentPixel
function desc: set current location with original pixel
inputs:
outputs:
returns:
*******************************************************************************/
void setCurrentPixel(uint16_t x, uint16_t y) {
uint8_t cpm;
cpm = get_clicks_per_move_option();
uint8_t xx;
uint8_t yy;
xx = x / (uint16_t)cpm;
yy = y / (uint16_t)cpm;
myimage.set_current_loc(xx, yy, (display.getPixel(xx,yy) == 1 ? 1 : 0));
}
/*******************************************************************************
function name: correctCursor
function desc: try to avoid problems with cursor blinking artifacts
inputs:
outputs:
returns:
*******************************************************************************/
void correctCursor(void) {
point current = myimage.get_current_loc();
display.drawPixel(current.x, current.y, current.is_drawn);
}