-
Notifications
You must be signed in to change notification settings - Fork 38
/
hakko_t12.ino
1788 lines (1666 loc) · 65.4 KB
/
hakko_t12.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
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
/*
* Soldering IRON controller for hakko t12 tips built on atmega328 microcontroller running 16 MHz
* The controller is using interrupts from the Timer1 to generate high-frequence PWM signal on port D10
* to silently heat the IRON and periodically check the IRON temperature on overflow interrupts
* Timer1 runs with prescale 1 through 0 to 255 and back and its frequency is 31250 Hz.
* The owerflow interrupt running as folows:
* First, the current through the IRON is checked
* then the power, supplien to the IRON interrupted and the controller waits for 32 timer interrupts (about 1 ms)
* then the IRON temperature is checked and the power to the IRON restored
* then the controller waits for check_period Timer1 interrupts to restart the all procedure over again
*/
// Edit the configuration file to select appropriate display type
#include <EEPROM.h>
#include "config.h"
#include "encoder.h"
#include "cfg.h"
#include "iron_tips.h"
#include "vars.h"
// Rotary encoder interface
const uint8_t R_MAIN_PIN = 2; // Rotary Encoder main pin (right)
const uint8_t R_SECD_PIN = 4; // Rotary Encoder second pin (left)
const uint8_t R_BUTN_PIN = 3; // Rotary Encoder push button pin
const uint8_t probePIN = A0; // Thermometer pin from soldering IRON
const uint8_t checkPIN = A1; // Iron current check pin
const uint8_t termisPIN = A2; // The thermistor pin to check ambient temperature
const uint8_t tiltswPIN = A3; // The tilt/reed/vibro switch pin
const uint8_t buzzerPIN = 11; // The simple buzzer to make a noise
const uint8_t heaterBIT = 0b00000100; // The Heater pin, D10, is 2-nd bit on the PORTB register
// The variables for Timer1 operations
volatile uint16_t tmr1_count; // The count to calculate the temperature and the current check periods
volatile bool iron_off; // Whether the IRON is switched off to check the temperature
const uint32_t temp_check_period = 20; // The IRON temperature check period, ms
//------------------------------------------ class BUZZER ------------------------------------------------------
class BUZZER {
public:
BUZZER(uint8_t buzzerP) { buzzer_pin = buzzerP; }
void activate(bool on) { this->on = on; }
void init(void) { pinMode(buzzer_pin, OUTPUT); noTone(buzzer_pin); }
void shortBeep(void) { if (on) tone(buzzer_pin, 3520, 160); }
void lowBeep(void) { if (on) tone(buzzer_pin, 880, 160); }
void doubleBeep(void) { if (on) { tone(buzzer_pin, 3520, 160); delay(300); tone(buzzer_pin, 3520, 160); } }
void failedBeep(void) { if (on) { tone(buzzer_pin, 3520, 160); delay(170);
tone(buzzer_pin, 880, 250); delay(260);
tone(buzzer_pin, 3520, 160); }
}
private:
uint8_t buzzer_pin;
bool on = true;
};
//------------------------------------------ class PID algoritm to keep the temperature -----------------------
/* The PID algorithm
* Un = Kp*(Xs - Xn) + Ki*summ{j=0; j<=n}(Xs - Xj) + Kd(Xn - Xn-1),
* Where Xs - is the setup temperature, Xn - the temperature on n-iteration step
* In this program the interactive formula is used:
* Un = Un-1 + Kp*(Xn-1 - Xn) + Ki*(Xs - Xn) + Kd*(Xn-2 + Xn - 2*Xn-1)
* With the first step:
* U0 = Kp*(Xs - X0) + Ki*(Xs - X0); Xn-1 = Xn;
*
* PID coefficients history:
* 10/14/2017 [768, 32, 328]
*/
class PID {
public:
PID(void) {
Kp = 2009;
Ki = 16;
Kd = 2048;
}
void resetPID(int temp = -1); // reset PID algorithm history parameters
// Calculate the power to be applied
int32_t reqPower(int temp_set, int temp_curr);
int16_t changePID(uint8_t p, int k); // set or get (if parameter < 0) PID parameter
private:
void debugPID(int t_set, int t_curr, int32_t kp, int32_t ki, int32_t kd, int32_t delta_p);
int16_t temp_h0, temp_h1; // previously measured temperature
bool pid_iterate; // Whether the iterative process is used
int32_t i_summ; // Ki summary multiplied by denominator
int32_t power; // The power iterative multiplied by denominator
int32_t Kp, Ki, Kd; // The PID algorithm coefficients multiplied by denominator
const uint8_t denominator_p = 11; // The common coefficient denominator power of 2 (11 means divide by 2048)
};
void PID::resetPID(int temp) {
temp_h0 = 0;
power = 0;
i_summ = 0;
pid_iterate = false;
if ((temp > 0) && (temp < 1000))
temp_h1 = temp;
else
temp_h1 = 0;
}
int16_t PID::changePID(uint8_t p, int k) {
switch(p) {
case 1:
if (k >= 0) Kp = k;
return Kp;
case 2:
if (k >= 0) Ki = k;
return Ki;
case 3:
if (k >= 0) Kd = k;
return Kd;
default:
break;
}
return 0;
}
int32_t PID::reqPower(int temp_set, int temp_curr) {
if (temp_h0 == 0) {
// When the temperature is near the preset one, reset the PID and prepare iterative formula
if ((temp_set - temp_curr) < 30) {
if (!pid_iterate) {
pid_iterate = true;
power = 0;
i_summ = 0;
}
}
i_summ += temp_set - temp_curr; // first, use the direct formula, not the iterate process
power = Kp*(temp_set - temp_curr) + Ki*i_summ;
// If the temperature is near, prepare the PID iteration process
} else {
int32_t kp = Kp * (temp_h1 - temp_curr);
int32_t ki = Ki * (temp_set - temp_curr);
int32_t kd = Kd * (temp_h0 + temp_curr - 2*temp_h1);
int32_t delta_p = kp + ki + kd;
power += delta_p; // power kept multiplied by denominator!
}
if (pid_iterate) temp_h0 = temp_h1;
temp_h1 = temp_curr;
int32_t pwr = power + (1 << (denominator_p-1)); // prepare the power to delete by denominator, round the result
pwr >>= denominator_p; // delete by the denominator
return pwr;
}
//------------------------- class FastPWM operations using Timer1 on pin D10 at 31250 Hz ----------------------
class FastPWM {
public:
FastPWM() { }
void init(void);
void duty(uint8_t d) { OCR1B = d; }
void off(void) { OCR1B = 0; PORTB &= ~heaterBIT; }
};
void FastPWM::init(void) {
pinMode(10, OUTPUT); // Use D10 pin for heating the IRON
PORTB &= ~heaterBIT; // Switch-off the power
tmr1_count = 0;
iron_off = false; // The volatile global variable
noInterrupts();
TCNT1 = 0;
TCCR1B = _BV(WGM13); // Set mode as phase and frequency correct pwm, stop the timer
TCCR1A = 0;
ICR1 = 256;
TCCR1B = _BV(WGM13) | _BV(CS10); // Top value = ICR1, prescale = 1; 31250 Hz
TCCR1A |= _BV(COM1B1); // XOR D10 on OC1B, detached from D09
OCR1B = 0; // Switch-off the signal on pin D10;
TIMSK1 = _BV(TOIE1); // Enable overflow interrupts @31250 Hz
interrupts();
}
//------------------------------------------ class soldering IRON ---------------------------------------------
class IRON : protected PID {
public:
IRON(uint8_t sensor_pin, uint8_t check_pin, uint8_t ambient_pin, uint8_t tilt_pin) {
sPIN = sensor_pin;
cPIN = check_pin;
aPIN = ambient_pin;
tPIN = tilt_pin;
h_counter = h_max_counter;
}
typedef enum { POWER_OFF, POWER_ON, POWER_FIXED, POWER_COOLING } PowerMode;
void init(void);
void switchPower(bool On);
bool isOn(void);
uint16_t presetTemp(void) { return temp_set; }
uint16_t currTemp(void) { return h_temp.read(); }
uint16_t tempAverage(void) { return h_temp.average(); }
uint16_t tempDispersion(void) { return h_temp.dispersion(); }
uint16_t powerDispersion(void) { return h_power.dispersion(); }
uint8_t getMaxFixedPower(void) { return max_fixed_power; }
int16_t changePID(uint8_t p, int k) { return PID::changePID(p, k); }
bool checkIron(void); // Check the IRON, return true if the iron is not connected
void keepTemp(void); // Keep the IRON temperature, called by Timer1 interrupt
uint8_t appliedPower(void); // Power applied to the IRON in percents
void setTemp(uint16_t t); // Set the temperature to be kept (internal units)
void lowPowerMode(uint16_t t); // Activate low power mode (setup temp. or 0 to return to standard mode)
uint8_t getAvgPower(void); // Average applied power
void fixPower(uint8_t Power); // Set the specified power to the the soldering IRON
void initTempHistory(void) { h_counter = h_max_counter; h_temp.init(); mode = POWER_OFF; }
bool isCold(void) { return (mode == POWER_OFF); }
int16_t ambientTemp(void);
void adjust(uint16_t t);
bool isIronTiltSwitch(bool reed);
void checkSWStatus(void);
private:
FastPWM fastPWM; // Power the iron using fast PWM through D10 pin using Timer1
uint8_t sPIN, cPIN, aPIN, tPIN; // The sensor PIN, current check PIN, ambient temperature PIN, tilt switch PIN
uint16_t temp_set; // The temperature that should be kept
uint16_t temp_low; // Low power mode temperature
uint8_t fix_power = 0; // Fixed power value of the IRON (or zero if off)
uint32_t check_iron_ms = 0; // The time in ms when check the IRON next time
uint32_t check_tilt_ms = 0; // The time in ms when check the tilt switch next time
bool disconnected; // Whether no current through the IRON (the iron disconnected)
int h_counter; // Put the temperature and power to the history, when the counter become 0
uint8_t applied_power = 0; // The applied power to the IRON, used in checkIron()
volatile PowerMode mode = POWER_OFF; // Working mode of the IRON
volatile bool chill; // Whether the IRON should be cooled (preset temp is lower than current)
HISTORY h_power; // The history data of power applied values
HISTORY h_temp; // The history data of the temperature
EMP_AVERAGE current; // The average value for the current through the IRON
EMP_AVERAGE tilt; // The average value of tilt port
volatile EMP_AVERAGE amb_int; // The internal reading of ambient temperature
bool tilt_toggle = false; // The tilt switch changed state
const uint8_t max_power = 210; // maximum power to the IRON
const uint8_t max_fixed_power = 120; // Maximum power in fixed power mode
const uint16_t min_curr = 10; // The minimum current value to check the IRON is connected
const uint32_t check_period = 503; // Check the iron period in ms
const uint16_t h_max_counter = 500 / temp_check_period; // Put the history data twice a second
const uint8_t emp_k = 2; // The exponential average coefficient
const uint8_t emp_tilt = 3; // The exponential average coefficient for tilt pin
const uint16_t iron_cold = 25; // The internal temperature when the IRON is cold
};
void IRON::init(void) {
pinMode(sPIN, INPUT);
pinMode(aPIN, INPUT);
pinMode(tPIN, INPUT);
fastPWM.init(); // Initialization for 31.5 kHz PWM on D10 pin
mode = POWER_OFF;
fix_power = 0;
applied_power = 0;
disconnected = true; // Do not read the ambient temperature when the IRON disconnected
check_iron_ms = 0;
resetPID();
h_counter = h_max_counter;
h_power.init();
h_temp.init();
current.length(emp_k);
tilt.length(emp_tilt);
amb_int.length(4);
}
void IRON::setTemp(uint16_t t) {
if (mode == POWER_ON) resetPID();
temp_set = t;
uint16_t ta = h_temp.average();
chill = (ta > t + 5); // The IRON must be cooled
temp_low = 0; // disable low power mode
}
void IRON::lowPowerMode(uint16_t t) {
if ((mode == POWER_ON && t < temp_set) || t == 0)
temp_low = t; // Activate low power mode
}
uint8_t IRON::getAvgPower(void) {
uint16_t p = h_power.average();
return p & 0xff;
}
uint8_t IRON::appliedPower(void) {
uint8_t p = getAvgPower();
return map(p, 0, max_power, 0, 100);
}
void IRON::switchPower(bool On) {
if (!On) {
fastPWM.off();
fix_power = 0;
if (mode != POWER_OFF)
mode = POWER_COOLING;
return;
}
resetPID(analogRead(sPIN));
h_power.init();
mode = POWER_ON;
}
bool IRON::isOn(void) {
return (mode == POWER_ON || mode == POWER_FIXED);
}
bool IRON::checkIron(void) {
if (millis() < check_iron_ms)
return disconnected;
check_iron_ms = millis() + check_period;
uint16_t curr = 0;
if (applied_power == 0) { // The IRON is switched-off
fastPWM.duty(127); // Quarter of maximum power
for (uint8_t i = 0; i < 5; ++i) { // Make sure we check the current in active phase of PWM signal
delayMicroseconds(31);
uint16_t c = analogRead(cPIN); // Check the current through the IRON
if (c > curr) curr = c; // The maximum value
}
fastPWM.off();
if (curr > min_curr * 2) // Do not put big values in to the history
curr = min_curr * 2; // This is enough to ensure the IRON is connected
curr = current.average(curr); // Calculate exponential average value of the current
} else {
curr = analogRead(cPIN);
}
disconnected = (curr < min_curr);
if (mode == POWER_OFF || mode == POWER_COOLING) { // If the soldering IRON is set to be switched off
fastPWM.off(); // Surely power off the IRON
}
return disconnected;
}
// This routine is used to keep the IRON temperature near required value and is activated by the Timer1
void IRON::keepTemp(void) {
uint16_t ambient = analogRead(aPIN); // Update ambient temperature
amb_int.update(ambient);
uint16_t t = analogRead(sPIN); // Read the IRON temperature
volatile uint16_t t_set = temp_set; // The preset temperature depends on usual/low power mode
if (temp_low) t_set = temp_low;
if ((t >= temp_max + 20) || (t > (t_set + 100))) { // Prevent global over heating
if (mode == POWER_ON) chill = true; // Turn off the power in main working mode only;
}
if (t < temp_max) { // Do not save to the history readings when the IRON is disconnected
if (--h_counter < 0) {
h_temp.update(t);
h_counter = h_max_counter;
}
}
int32_t p = 0;
switch (mode) {
case POWER_OFF:
break;
case POWER_COOLING:
if (h_temp.average() < iron_cold)
mode = POWER_OFF;
break;
case POWER_ON:
if (chill) {
if (t < (t_set - 2)) {
chill = false;
resetPID();
} else {
break;
}
}
p = PID::reqPower(t_set, t);
p = constrain(p, 0, max_power);
break;
case POWER_FIXED:
p = fix_power;
break;
default:
break;
}
applied_power = p & 0xff;
if (h_counter == 1) {
h_power.update(applied_power);
}
fastPWM.duty(applied_power);
}
void IRON::fixPower(uint8_t Power) {
if (Power == 0) { // To switch off the IRON, set the Power to 0
fix_power = 0;
fastPWM.off();
mode = POWER_COOLING;
return;
}
if (Power > max_fixed_power)
Power = max_fixed_power;
fix_power = Power;
mode = POWER_FIXED;
}
/*
* Return ambient temperature in Celsius
* Caches previous result to skip expensive calculations
*/
int16_t IRON::ambientTemp(void) {
static const uint16_t add_resistor = 10030; // The additional resistor value (10koHm)
static const float normal_temp[2]= { 10000, 25 }; // nominal resistance and the nominal temperature
static const uint16_t beta = 3950; // The beta coefficient of the thermistor (usually 3000-4000)
static int32_t average = -1; // Previous value of ambient temperature (readings on aPIN)
static int cached_ambient = ambient_tempC; // Previous value of the temperature
uint16_t a_temp = amb_int.read(); // Average value of ambient temperature
if (abs(a_temp - average) < 20)
return cached_ambient;
average = a_temp;
if (average < 975) { // prevent division by zero
// convert the value to resistance
float resistance = 1023.0 / (float)average - 1.0;
resistance = (float)add_resistor / resistance;
float steinhart = resistance / normal_temp[0]; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= beta; // 1/B * ln(R/Ro)
steinhart += 1.0 / (normal_temp[1] + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to Celsius
cached_ambient = round(steinhart);
} else { // about -30 *C, perhaps, the IRON is disconnected
cached_ambient = ambient_tempC;
}
return cached_ambient;
}
void IRON::adjust(uint16_t t) {
if (t > temp_max) t = temp_max; // Do not allow over heating
temp_set = t;
}
// If any switch is short, its status is 'true'
void IRON::checkSWStatus(void) {
if (millis() > check_tilt_ms) {
check_tilt_ms = millis() + 100;
if (!disconnected) { // Current through the IRON is registered
uint16_t avg = tilt.read();
if (300 < avg && avg < 700) { // Middle state
avg = tilt.average(analogRead(tPIN));
if (avg < 300 || avg > 700) { // Toggle state
tilt_toggle = true;
}
} else {
tilt.update(analogRead(tPIN));
}
}
}
}
bool IRON::isIronTiltSwitch(bool reed) {
bool ret = tilt_toggle;
tilt_toggle = false;
if (reed) {
return tilt.read() < 300;
}
return ret;
}
//------------------------------------------ class SCREEN ------------------------------------------------------
class SCREEN {
public:
SCREEN* next; // Pointer to the next screen
SCREEN* nextL; // Pointer to the next Level screen, usually, setup
SCREEN* main; // Pointer to the main screen
SCREEN* no_iron; // Pointer to the screen when the IRON was disconnected
SCREEN() {
next = nextL = main = no_iron = 0;
update_screen = 0;
scr_timeout = 0;
time_to_return = 0;
}
virtual void init(void) { }
virtual SCREEN* show(void) { return this; }
virtual SCREEN* menu(void) { if (this->next != 0) return this->next; else return this; }
virtual SCREEN* menu_long(void) { if (this->nextL != 0) return this->nextL; else return this; }
virtual void rotaryValue(int16_t value) { }
bool isSetup(void) { return (scr_timeout != 0); }
void forceRedraw(void) { update_screen = 0; }
virtual SCREEN* returnToMain(void) {
if (main && scr_timeout && (millis() >= time_to_return)) {
scr_timeout = 0;
return main;
}
return this;
}
void resetTimeout(void) {
if (scr_timeout > 0)
time_to_return = millis() + scr_timeout*1000;
}
void setSCRtimeout(uint16_t t) {
scr_timeout = t;
resetTimeout();
}
bool wasRecentlyReset(void) {
uint32_t to = (time_to_return - millis()) / 1000;
return((scr_timeout - to) < 15);
}
protected:
uint32_t update_screen; // Time in ms when the screen should be updated
uint32_t scr_timeout; // Timeout is sec. to return to the main screen, canceling all changes
uint32_t time_to_return; // Time in ms to return to main screen
};
//---------------------------------------- class mainSCREEN [the soldering IRON is OFF] ------------------------
class mainSCREEN : public SCREEN {
public:
mainSCREEN(IRON* Iron, DSPL* DSP, RENC* ENC, BUZZER* Buzz, IRON_CFG* Cfg) {
pIron = Iron;
pD = DSP;
pEnc = ENC;
pBz = Buzz;
pCfg = Cfg;
}
virtual void init(void);
virtual SCREEN* show(void);
virtual void rotaryValue(int16_t value); // Setup the preset temperature
private:
IRON* pIron; // Pointer to the IRON instance
DSPL* pD; // Pointer to the DSPLay instance
RENC* pEnc; // Pointer to the rotary encoder instance
BUZZER* pBz; // Pointer to the simple buzzer instance
IRON_CFG* pCfg; // Pointer to the configuration instance
uint32_t clear_used_ms; // Time in ms when used flag should be cleared (if > 0)
uint32_t change_display; // Time in ms when to switch display between preset temperature and tip name
bool used; // Whether the IRON was used (was hot)
bool cool_notified; // Whether there was cold notification played
bool show_tip; // Whether show the tip name instead of preset temperature
const uint16_t period = 1000; // The period to update the screen
const uint32_t cool_notify_period = 120000; // The period to display 'cool' message (ms)
const uint16_t show_temp = 20000; // The period in ms to show the preset temperature
};
void mainSCREEN::init(void) {
pIron->switchPower(false);
uint16_t temp_set = pIron->presetTemp();
int16_t ambient = pIron->ambientTemp();
temp_set = pCfg->tempToHuman(temp_set, ambient); // The preset temperature in the human readable units
if (pCfg->isCelsius())
pEnc->reset(temp_set, temp_minC, temp_maxC, 1, 5);
else
pEnc->reset(temp_set, temp_minF, temp_maxF, 1, 5);
used = !pIron->isCold();
cool_notified = !used;
if (used) { // the IRON was used, we should save new data in EEPROM
pCfg->savePresetTempHuman(temp_set);
}
clear_used_ms = 0;
forceRedraw();
pD->clear();
pD->msgOff();
show_tip = false;
change_display = millis() + show_temp;
}
void mainSCREEN::rotaryValue(int16_t value) {
int16_t ambient = pIron->ambientTemp();
uint16_t temp = pCfg->humanToTemp(value, ambient);
pIron->setTemp(temp);
pD->tSet(value, pCfg->isCelsius());
uint32_t ms = millis();
update_screen = ms + period;
change_display = ms + show_temp;
show_tip = false;
}
SCREEN* mainSCREEN::show(void) {
SCREEN* nxt = this;
if (no_iron && pIron->checkIron()) { // Check that the IRON is connected
nxt = no_iron;
}
if (millis() < update_screen) return nxt;
update_screen = millis() + period;
if (clear_used_ms && (millis() > clear_used_ms)) {
clear_used_ms = 0;
used = false;
}
if (millis() > change_display) {
show_tip = !show_tip;
change_display = millis() + 2000;
if (!show_tip) change_display += show_temp;
}
int16_t ambient = pIron->ambientTemp();
if (show_tip) {
pD->tip(pCfg->tipName(), true);
} else {
uint16_t temp_set = pIron->presetTemp();
temp_set = pCfg->tempToHuman(temp_set, ambient); // The preset temperature in the human readable units
pD->tSet(temp_set, pCfg->isCelsius());
}
pD->msgOff();
uint16_t temp = pIron->tempAverage();
uint16_t tempH = pCfg->tempToHuman(temp, ambient);
if (pIron->isCold()) {
if (used)
pD->msgCold();
else
pD->tCurr(tempH);
if (used && !cool_notified) {
pBz->lowBeep();
cool_notified = true;
clear_used_ms = millis() + cool_notify_period;
}
} else {
pD->tCurr(tempH);
}
return nxt;
}
//---------------------------------------- class tipSCREEN [tip is disconnected, choose new tip] ---------------
class tipSCREEN : public SCREEN {
public:
tipSCREEN(IRON* Iron, DSPL* DSP, RENC* ENC, IRON_CFG* Cfg) {
pIron = Iron;
pD = DSP;
pEnc = ENC;
pCfg = Cfg;
}
virtual void init(void);
virtual SCREEN* show(void);
virtual void rotaryValue(int16_t value); // Select the tip
private:
uint8_t old_tip; // previous tip index
IRON* pIron; // Pointer to the IRON instance
DSPL* pD; // Pointer to the DSPLay instance
RENC* pEnc; // Pointer to the rotary encoder instance
IRON_CFG* pCfg; // Pointer to the configuration instance
const uint16_t period = 1000; // The period to update the screen
};
void tipSCREEN::init(void) {
pIron->switchPower(false);
old_tip = pCfg->tipIndex();
pEnc->reset(old_tip, 0, pCfg->tipsLoaded(), 1, 1, true);// Select the tip by the rotary encoder
forceRedraw();
pD->clear();
pD->msgSelectTip();
}
void tipSCREEN::rotaryValue(int16_t value) {
if (value == old_tip) return;
update_screen = millis() + period;
uint8_t index = pCfg->nextTip(old_tip, value > old_tip);
uint16_t temp = pIron->presetTemp(); // Preset temperature in internal units
int16_t ambient = pIron->ambientTemp();
temp = pCfg->tempToHuman(temp, ambient); // The temperature in human readable units (Celsius o Fahrenheit)
index = pCfg->selectTip(index);
old_tip = index;
pEnc->write(index);
temp = pCfg->humanToTemp(temp, ambient); // Translate previously set temperature in human readable units into internal value
pIron->setTemp(temp); // Install previously set temperature into the IRON by new tip calibration
forceRedraw();
}
SCREEN* tipSCREEN::show(void) {
SCREEN* nxt = this;
if (no_iron && !pIron->checkIron()) { // Check that the IRON is disconnected
nxt = no_iron;
pIron->initTempHistory(); // The new tip is connected, reset the temp history
}
if (millis() < update_screen) return nxt;
update_screen = millis() + period;
pD->tip(pCfg->tipName(), false);
pD->mark('*', !pCfg->isCalibrated());
return nxt;
}
//---------------------------------------- class actSCREEN [Toggle tip activation ] ----------------------------
class actSCREEN : public SCREEN {
public:
actSCREEN(IRON* Iron, DSPL* DSP, RENC* ENC, IRON_CFG* Cfg) {
pIron = Iron;
pD = DSP;
pEnc = ENC;
pCfg = Cfg;
}
virtual void init(void);
virtual SCREEN* show(void);
virtual SCREEN* menu(void);
virtual void rotaryValue(int16_t value); // Select the tip
private:
IRON* pIron; // Pointer to the IRON instance
DSPL* pD; // Pointer to the DSPLay instance
RENC* pEnc; // Pointer to the rotary encoder instance
IRON_CFG* pCfg; // Pointer to the configuration instance
const uint16_t period = 10000; // The period to update the screen
};
void actSCREEN::init(void) {
pIron->switchPower(false);
char *n = pCfg->tipName();
int8_t global_index = pCfg->index(n); // Find current tip in the global tip array by the name
if (global_index < 0) global_index = 0;
pEnc->reset(global_index, 0, pCfg->tipsLoaded(), 1, 1, true);// Select the tip by the rotary encoder
pD->clear();
pD->msgActivateTip();
pD->tip(pCfg->tipName(), false);
}
void actSCREEN::rotaryValue(int16_t value) {
forceRedraw();
}
SCREEN* actSCREEN::menu(void) {
uint8_t tip = pEnc->read();
bool active = pCfg->toggleTipActivation(tip);
forceRedraw();
return this;
}
SCREEN* actSCREEN::show(void) {
if (millis() < update_screen) return this;
update_screen = millis() + period;
uint8_t tip = pEnc->read();
bool active = pCfg->isTipActive(tip);
char tip_name[tip_name_sz+1];
pCfg->name(tip_name, tip);
tip_name[tip_name_sz] = '\0';
pD->clear();
pD->tip(tip_name, false);
pD->mark('x', active);
return this;
}
//---------------------------------------- class workSCREEN [the soldering IRON is ON] -------------------------
class workSCREEN : public SCREEN {
public:
workSCREEN(IRON* Iron, DSPL* DSP, RENC* Enc, BUZZER* Buzz, IRON_CFG* Cfg) {
update_screen = 0;
pIron = Iron;
pD = DSP;
pBz = Buzz;
pEnc = Enc;
pCfg = Cfg;
}
virtual void init(void);
virtual SCREEN* show(void);
virtual void rotaryValue(int16_t value); // Change the preset temperature
virtual SCREEN* returnToMain(void); // Automatic power-off
private:
void adjustPresetTemp(void);
void hwTimeout(uint16_t low_temp, bool tilt_active);
IRON* pIron; // Pointer to the IRON instance
DSPL* pD; // Pointer to the DSPLay instance
BUZZER* pBz; // Pointer to the simple Buzzer instance
RENC* pEnc; // Pointer to the rotary encoder instance
IRON_CFG* pCfg; // Pointer to the configuration instance
bool ready; // Whether the IRON have reached the preset temperature
bool lowpower_mode = false; // Whether hardware low power mode using tilt switch
uint32_t lowpower_time = 0; // Time when switch to standby power mode
uint32_t auto_off_notified; // The time (in ms) when the automatic power-off was notified
uint16_t tempH = 0; // The preset temperature in human readable units
const uint16_t period = 1000; // The period to update the screen (ms)
};
void workSCREEN::init(void) {
uint16_t temp_set = pIron->presetTemp();
int16_t ambient = pIron->ambientTemp();
bool is_celsius = pCfg->isCelsius();
tempH = pCfg->tempToHuman(temp_set, ambient);
if (is_celsius)
pEnc->reset(tempH, temp_minC, temp_maxC, 1, 5);
else
pEnc->reset(tempH, temp_minF, temp_maxF, 1, 5);
pIron->switchPower(true);
ready = false;
lowpower_mode = false;
lowpower_time = 0;
time_to_return = 0;
pD->clear();
pD->tSet(tempH, is_celsius);
pD->msgOn();
uint16_t to = pCfg->getOffTimeout() * 60;
this->setSCRtimeout(to);
auto_off_notified = 0;
forceRedraw();
}
void workSCREEN::rotaryValue(int16_t value) { // Setup new preset temperature by rotating the encoder
tempH = value;
ready = false;
lowpower_mode = false;
pD->msgOn();
int16_t ambient = pIron->ambientTemp();
uint16_t temp = pCfg->humanToTemp(value, ambient); // Translate human readable temperature into internal value
pIron->setTemp(temp);
pD->tSet(value, pCfg->isCelsius());
SCREEN::resetTimeout();
update_screen = millis() + period;
}
SCREEN* workSCREEN::show(void) {
SCREEN* nxt = this;
if (millis() < update_screen) return nxt;
update_screen = millis() + period;
int16_t temp = pIron->tempAverage();
int16_t temp_set = pIron->presetTemp();
int16_t ambient = pIron->ambientTemp();
int tempH = pCfg->tempToHuman(temp, ambient);
pD->tCurr(tempH);
uint8_t p = pIron->appliedPower();
pD->percent(p);
uint16_t td = pIron->tempDispersion();
uint16_t pd = pIron->powerDispersion();
int ap = pIron->getAvgPower();
uint16_t low_temp = pCfg->lowTemp(); // 'Standby temperature' setup in the main menu
if ((abs(temp_set - temp) < 3) && (pIron->tempDispersion() <= 10) && (ap > 0)) {
if (!ready) {
pBz->shortBeep();
ready = true;
pD->msgReady();
if (low_temp)
lowpower_time = millis() + (uint32_t)pCfg->lowTimeout() * 1000;
update_screen = millis() + (period << 2);
return this;
}
}
bool tilt_active = false;
if (low_temp) {
tilt_active = pIron->isIronTiltSwitch(pCfg->isReedType());
}
// If the automatic power-off feature is enabled, check the IRON status
if (low_temp && ready && pCfg->getOffTimeout()) { // The IRON has reaches the preset temperature
hwTimeout(low_temp, tilt_active); // Use hardware tilt switch to turn low power mode
}
if (!lowpower_mode && pCfg->isAmbientSensor())
adjustPresetTemp();
uint32_t to = (time_to_return - millis()) / 1000;
if (ready) {
if (scr_timeout > 0 && (to < 100)) {
pD->timeToOff(to);
if (!auto_off_notified) {
pBz->shortBeep();
auto_off_notified = millis();
}
} else if (lowpower_mode) {
pD->msgStandby();
} else if (SCREEN::wasRecentlyReset()) {
pD->msgWorking();
} else {
pD->msgReady();
}
} else {
pD->msgOn();
resetTimeout();
}
return nxt;
}
SCREEN* workSCREEN::returnToMain(void) {
if (main && scr_timeout && (millis() >= time_to_return)) {
scr_timeout = 0;
pBz->doubleBeep();
return main;
}
return this;
}
void workSCREEN::adjustPresetTemp(void) {
uint16_t presetTemp = pIron->presetTemp(); // Preset temperature (internal units)
int16_t ambient = pIron->ambientTemp();
uint16_t temp = pCfg->humanToTemp(tempH, ambient); // Expected temperature of IRON in internal units
if (temp != presetTemp) { // The ambient temperature have changed, we need to adjust preset temperature
pIron->adjust(temp);
}
}
void workSCREEN::hwTimeout(uint16_t low_temp, bool tilt_active) {
uint32_t now_ms = millis();
if (tilt_active) { // If the IRON is used, Reset standby time
lowpower_time = now_ms + (uint32_t)pCfg->lowTimeout() * 1000; // Convert timeout to milliseconds
if (lowpower_mode) { // If the IRON is in low power mode, return to main working mode
pIron->lowPowerMode(0);
lowpower_time = 0;
lowpower_mode = false;
ready = false;
pD->msgOn();
}
} else if (!lowpower_mode) {
if (lowpower_time) {
if (now_ms >= lowpower_time) {
int16_t ambient = pIron->ambientTemp();
uint16_t temp_low = pCfg->lowTemp();
uint16_t temp = pCfg->humanToTemp(temp_low, ambient);
pIron->lowPowerMode(temp);
auto_off_notified = false;
lowpower_mode = true;
resetTimeout(); // Activate automatic power-off
return;
}
} else {
lowpower_time = now_ms + (uint32_t)pCfg->lowTimeout() * 1000;
}
}
}
//---------------------------------------- class powerSCREEN [fixed power to the IRON] -------------------------
class powerSCREEN : public SCREEN {
public:
powerSCREEN(IRON* Iron, DSPL* DSP, RENC* Enc, IRON_CFG* CFG) {
pIron = Iron;
pD = DSP;
pEnc = Enc;
pCfg = CFG;
on = false;
}
virtual void init(void);
virtual SCREEN* show(void);
virtual void rotaryValue(int16_t value);
virtual SCREEN* menu(void);
virtual SCREEN* menu_long(void);
private:
IRON* pIron; // Pointer to the IRON instance
DSPL* pD; // Pointer to the DSPLay instance
RENC* pEnc; // Pointer to the rotary encoder instance
IRON_CFG* pCfg; // Pointer to the configuration instance
uint32_t update_screen; // Time in ms to update the screen
bool on; // Whether the power of soldering IRON is on
const uint16_t period = 1000; // The period in ms to update the screen
};
void powerSCREEN::init(void) {
uint8_t p = pIron->getAvgPower();
uint8_t max_power = pIron->getMaxFixedPower();
pEnc->reset(p, 0, max_power, 1);
on = true; // Do start heating immediately
pIron->switchPower(false);
pIron->fixPower(p);
pD->clear();
pD->pSet(p);
}
SCREEN* powerSCREEN::show(void) {
SCREEN* nxt = this;
if (no_iron && pIron->checkIron()) { // Check that the IRON is connected
nxt = no_iron;
}
if (millis() < update_screen) return nxt;
update_screen = millis() + period;
uint16_t temp = pIron->tempAverage();
int16_t ambient = pIron->ambientTemp();
temp = pCfg->tempToHuman(temp, ambient);
pD->tCurr(temp);
return nxt;
}
void powerSCREEN::rotaryValue(int16_t value) {
pD->pSet(value);
pIron->fixPower(value);
on = true;
update_screen = millis() + (period * 2);
}
SCREEN* powerSCREEN::menu(void) {
on = !on;
if (on) {
uint16_t pos = pEnc->read();
pIron->fixPower(pos);
pD->clear();
pD->pSet(pos);
} else {
pIron->fixPower(0);
pD->clear();
pD->pSet(0);
}
forceRedraw();
return this;
}
SCREEN* powerSCREEN::menu_long(void) {
pIron->fixPower(0);
if (nextL) {
pIron->switchPower(true);
return nextL;
}
return this;
}
//---------------------------------------- class errorSCREEN [the soldering IRON error detected] ---------------
class errorSCREEN : public SCREEN {
public:
errorSCREEN(IRON* Iron, DSPL* DSP, BUZZER* Buzz) {
pIron = Iron;
pD = DSP;
pBz = Buzz;
}
virtual void init(void) { pIron->switchPower(false); pD->clear(); pD->msgFail(); pBz->failedBeep(); }
private:
IRON* pIron; // Pointer to the IRON instance
DSPL* pD; // Pointer to the display instance
BUZZER* pBz; // Pointer to the simple Buzzer instance
};
//---------------------------------------- class configSCREEN [configuration menu] -----------------------------
class configSCREEN : public SCREEN {
public:
configSCREEN(IRON* Iron, DSPL* DSP, RENC* Enc, IRON_CFG* Cfg, BUZZER* Buzz) {
pIron = Iron;