-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.cpp
2996 lines (2909 loc) · 93.5 KB
/
ui.cpp
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of Repetier-Firmware.
Repetier-Firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Repetier-Firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Repetier-Firmware. If not, see <http://www.gnu.org/licenses/>.
*/
#define UI_MAIN
#include "Repetier.h"
extern const int8_t encoder_table[16] PROGMEM ;
#include <math.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#if UI_ENCODER_SPEED==0
const int8_t encoder_table[16] PROGMEM = {0,1,-1,0,-1,0,0,1,1,0,0,-1,0,-1,1,0}; // Full speed
#elif UI_ENCODER_SPEED==1
const int8_t encoder_table[16] PROGMEM = {0,0,-1,0,0,0,0,1,1,0,0,0,0,-1,0,0}; // Half speed
#else
//const int8_t encoder_table[16] PROGMEM = {0,0,0,0,0,0,0,0,1,0,0,0,0,-1,0,0}; // Quart speed
//const int8_t encoder_table[16] PROGMEM = {0,1,0,0,-1,0,0,0,0,0,0,0,0,0,0,0}; // Quart speed
const int8_t encoder_table[16] PROGMEM = {0,0,0,0,0,0,0,0,0,0,0,-1,0,0,1,0}; // Quart speed
#endif
#if BEEPER_TYPE==2 && defined(UI_HAS_I2C_KEYS) && UI_I2C_KEY_ADDRESS!=BEEPER_ADDRESS
#error Beeper address and i2c key address must be identical
#else
#if BEEPER_TYPE==2
#define UI_I2C_KEY_ADDRESS BEEPER_ADDRESS
#endif
#endif
#if UI_AUTORETURN_TO_MENU_AFTER!=0
long ui_autoreturn_time=0;
#endif
void beep(uint8_t duration,uint8_t count)
{
#if FEATURE_BEEPER
#if BEEPER_TYPE!=0
#if BEEPER_TYPE==1 && defined(BEEPER_PIN) && BEEPER_PIN>=0
SET_OUTPUT(BEEPER_PIN);
#endif
#if BEEPER_TYPE==2
HAL::i2cStartWait(BEEPER_ADDRESS+I2C_WRITE);
#if UI_DISPLAY_I2C_CHIPTYPE==1
HAL::i2cWrite( 0x14); // Start at port a
#endif
#endif
for(uint8_t i=0; i<count; i++)
{
#if BEEPER_TYPE==1 && defined(BEEPER_PIN) && BEEPER_PIN>=0
#if defined(BEEPER_TYPE_INVERTING) && BEEPER_TYPE_INVERTING
WRITE(BEEPER_PIN,LOW);
#else
WRITE(BEEPER_PIN,HIGH);
#endif
#else
#if UI_DISPLAY_I2C_CHIPTYPE==0
#if BEEPER_ADDRESS == UI_DISPLAY_I2C_ADDRESS
HAL::i2cWrite(uid.outputMask & ~BEEPER_PIN);
#else
HAL::i2cWrite(~BEEPER_PIN);
#endif
#endif
#if UI_DISPLAY_I2C_CHIPTYPE==1
HAL::i2cWrite((BEEPER_PIN) | uid.outputMask);
HAL::i2cWrite(((BEEPER_PIN) | uid.outputMask)>>8);
#endif
#endif
HAL::delayMilliseconds(duration);
#if BEEPER_TYPE==1 && defined(BEEPER_PIN) && BEEPER_PIN>=0
#if defined(BEEPER_TYPE_INVERTING) && BEEPER_TYPE_INVERTING
WRITE(BEEPER_PIN,HIGH);
#else
WRITE(BEEPER_PIN,LOW);
#endif
#else
#if UI_DISPLAY_I2C_CHIPTYPE==0
#if BEEPER_ADDRESS == UI_DISPLAY_I2C_ADDRESS
HAL::i2cWrite((BEEPER_PIN) | uid.outputMask);
#else
HAL::i2cWrite(255);
#endif
#endif
#if UI_DISPLAY_I2C_CHIPTYPE==1
HAL::i2cWrite( uid.outputMask);
HAL::i2cWrite(uid.outputMask>>8);
#endif
#endif
HAL::delayMilliseconds(duration);
}
#if BEEPER_TYPE==2
HAL::i2cStop();
#endif
#endif
#endif
}
bool UIMenuEntry::showEntry() const
{
bool ret = true;
uint8_t f,f2;
f = HAL::readFlashByte((const char*)&filter);
if(f!=0)
ret = (f & Printer::menuMode) != 0;
f2 = HAL::readFlashByte((const char*)&nofilter);
if(ret && f2!=0)
{
ret = (f2 & Printer::menuMode) == 0;
}
return ret;
}
#if UI_DISPLAY_TYPE!=0
UIDisplay uid;
char displayCache[UI_ROWS][MAX_COLS+1];
// Menu up sign - code 1
// ..*.. 4
// .***. 14
// *.*.* 21
// ..*.. 4
// ***.. 28
// ..... 0
// ..... 0
// ..... 0
const uint8_t character_back[8] PROGMEM = {4,14,21,4,28,0,0,0};
// Degrees sign - code 2
// ..*.. 4
// .*.*. 10
// ..*.. 4
// ..... 0
// ..... 0
// ..... 0
// ..... 0
// ..... 0
const uint8_t character_degree[8] PROGMEM = {4,10,4,0,0,0,0,0};
// selected - code 3
// ..... 0
// ***** 31
// ***** 31
// ***** 31
// ***** 31
// ***** 31
// ***** 31
// ..... 0
// ..... 0
const uint8_t character_selected[8] PROGMEM = {0,31,31,31,31,31,0,0};
// unselected - code 4
// ..... 0
// ***** 31
// *...* 17
// *...* 17
// *...* 17
// *...* 17
// ***** 31
// ..... 0
// ..... 0
const uint8_t character_unselected[8] PROGMEM = {0,31,17,17,17,31,0,0};
// unselected - code 5
// ..*.. 4
// .*.*. 10
// .*.*. 10
// .*.*. 10
// .*.*. 10
// .***. 14
// ***** 31
// ***** 31
// .***. 14
const uint8_t character_temperature[8] PROGMEM = {4,10,10,10,14,31,31,14};
// unselected - code 6
// ..... 0
// ***.. 28
// ***** 31
// *...* 17
// *...* 17
// ***** 31
// ..... 0
// ..... 0
const uint8_t character_folder[8] PROGMEM = {0,28,31,17,17,31,0,0};
// printer ready - code 7
// *...* 17
// .*.*. 10
// ..*.. 4
// *...* 17
// ..*.. 4
// .*.*. 10
// *...* 17
// *...* 17
const byte character_ready[8] PROGMEM = {17,10,4,17,4,10,17,17};
const long baudrates[] PROGMEM = {9600,14400,19200,28800,38400,56000,57600,76800,111112,115200,128000,230400,250000,256000,
460800,500000,921600,1000000,1500000,0
};
#define LCD_ENTRYMODE 0x04 /**< Set entrymode */
/** @name GENERAL COMMANDS */
/*@{*/
#define LCD_CLEAR 0x01 /**< Clear screen */
#define LCD_HOME 0x02 /**< Cursor move to first digit */
/*@}*/
/** @name ENTRYMODES */
/*@{*/
#define LCD_ENTRYMODE 0x04 /**< Set entrymode */
#define LCD_INCREASE LCD_ENTRYMODE | 0x02 /**< Set cursor move direction -- Increase */
#define LCD_DECREASE LCD_ENTRYMODE | 0x00 /**< Set cursor move direction -- Decrease */
#define LCD_DISPLAYSHIFTON LCD_ENTRYMODE | 0x01 /**< Display is shifted */
#define LCD_DISPLAYSHIFTOFF LCD_ENTRYMODE | 0x00 /**< Display is not shifted */
/*@}*/
/** @name DISPLAYMODES */
/*@{*/
#define LCD_DISPLAYMODE 0x08 /**< Set displaymode */
#define LCD_DISPLAYON LCD_DISPLAYMODE | 0x04 /**< Display on */
#define LCD_DISPLAYOFF LCD_DISPLAYMODE | 0x00 /**< Display off */
#define LCD_CURSORON LCD_DISPLAYMODE | 0x02 /**< Cursor on */
#define LCD_CURSOROFF LCD_DISPLAYMODE | 0x00 /**< Cursor off */
#define LCD_BLINKINGON LCD_DISPLAYMODE | 0x01 /**< Blinking on */
#define LCD_BLINKINGOFF LCD_DISPLAYMODE | 0x00 /**< Blinking off */
/*@}*/
/** @name SHIFTMODES */
/*@{*/
#define LCD_SHIFTMODE 0x10 /**< Set shiftmode */
#define LCD_DISPLAYSHIFT LCD_SHIFTMODE | 0x08 /**< Display shift */
#define LCD_CURSORMOVE LCD_SHIFTMODE | 0x00 /**< Cursor move */
#define LCD_RIGHT LCD_SHIFTMODE | 0x04 /**< Right shift */
#define LCD_LEFT LCD_SHIFTMODE | 0x00 /**< Left shift */
/*@}*/
/** @name DISPLAY_CONFIGURATION */
/*@{*/
#define LCD_CONFIGURATION 0x20 /**< Set function */
#define LCD_8BIT LCD_CONFIGURATION | 0x10 /**< 8 bits interface */
#define LCD_4BIT LCD_CONFIGURATION | 0x00 /**< 4 bits interface */
#define LCD_2LINE LCD_CONFIGURATION | 0x08 /**< 2 line display */
#define LCD_1LINE LCD_CONFIGURATION | 0x00 /**< 1 line display */
#define LCD_5X10 LCD_CONFIGURATION | 0x04 /**< 5 X 10 dots */
#define LCD_5X7 LCD_CONFIGURATION | 0x00 /**< 5 X 7 dots */
#define LCD_SETCGRAMADDR 0x40
#define lcdPutChar(value) lcdWriteByte(value,1)
#define lcdCommand(value) lcdWriteByte(value,0)
static const uint8_t LCDLineOffsets[] PROGMEM = UI_LINE_OFFSETS;
static const char versionString[] PROGMEM = UI_VERSION_STRING;
#if UI_DISPLAY_TYPE==3
// ============= I2C LCD Display driver ================
inline void lcdStartWrite()
{
HAL::i2cStartWait(UI_DISPLAY_I2C_ADDRESS+I2C_WRITE);
#if UI_DISPLAY_I2C_CHIPTYPE==1
HAL::i2cWrite( 0x14); // Start at port a
#endif
}
inline void lcdStopWrite()
{
HAL::i2cStop();
}
void lcdWriteNibble(uint8_t value)
{
#if UI_DISPLAY_I2C_CHIPTYPE==0
value|=uid.outputMask;
#if UI_DISPLAY_D4_PIN==1 && UI_DISPLAY_D5_PIN==2 && UI_DISPLAY_D6_PIN==4 && UI_DISPLAY_D7_PIN==8
HAL::i2cWrite((value) | UI_DISPLAY_ENABLE_PIN);
HAL::i2cWrite(value);
#else
uint8_t v=(value & 1?UI_DISPLAY_D4_PIN:0)|(value & 2?UI_DISPLAY_D5_PIN:0)|(value & 4?UI_DISPLAY_D6_PIN:0)|(value & 8?UI_DISPLAY_D7_PIN:0);
HAL::i2cWrite((v) | UI_DISPLAY_ENABLE_PIN);
HAL::i2cWrite(v);
#
#endif
#endif
#if UI_DISPLAY_I2C_CHIPTYPE==1
unsigned int v=(value & 1?UI_DISPLAY_D4_PIN:0)|(value & 2?UI_DISPLAY_D5_PIN:0)|(value & 4?UI_DISPLAY_D6_PIN:0)|(value & 8?UI_DISPLAY_D7_PIN:0) | uid.outputMask;
unsigned int v2 = v | UI_DISPLAY_ENABLE_PIN;
HAL::i2cWrite(v2 & 255);
HAL::i2cWrite(v2 >> 8);
HAL::i2cWrite(v & 255);
HAL::i2cWrite(v >> 8);
#endif
}
void lcdWriteByte(uint8_t c,uint8_t rs)
{
#if UI_DISPLAY_I2C_CHIPTYPE==0
uint8_t mod = (rs?UI_DISPLAY_RS_PIN:0) | uid.outputMask; // | (UI_DISPLAY_RW_PIN);
#if UI_DISPLAY_D4_PIN==1 && UI_DISPLAY_D5_PIN==2 && UI_DISPLAY_D6_PIN==4 && UI_DISPLAY_D7_PIN==8
uint8_t value = (c >> 4) | mod;
HAL::i2cWrite((value) | UI_DISPLAY_ENABLE_PIN);
HAL::i2cWrite(value);
value = (c & 15) | mod;
HAL::i2cWrite((value) | UI_DISPLAY_ENABLE_PIN);
HAL::i2cWrite(value);
#else
uint8_t value = (c & 16?UI_DISPLAY_D4_PIN:0)|(c & 32?UI_DISPLAY_D5_PIN:0)|(c & 64?UI_DISPLAY_D6_PIN:0)|(c & 128?UI_DISPLAY_D7_PIN:0) | mod;
HAL::i2cWrite((value) | UI_DISPLAY_ENABLE_PIN);
HAL::i2cWrite(value);
value = (c & 1?UI_DISPLAY_D4_PIN:0)|(c & 2?UI_DISPLAY_D5_PIN:0)|(c & 4?UI_DISPLAY_D6_PIN:0)|(c & 8?UI_DISPLAY_D7_PIN:0) | mod;
HAL::i2cWrite((value) | UI_DISPLAY_ENABLE_PIN);
HAL::i2cWrite(value);
#endif
#endif
#if UI_DISPLAY_I2C_CHIPTYPE==1
unsigned int mod = (rs?UI_DISPLAY_RS_PIN:0) | uid.outputMask; // | (UI_DISPLAY_RW_PIN);
unsigned int value = (c & 16?UI_DISPLAY_D4_PIN:0)|(c & 32?UI_DISPLAY_D5_PIN:0)|(c & 64?UI_DISPLAY_D6_PIN:0)|(c & 128?UI_DISPLAY_D7_PIN:0) | mod;
unsigned int value2 = (value) | UI_DISPLAY_ENABLE_PIN;
HAL::i2cWrite(value2 & 255);
HAL::i2cWrite(value2 >>8);
HAL::i2cWrite(value & 255);
HAL::i2cWrite(value>>8);
value = (c & 1?UI_DISPLAY_D4_PIN:0)|(c & 2?UI_DISPLAY_D5_PIN:0)|(c & 4?UI_DISPLAY_D6_PIN:0)|(c & 8?UI_DISPLAY_D7_PIN:0) | mod;
value2 = (value) | UI_DISPLAY_ENABLE_PIN;
HAL::i2cWrite(value2 & 255);
HAL::i2cWrite(value2 >>8);
HAL::i2cWrite(value & 255);
HAL::i2cWrite(value>>8);
#endif
}
void initializeLCD()
{
HAL::delayMilliseconds(235);
lcdStartWrite();
HAL::i2cWrite(uid.outputMask & 255);
#if UI_DISPLAY_I2C_CHIPTYPE==1
HAL::i2cWrite(uid.outputMask >> 16);
#endif
HAL::delayMicroseconds(10);
lcdWriteNibble(0x03);
HAL::delayMicroseconds(5000); // I have one LCD for which 4500 here was not long enough.
// second try
lcdWriteNibble(0x03);
HAL::delayMicroseconds(150); // wait
// third go!
lcdWriteNibble(0x03);
HAL::delayMicroseconds(150);
// finally, set to 4-bit interface
lcdWriteNibble(0x02);
HAL::delayMicroseconds(150);
// finally, set # lines, font size, etc.
lcdCommand(LCD_4BIT | LCD_2LINE | LCD_5X7);
lcdCommand(LCD_CLEAR); //- Clear Screen
HAL::delayMilliseconds(2); // clear is slow operation
lcdCommand(LCD_INCREASE | LCD_DISPLAYSHIFTOFF); //- Entrymode (Display Shift: off, Increment Address Counter)
lcdCommand(LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKINGOFF); //- Display on
uid.lastSwitch = uid.lastRefresh = HAL::timeInMilliseconds();
uid.createChar(1,character_back);
uid.createChar(2,character_degree);
uid.createChar(3,character_selected);
uid.createChar(4,character_unselected);
uid.createChar(5,character_temperature);
uid.createChar(6,character_folder);
uid.createChar(7,character_ready);
lcdStopWrite();
}
#endif
#if UI_DISPLAY_TYPE==1 || UI_DISPLAY_TYPE==2
void lcdWriteNibble(uint8_t value)
{
WRITE(UI_DISPLAY_D4_PIN,value & 1);
WRITE(UI_DISPLAY_D5_PIN,value & 2);
WRITE(UI_DISPLAY_D6_PIN,value & 4);
WRITE(UI_DISPLAY_D7_PIN,value & 8);
WRITE(UI_DISPLAY_ENABLE_PIN, HIGH);// enable pulse must be >450ns
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
WRITE(UI_DISPLAY_ENABLE_PIN, LOW);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
}
void lcdWriteByte(uint8_t c,uint8_t rs)
{
#if UI_DISPLAY_RW_PIN<0
HAL::delayMicroseconds(UI_DELAYPERCHAR);
#else
SET_INPUT(UI_DISPLAY_D4_PIN);
SET_INPUT(UI_DISPLAY_D5_PIN);
SET_INPUT(UI_DISPLAY_D6_PIN);
SET_INPUT(UI_DISPLAY_D7_PIN);
WRITE(UI_DISPLAY_RW_PIN, HIGH);
WRITE(UI_DISPLAY_RS_PIN, LOW);
uint8_t busy;
do
{
WRITE(UI_DISPLAY_ENABLE_PIN, HIGH);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
busy = READ(UI_DISPLAY_D7_PIN);
WRITE(UI_DISPLAY_ENABLE_PIN, LOW);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
WRITE(UI_DISPLAY_ENABLE_PIN, HIGH);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
WRITE(UI_DISPLAY_ENABLE_PIN, LOW);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
}
while (busy);
SET_OUTPUT(UI_DISPLAY_D4_PIN);
SET_OUTPUT(UI_DISPLAY_D5_PIN);
SET_OUTPUT(UI_DISPLAY_D6_PIN);
SET_OUTPUT(UI_DISPLAY_D7_PIN);
WRITE(UI_DISPLAY_RW_PIN, LOW);
#endif
WRITE(UI_DISPLAY_RS_PIN, rs);
WRITE(UI_DISPLAY_D4_PIN, c & 0x10);
WRITE(UI_DISPLAY_D5_PIN, c & 0x20);
WRITE(UI_DISPLAY_D6_PIN, c & 0x40);
WRITE(UI_DISPLAY_D7_PIN, c & 0x80);
WRITE(UI_DISPLAY_ENABLE_PIN, HIGH); // enable pulse must be >450ns
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
WRITE(UI_DISPLAY_ENABLE_PIN, LOW);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
WRITE(UI_DISPLAY_D4_PIN, c & 0x01);
WRITE(UI_DISPLAY_D5_PIN, c & 0x02);
WRITE(UI_DISPLAY_D6_PIN, c & 0x04);
WRITE(UI_DISPLAY_D7_PIN, c & 0x08);
WRITE(UI_DISPLAY_ENABLE_PIN, HIGH); // enable pulse must be >450ns
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
WRITE(UI_DISPLAY_ENABLE_PIN, LOW);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
}
void initializeLCD()
{
// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
// according to datasheet, we need at least 40ms after power rises above 2.7V
// before sending commands. Arduino can turn on way before 4.5V.
// is this delay long enough for all cases??
HAL::delayMilliseconds(235);
SET_OUTPUT(UI_DISPLAY_D4_PIN);
SET_OUTPUT(UI_DISPLAY_D5_PIN);
SET_OUTPUT(UI_DISPLAY_D6_PIN);
SET_OUTPUT(UI_DISPLAY_D7_PIN);
SET_OUTPUT(UI_DISPLAY_RS_PIN);
#if UI_DISPLAY_RW_PIN>-1
SET_OUTPUT(UI_DISPLAY_RW_PIN);
#endif
SET_OUTPUT(UI_DISPLAY_ENABLE_PIN);
// Now we pull both RS and R/W low to begin commands
WRITE(UI_DISPLAY_RS_PIN, LOW);
WRITE(UI_DISPLAY_ENABLE_PIN, LOW);
//put the LCD into 4 bit mode
// this is according to the hitachi HD44780 datasheet
// figure 24, pg 46
// we start in 8bit mode, try to set 4 bit mode
// at this point we are in 8 bit mode but of course in this
// interface 4 pins are dangling unconnected and the values
// on them don't matter for these instructions.
WRITE(UI_DISPLAY_RS_PIN, LOW);
HAL::delayMicroseconds(10);
lcdWriteNibble(0x03);
HAL::delayMicroseconds(5000); // I have one LCD for which 4500 here was not long enough.
// second try
lcdWriteNibble(0x03);
HAL::delayMicroseconds(150); // wait
// third go!
lcdWriteNibble(0x03);
HAL::delayMicroseconds(150);
// finally, set to 4-bit interface
lcdWriteNibble(0x02);
HAL::delayMicroseconds(150);
// finally, set # lines, font size, etc.
lcdCommand(LCD_4BIT | LCD_2LINE | LCD_5X7);
lcdCommand(LCD_CLEAR); //- Clear Screen
HAL::delayMilliseconds(2); // clear is slow operation
lcdCommand(LCD_INCREASE | LCD_DISPLAYSHIFTOFF); //- Entrymode (Display Shift: off, Increment Address Counter)
lcdCommand(LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKINGOFF); //- Display on
uid.lastSwitch = uid.lastRefresh = HAL::timeInMilliseconds();
uid.createChar(1,character_back);
uid.createChar(2,character_degree);
uid.createChar(3,character_selected);
uid.createChar(4,character_unselected);
uid.createChar(5,character_temperature);
uid.createChar(6,character_folder);
uid.createChar(7,character_ready);
}
// ----------- end direct LCD driver
#endif
#if UI_DISPLAY_TYPE<4
void UIDisplay::printRow(uint8_t r,char *txt,char *txt2,uint8_t changeAtCol)
{
changeAtCol = RMath::min(UI_COLS,changeAtCol);
uint8_t col=0;
// Set row
if(r >= UI_ROWS) return;
#if UI_DISPLAY_TYPE==3
lcdStartWrite();
#endif
lcdWriteByte(128 + HAL::readFlashByte((const char *)&LCDLineOffsets[r]),0); // Position cursor
char c;
while((c=*txt) != 0x00 && col<changeAtCol)
{
txt++;
lcdPutChar(c);
col++;
}
while(col<changeAtCol)
{
lcdPutChar(' ');
col++;
}
if(txt2!=NULL)
{
while((c=*txt2) != 0x00 && col<UI_COLS)
{
txt2++;
lcdPutChar(c);
col++;
}
while(col<UI_COLS)
{
lcdPutChar(' ');
col++;
}
}
#if UI_DISPLAY_TYPE==3
lcdStopWrite();
#endif
#if UI_HAS_KEYS==1 && UI_HAS_I2C_ENCODER>0
ui_check_slow_encoder();
#endif
}
#endif
#if UI_DISPLAY_TYPE==4
// Use LiquidCrystal library instead
#include <LiquidCrystal.h>
LiquidCrystal lcd(UI_DISPLAY_RS_PIN, UI_DISPLAY_RW_PIN,UI_DISPLAY_ENABLE_PIN,UI_DISPLAY_D4_PIN,UI_DISPLAY_D5_PIN,UI_DISPLAY_D6_PIN,UI_DISPLAY_D7_PIN);
void UIDisplay::createChar(uint8_t location,const uint8_t charmap[])
{
location &= 0x7; // we only have 8 locations 0-7
uint8_t data[8];
for (int i=0; i<8; i++)
{
data[i]=pgm_read_byte(&(charmap[i]));
}
lcd.createChar(location, data);
}
void UIDisplay::printRow(uint8_t r,char *txt,char *txt2,uint8_t changeAtCol)
{
changeAtCol = RMath::min(UI_COLS,changeAtCol);
uint8_t col=0;
// Set row
if(r >= UI_ROWS) return;
lcd.setCursor(0,r);
char c;
while((c=*txt) != 0x00 && col<changeAtCol)
{
txt++;
lcd.write(c);
col++;
}
while(col<changeAtCol)
{
lcd.write(' ');
col++;
}
if(txt2!=NULL)
{
while((c=*txt2) != 0x00 && col<UI_COLS)
{
txt2++;
lcd.write(c);
col++;
}
while(col<UI_COLS)
{
lcd.write(' ');
col++;
}
}
#if UI_HAS_KEYS==1 && UI_HAS_I2C_ENCODER>0
ui_check_slow_encoder();
#endif
}
void initializeLCD()
{
lcd.begin(UI_COLS,UI_ROWS);
uid.lastSwitch = uid.lastRefresh = HAL::timeInMilliseconds();
uid.createChar(1,character_back);
uid.createChar(2,character_degree);
uid.createChar(3,character_selected);
uid.createChar(4,character_unselected);
}
// ------------------ End LiquidCrystal library as LCD driver
#endif // UI_DISPLAY_TYPE==4
#if UI_DISPLAY_TYPE==5
//u8glib
#ifdef U8GLIB_ST7920
#define UI_SPI_SCK UI_DISPLAY_D4_PIN
#define UI_SPI_MOSI UI_DISPLAY_ENABLE_PIN
#define UI_SPI_CS UI_DISPLAY_RS_PIN
#endif
#include "u8glib_ex.h"
u8g_t u8g;
u8g_uint_t u8_tx = 0, u8_ty = 0;
void u8PrintChar(char c)
{
switch(c)
{
case 0x7E: // right arrow
u8g_SetFont(&u8g, u8g_font_6x12_67_75);
u8_tx += u8g_DrawGlyph(&u8g, u8_tx, u8_ty, 0x52);
u8g_SetFont(&u8g, UI_FONT_DEFAULT);
break;
case CHAR_SELECTOR:
u8g_SetFont(&u8g, u8g_font_6x12_67_75);
u8_tx += u8g_DrawGlyph(&u8g, u8_tx, u8_ty, 0xb7);
u8g_SetFont(&u8g, UI_FONT_DEFAULT);
break;
case CHAR_SELECTED:
u8g_SetFont(&u8g, u8g_font_6x12_67_75);
u8_tx += u8g_DrawGlyph(&u8g, u8_tx, u8_ty, 0xb6);
u8g_SetFont(&u8g, UI_FONT_DEFAULT);
break;
default:
u8_tx += u8g_DrawGlyph(&u8g, u8_tx, u8_ty, c);
}
}
void printU8GRow(uint8_t x,uint8_t y,char *text)
{
char c;
while((c = *(text++)) != 0)
x += u8g_DrawGlyph(&u8g,x,y,c);
}
void UIDisplay::printRow(uint8_t r,char *txt,char *txt2,uint8_t changeAtCol)
{
changeAtCol = RMath::min(UI_COLS,changeAtCol);
uint8_t col=0;
// Set row
if(r >= UI_ROWS) return;
int y = r*UI_FONT_HEIGHT;
if(!u8g_IsBBXIntersection(&u8g,0,y,UI_LCD_WIDTH,UI_FONT_HEIGHT+2)) return; // row not visible
u8_tx = 0;
u8_ty = y+UI_FONT_HEIGHT; //set position
bool highlight = ((uint8_t)(*txt) == CHAR_SELECTOR) || ((uint8_t)(*txt) == CHAR_SELECTED);
if(highlight)
{
u8g_SetColorIndex(&u8g,1);
u8g_draw_box(&u8g, 0, y+1, u8g_GetWidth(&u8g), UI_FONT_HEIGHT+1);
u8g_SetColorIndex(&u8g,0);
}
char c;
while((c = *(txt++)) != 0 && col < changeAtCol)
{
u8PrintChar(c);
col++;
}
if(txt2 != NULL)
{
col = changeAtCol;
u8_tx = col*UI_FONT_WIDTH; //set position
while((c=*(txt2++)) != 0 && col < UI_COLS)
{
u8PrintChar(c);
col++;
}
}
if(highlight)
{
u8g_SetColorIndex(&u8g,1);
}
#if UI_HAS_KEYS==1 && UI_HAS_I2C_ENCODER>0
ui_check_slow_encoder();
#endif
}
void initializeLCD()
{
#ifdef U8GLIB_ST7920
//U8GLIB_ST7920_128X64_1X u8g(UI_DISPLAY_D4_PIN, UI_DISPLAY_ENABLE_PIN, UI_DISPLAY_RS_PIN);
u8g_InitSPI(&u8g,&u8g_dev_st7920_128x64_sw_spi, UI_DISPLAY_D4_PIN, UI_DISPLAY_ENABLE_PIN, UI_DISPLAY_RS_PIN, U8G_PIN_NONE, U8G_PIN_NONE);
#endif
u8g_Begin(&u8g);
//u8g.firstPage();
u8g_FirstPage(&u8g);
do
{
u8g_SetColorIndex(&u8g, 0);
}
while( u8g_NextPage(&u8g) );
u8g_SetFont(&u8g, UI_FONT_DEFAULT);
u8g_SetColorIndex(&u8g, 1);
uid.lastSwitch = uid.lastRefresh = HAL::timeInMilliseconds();
}
// ------------------ End u8GLIB library as LCD driver
#endif // UI_DISPLAY_TYPE==5
char printCols[MAX_COLS+1];
UIDisplay::UIDisplay()
{
}
#if UI_ANIMATION
void slideIn(uint8_t row,FSTRINGPARAM(text))
{
char *empty="";
int8_t i = 0;
uid.col=0;
uid.addStringP(text);
printCols[uid.col]=0;
for(i=UI_COLS-1; i>=0; i--)
{
uid.printRow(row,empty,printCols,i);
HAL::pingWatchdog();
HAL::delayMilliseconds(10);
}
}
#endif // UI_ANIMATION
void UIDisplay::initialize()
{
oldMenuLevel = -2;
#ifdef COMPILE_I2C_DRIVER
uid.outputMask = UI_DISPLAY_I2C_OUTPUT_START_MASK;
#if UI_DISPLAY_I2C_CHIPTYPE==0 && BEEPER_TYPE==2 && BEEPER_PIN>=0
#if BEEPER_ADDRESS == UI_DISPLAY_I2C_ADDRESS
uid.outputMask |= BEEPER_PIN;
#endif
#endif
HAL::i2cInit(UI_I2C_CLOCKSPEED);
#if UI_DISPLAY_I2C_CHIPTYPE==1
// set direction of pins
HAL::i2cStart(UI_DISPLAY_I2C_ADDRESS+I2C_WRITE);
HAL::i2cWrite(0); // IODIRA
HAL::i2cWrite(~(UI_DISPLAY_I2C_OUTPUT_PINS & 255));
HAL::i2cWrite(~(UI_DISPLAY_I2C_OUTPUT_PINS >> 8));
HAL::i2cStop();
// Set pullups according to UI_DISPLAY_I2C_PULLUP
HAL::i2cStart(UI_DISPLAY_I2C_ADDRESS+I2C_WRITE);
HAL::i2cWrite(0x0C); // GPPUA
HAL::i2cWrite(UI_DISPLAY_I2C_PULLUP & 255);
HAL::i2cWrite(UI_DISPLAY_I2C_PULLUP >> 8);
HAL::i2cStop();
#endif
#endif
flags = 0;
menuLevel = 0;
shift = -2;
menuPos[0] = 0;
lastAction = 0;
lastButtonAction = 0;
activeAction = 0;
statusMsg[0] = 0;
ui_init_keys();
#if SDSUPPORT
cwd[0]='/';
cwd[1]=0;
folderLevel=0;
#endif
UI_STATUS(UI_TEXT_PRINTER_READY);
#if UI_DISPLAY_TYPE>0
initializeLCD();
#if UI_DISPLAY_TYPE==3
// I don't know why but after power up the lcd does not come up
// but if I reinitialize i2c and the lcd again here it works.
HAL::delayMilliseconds(10);
HAL::i2cInit(UI_I2C_CLOCKSPEED);
initializeLCD();
#endif
#if UI_ANIMATION==false || UI_DISPLAY_TYPE==5
#if UI_DISPLAY_TYPE == 5
//u8g picture loop
u8g_FirstPage(&u8g);
do
{
#endif
for(uint8_t y=0; y<UI_ROWS; y++) displayCache[y][0] = 0;
printRowP(0, versionString);
printRowP(1, PSTR(UI_PRINTER_NAME));
#if UI_ROWS>2
printRowP(UI_ROWS-1, PSTR(UI_PRINTER_COMPANY));
#endif
#if UI_DISPLAY_TYPE == 5
}
while( u8g_NextPage(&u8g) ); //end picture loop
#endif
#else
slideIn(0, versionString);
strcpy(displayCache[0], printCols);
slideIn(1, PSTR(UI_PRINTER_NAME));
strcpy(displayCache[1], printCols);
#if UI_ROWS>2
slideIn(UI_ROWS-1, PSTR(UI_PRINTER_COMPANY));
strcpy(displayCache[UI_ROWS-1], printCols);
#endif
#endif
HAL::delayMilliseconds(UI_START_SCREEN_DELAY);
#endif
#if UI_DISPLAY_I2C_CHIPTYPE==0 && (BEEPER_TYPE==2 || defined(UI_HAS_I2C_KEYS))
// Make sure the beeper is off
HAL::i2cStartWait(UI_I2C_KEY_ADDRESS+I2C_WRITE);
HAL::i2cWrite(255); // Disable beeper, enable read for other pins.
HAL::i2cStop();
#endif
}
#if UI_DISPLAY_TYPE==1 || UI_DISPLAY_TYPE==2 || UI_DISPLAY_TYPE==3
void UIDisplay::createChar(uint8_t location,const uint8_t PROGMEM charmap[])
{
location &= 0x7; // we only have 8 locations 0-7
lcdCommand(LCD_SETCGRAMADDR | (location << 3));
for (int i=0; i<8; i++)
{
lcdPutChar(pgm_read_byte(&(charmap[i])));
}
}
#endif
void UIDisplay::waitForKey()
{
int nextAction = 0;
lastButtonAction = 0;
while(lastButtonAction==nextAction)
{
ui_check_slow_keys(nextAction);
}
}
void UIDisplay::printRowP(uint8_t r,PGM_P txt)
{
if(r >= UI_ROWS) return;
col=0;
addStringP(txt);
printCols[col]=0;
printRow(r,printCols,NULL,UI_COLS);
}
void UIDisplay::addInt(int value,uint8_t digits,char fillChar)
{
uint8_t dig=0,neg=0;
if(value<0)
{
value = -value;
neg=1;
dig++;
}
char buf[7]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[6];
buf[6]=0;
do
{
unsigned int m = value;
value /= 10;
char c = m - 10 * value;
*--str = c + '0';
dig++;
}
while(value);
if(neg)
printCols[col++]='-';
if(digits<6)
while(dig<digits)
{
*--str = fillChar; //' ';
dig++;
}
while(*str && col<MAX_COLS)
{
printCols[col++] = *str;
str++;
}
}
void UIDisplay::addLong(long value,char digits)
{
uint8_t dig = 0,neg=0;
if(value<0)
{
neg=1;
value = -value;
dig++;
}
char buf[13]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[12];
buf[12]=0;
do
{
unsigned long m = value;
value /= 10;
char c = m - 10 * value;
*--str = c + '0';
dig++;
}
while(value);
if(neg)
printCols[col++]='-';
if(digits<=11)
while(dig<digits)
{
*--str = ' ';
dig++;
}
while(*str && col<MAX_COLS)
{
printCols[col++] = *str;
str++;
}
}
const float roundingTable[] PROGMEM = {0.5,0.05,0.005,0.0005};
void UIDisplay::addFloat(float number, char fixdigits,uint8_t digits)
{
// Handle negative numbers
if (number < 0.0)
{
printCols[col++]='-';
if(col>=MAX_COLS) return;
number = -number;
fixdigits--;
}
number += pgm_read_float(&roundingTable[digits]); // for correct rounding
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
float remainder = number - (float)int_part;
addLong(int_part,fixdigits);
if(col>=UI_COLS) return;
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
{
printCols[col++]='.';
}
// Extract digits from the remainder one at a time
while (col<MAX_COLS && digits-- > 0)
{
remainder *= 10.0;
uint8_t toPrint = uint8_t(remainder);
printCols[col++] = '0'+toPrint;
remainder -= toPrint;
}
}
void UIDisplay::addStringP(FSTRINGPARAM(text))
{
while(col<MAX_COLS)
{
uint8_t c = HAL::readFlashByte(text++);
if(c==0) return;
printCols[col++]=c;
}
}
UI_STRING(ui_text_on,UI_TEXT_ON);
UI_STRING(ui_text_off,UI_TEXT_OFF);
UI_STRING(ui_text_na,UI_TEXT_NA);
UI_STRING(ui_yes,UI_TEXT_YES);
UI_STRING(ui_no,UI_TEXT_NO);
UI_STRING(ui_print_pos,UI_TEXT_PRINT_POS);
UI_STRING(ui_selected,UI_TEXT_SEL);
UI_STRING(ui_unselected,UI_TEXT_NOSEL);
UI_STRING(ui_action,UI_TEXT_STRING_ACTION);
void UIDisplay::parse(char *txt,bool ram)
{
int ivalue=0;
float fvalue=0;
while(col<MAX_COLS)
{
char c=(ram ? *(txt++) : pgm_read_byte(txt++));
if(c==0) break; // finished
if(c!='%')
{
printCols[col++]=c;
continue;
}
// dynamic parameter, parse meaning and replace
char c1=(ram ? *(txt++) : pgm_read_byte(txt++));
char c2=(ram ? *(txt++) : pgm_read_byte(txt++));
switch(c1)