-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaDSEngine.cpp
2938 lines (2574 loc) · 78.3 KB
/
aDSEngine.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
///
/// \copyright Copyright (C) 2015-2017 F1RMB, Daniel Caujolle-Bert <[email protected]>
///
/// \license
/// This program 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 2
/// of the License, or (at your option) any later version.<br><br>
/// This program 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.<br><br>
/// You should have received a copy of the GNU General Public License
/// along with this program; if not, write to the Free Software
/// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///
#include "aDSEngine.h"
#include <wiring_private.h>
///
/// \file aDSEngine.cpp
/// \author F1RMB, Daniel Caujolle-Bert <[email protected]>
///
///
/// \page mainpage SMD Soldering Station for Weller RT Series Tips
///
/// Based on a project by __Martin Kumm__ http://www.martin-kumm.de/wiki/doku.php?id=Projects:SMD_Solderstation.
///
/// The hardware has been redesigned and modified (two channels, 16x2 LCD instead of 7 segments display, etc). The software has also been rewritten from scratch.
///
/// <br>
/// I would especially like to thank my friend __Olivier__, <i><b>F5LGJ</b></i>, for his great help and support in this project.
///
/// Big thumbs up to __Patrick__, <i><b>F6AZZ</b></i>, for the documentation corrections ;-)
///
///
///
/// \page UI User Interface overview
///
/// - The Soldering Station control is performed using a simple rotary encoder, which integrates a push button.
///
/// <br>
/// - The temperature range varies from 100°C, up to 450°C.
///
///
/// <br>
/// - Depending on the hardware assembly, the station can handle one or two soldering irons:
///
/// -# The Single mode, the temperature readings and settings are displayed using a double height font.
///
/// -# The dual channels version, both soldering irons can be controlled separately, or can be merged:
/// + when the separate channels mode is used, each channel is independent. Simple clicking on the encoder push button
/// will set the focus to the next channel. The focused channel's temperature will be surrounded by the symbols <b>[</b> and <b>]</b>
///
/// + when merged mode is used, the temperature is displayed in the same way as the <i>Single</i> channel mode (double height font), both
/// channels share the same settings (target temperature).
///
/// <br>
/// - LED status decoding:
/// LED Status | Meaning
/// ------------|---------
/// ON | the tip is heating
/// OFF | the tip is cooling
/// Blinking | the tip has reached his target temperature
/// Three times blinking | the soldering station is in Standby mode (see \ref standby)
///
///
/// <br>
/// - The target temperature is stored, for each channel, into the microcontroller's EEPROM.
/// The values will be restored on the next startup.
/// After a timeout of 30 seconds, a new defined target temperature will be stored into the EEPROM.
/// If in the meantime the user defines a new target temperature, the timeout will be reset.
///
///
/// <br>
/// - When the station is in the temperature reading mode, the displayed values are left aligned.
/// When the station is in the settings mode, the displayed values are right aligned.
///
///
///
/// <br>
/// \section encoderuse Encoder use
///
/// - In any mode (settings or temperature readings), the rotary encoder is used to define the target temperature. Turn the encoder clockwise to increase the
/// target temperature, and anti-clockwise to decrease it.
///
/// <br>
/// - When the soldering station is not in the settings mode, it displays the soldering tips temperature. A single encoder detents rotation
/// will switch the soldering station into the settings mode, and displays the target temperature without any change to the target temperature
/// settings.
///
/// <br>
/// - When the soldering station is in the settings mode, and no action is performed using the encoder rotation within 3 seconds, it will
/// switch back to the temperature readings.
///
/// <br>
/// - Encoder push button:
///
/// + Single soldering tip version:
/// Button | Action
/// -------|-------
/// Single Click | <i>no effect</i>
/// Double Click | switch to standby mode (see \ref standby)
/// Held | <i>no effect</i>
///
/// <br>
/// + Dual soldering tip version:
/// Button | Action
/// -------|-------
/// Single click | change the focus to the next channel (if not in merged mode)
/// Double click | switch to standby mode (see \ref standby)
/// Held | toggles mergded mode (see \ref merged)
///
///
/// <br>
/// \section merged Merged mode
///
/// - With Dual Channel enabled hardware, it is possible to share the same temperature preset values for both soldering tips.
///
/// See \ref encoderuse
///
///
/// <br>
/// \section standby Standby
///
/// - A double-click on the encoder brings the soldering station in the standby mode.
///
/// <br>
/// - When the standby mode is enabled, the target temperature will decrease to 150°C if the temperature setting is set
/// above this point, otherwise it will decrease to 100°C.
///
/// <br>
/// - Any encoder action will exits from <i>Standby</i> mode.
///
/// <br>
/// - When the <i>Standby</i> mode is activated, the LEDs blink three times cyclically.
///
///
///
/// \page cal Calibration Process
///
///
/// + __Prerequisites__:<br><br>
///
/// - __Hardware__:
/// - Digital Thermometer (e.g: your digital multimeter with a thermocouple)
/// <br><br>
/// - __Software__:
/// * A serial terminal emulator (e.g. “<i>HyperTerminal</i>” or “<i>Tera Term</i>” on Windows, “<i>minicom</i>” or “<i>cutecom</i>” on Linux).
/// * The calibration spreadsheet file <b>aWXIronsCalibration.ods</b>
/// * A software able to open the calibration spreadsheet, like “<i>LibreOffice</i>“, “<i>OpenOffice</i>“ and so on.
///
/// The serial communication settings are: <b>57600</b>, <b>8</b>, <b>N</b>, <b>1</b>
///
/// <br>
/// + __Why a calibration__:<br>
///
/// The calibration process is necessary get accurate temperature control.<br>
/// Some average values are used by default, but that won't gives you accurate temperature control.
///
/// <br>
/// + __Process Description__:<br><br>
///
/// - __Step 1: <i>Calibration Mode</i>__
///
/// You have to open the soldering station's box and connect the soldering station to the PC, using a USB cable.<br>
/// To turn the soldering station in calibration, you have to keep the encoder's push button pressed while turning ON the station.
/// Once the station is ready to use, the '<b><i>CAL</i></b>' string is displayed on the top left side of the LCD display.<br>
///
/// In calibration mode, the readed temperature isn't displayed anymore. Instead, the ADC value is shown.<br>
///
/// The station will self set the target temperature to 100°C. You have to wait until the temperature stabilizes.<br><br>
///
/// - __Step 2: <i>Calibrate Channel 1 or 2</i>__
///
/// Select matching <b><i>Channel</i></b> tab in the calibration spreadsheet file.<br>
/// Set the temperature target using the pseudo temperature value in the first column. Wait until LED blinks and the displayed ADC value stabilize.<br>
/// Apply the thermocouple to the soldering tip, then write down the readed temperature to the column named '<b><i>Temp °C'</i></b>.<br>
/// If needed, adjust the value in the '<b><i>ADCread</i></b>' column, accordingly to the one displayed on the station's LCD.<br>
/// Apply the same procedure for all spreadsheet's rows.<br>
///
/// Once you completed the array, down the chart, the '<i>Calibration String</i>' cell contains the string you have to copy and paste to the serial terminal emulator, e.g:
/// \code :CAL:1:0.3757498594,51.7808993467 \endcode
/// This string starts with '<b>:CAL:</b>', followed by the channel's name, then two floating point values, comma separated.<br>
/// Once the string entered and validated with the <b>[RETURN]</b> key, you should get a '<b><i>:OK:</i></b>' acknowledge message.<br>
/// In case you get '<b><i>:ERR:</i></b>', double check the calibration string you pasted.<br>
///
/// If you own a Dual Channel soldering station, <b>repeat this step for the second Channel</b>.<br><br>
///
/// - __Last Step: <i>Backup</i>__
///
/// Once the full calibration is done, you <b>HAVE</b> to store the new values into the EEPROM, using the following command:
/// \code :CAL:SAVE \endcode
/// As usual, you should get a '<i><b>:OK:</b><i>' acknoledge message.
///
/// Once you validate the calibration with this command, you leave the calibration mode.<br>
/// You can unplug the USB cable, close the box and use your soldering station now.
///
/// <br>
/// + __Other available calibration commands__:<br><br>
///
/// - <i>:CAL:</i><b>OFF</b> Cancels the calibration process, restoring previous values.
///
/// - <i>:CAL:</i><b>DUMP</b> Displays the calibration values (stored in EEPROM and current ones) in the serial terminal emulator
///
/// \warning If you own a Dual Channel soldering station, you <b>HAVE</b> to calibrate both channels, or at least enter the <i>old</i> calibration string for the channel you won't calibrate.
///
static const uint8_t DIGIT_WIDTH = 3; ///< Max numerical length of temperature (used with big digits)
static const uint8_t _glyphs[][8] PROGMEM = ///< LCD glyphs (for big digits and LED)
{
{
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
},
{
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111
},
{
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111
},
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
},
{
B00000,
B00000,
B00000,
B00000,
B00000,
B01110,
B01110,
B01110
},
{
B00000,
B00000,
B00011,
B00011,
B00011,
B00011,
B00000,
B00000
},
{
B00000,
B00000,
B11000,
B11000,
B11000,
B11000,
B00000,
B00000
},
{ // LED
B00000,
B01110,
B10011,
B10111,
B10111,
B11111,
B01110,
B00000
}
};
/// _glyphs[] offsets
static const uint8_t _bigDigitsTop[12][DIGIT_WIDTH] = ///< 0..9 + ' ' top characters matrix
{
{ 3, 0, 3 }, // 0
{ 0, 3, 32 }, // 1
{ 2, 2, 3 }, // 2
{ 0, 2, 3 }, // 3
{ 3, 1, 3 }, // 4
{ 3, 2, 2 }, // 5
{ 3, 2, 2 }, // 6
{ 0, 0, 3 }, // 7
{ 3, 2, 3 }, // 8
{ 3, 2, 3 }, // 9
{ 32, 32, 32 }, // empty
{ 1, 1, 1 } // -
};
/// _glyphs[] offsets
static const uint8_t _bigDigitsBottom[12][DIGIT_WIDTH] = ///< 0..9 + ' ' bottom characters matrix
{
{ 3, 1, 3 }, // 0
{ 1, 3, 1 }, // 1
{ 3, 1, 1 }, // 2
{ 1, 1, 3 }, // 3
{ 32, 32, 3 }, // 4
{ 1, 1, 3 }, // 5
{ 3, 1, 3 }, // 6
{ 32, 32, 3 }, // 7
{ 3, 1, 3 }, // 8
{ 1, 1, 3 }, // 9
{ 32, 32, 32 }, // empty
{ 0, 0, 0 } // -
};
static ClickEncoder *pEncoder = NULL; ///< Global pointer to ClickEncoder object, used inside timer1ISR() function
#if CHANNEL_COUNTING
static uint8_t channelCount = 0;
#endif
/// \brief Returns numerical character length of argument
///
/// \param n int16_t : value to get length from
/// \return int8_t : length
///
///
static int8_t getNumericalLength(int16_t n)
{
char buf[16];
return (static_cast<int8_t>(snprintf_P(buf, sizeof(buf) - 1, PSTR("%d"), n)));
}
//
// Begin of Class aDSTemperatureAveraging
//
#ifdef DOUBLE_AVERAGE
/// \brief ValueAveraging class constructor
///
///
ValueAveraging::ValueAveraging() :
m_average(ARRAY_SIZE_MAX)
{
// ctor
ResetValues();
}
/// \brief ValueAveraging class destructor
///
///
ValueAveraging::~ValueAveraging()
{
// dtor
}
/// \brief Stacks the value to an array, used to compute an averaged value
///
/// \param value int16_t : value to stack
/// \return void
///
///
template<typename T>
void ValueAveraging::StackValue(T value)
{
if (value > 0)
{
m_offset = (m_offset + 1) % m_average;
m_values[m_offset] = static_cast<double>(value);
}
}
/// \brief Returns the averaged value, computed from stacked values
///
/// \return int16_t : averaged value
///
///
template<typename T>
T ValueAveraging::GetValue()
{
uint16_t n = 0;
double sum = 0.0;
for (uint16_t i = 0; i < m_average; i++)
{
if (isnan(m_values[i]))
break;
if (m_values[i] > 0)
{
sum += m_values[i];
n++;
}
}
// No usable value found.
if (n == 0)
return 0;
return static_cast<T>((sum / double(n)) + 0.5); // ceil
}
/// \brief Set how many values will be used to compute the average
///
/// \param v uint16_t : values used for averaging
/// \return bool : true on success
///
///
bool ValueAveraging::SetAverage(uint16_t v)
{
bool ret = false;
if ((v >= 0) && (v <= ARRAY_SIZE_MAX))
{
// Zeroing the array
ResetValues();
m_average = v;
ret = true;
}
return ret;
}
/// \brief Get how many values will be used to compute the average
///
/// \return uint16_t : values used for averaging
///
///
uint16_t ValueAveraging::GetAverage()
{
return m_average;
}
/// \brief Get max value that can be used to build the average
///
/// \return uint16_t : max values used for averaging
///
///
uint16_t ValueAveraging::GetMaxAverage()
{
return ARRAY_SIZE_MAX;
}
/// \brief Reset the array used to store values to be averaged
///
/// \return void
///
///
void ValueAveraging::ResetValues()
{
for (uint16_t i = 0; i < ARRAY_SIZE_MAX; i++)
m_values[i] = NAN;
m_offset = ARRAY_SIZE_MAX - 1;
}
#else
/// \brief ValueAveraging class constructor
///
///
ValueAveraging::ValueAveraging() :
m_average(ARRAY_SIZE_MAX)
{
// ctor
ResetValues();
}
/// \brief ValueAveraging class destructor
///
///
ValueAveraging::~ValueAveraging()
{
// dtor
}
/// \brief Stacks the value to an array, used to compute an averaged value
///
/// \param value int16_t : value to stack
/// \return void
///
///
void ValueAveraging::StackValue(int16_t value)
{
if (value > 0)
{
m_offset = (m_offset + 1) % m_average;
m_values[m_offset] = value;
}
}
/// \brief Returns the averaged value, computed from stacked values
///
/// \return int16_t : averaged value
///
///
int16_t ValueAveraging::GetValue()
{
uint16_t n = 0;
double sum = 0.0;
for (uint16_t i = 0; i < m_average; i++)
{
if (m_values[i] == -1)
break;
if (m_values[i] > 0)
{
sum += m_values[i];
n++;
}
}
// No usable value found.
if (n == 0)
return 0;
return static_cast<int16_t>((sum / double(n)) + 0.5); // ceil
}
/// \brief Set how many values will be used to compute the average
///
/// \param v uint16_t : values used for averaging
/// \return bool : true on success
///
///
bool ValueAveraging::SetAverage(uint16_t v)
{
bool ret = false;
if ((v >= 0) && (v <= ARRAY_SIZE_MAX))
{
// Zeroing the array
ResetValues();
m_average = v;
ret = true;
}
return ret;
}
/// \brief Get how many values will be used to compute the average
///
/// \return uint16_t : values used for averaging
///
///
uint16_t ValueAveraging::GetAverage()
{
return m_average;
}
/// \brief Get max value that can be used to build the average
///
/// \return uint16_t : max values used for averaging
///
///
uint16_t ValueAveraging::GetMaxAverage()
{
return ARRAY_SIZE_MAX;
}
/// \brief Reset the array used to store values to be averaged
///
/// \return void
///
///
void ValueAveraging::ResetValues()
{
for (uint16_t i = 0; i < ARRAY_SIZE_MAX; i++)
m_values[i] = -1;
m_offset = ARRAY_SIZE_MAX - 1;
}
#endif
//
// End of Class ValueAveraging
//
//
// Begin of Class aDSChannel
//
/// \brief aDSChannel class constructor
///
aDSChannel::aDSChannel() :
m_hasFocus(false),
m_targetTemp(0),
m_currentTemp(0),
m_pwmValue(0),
m_adcValue(0),
m_inStandby(false),
m_heatState(HEATING_STATE_COOLING),
m_ledState(LOW),
m_nextPass(0),
m_tempHasChanged(true),
m_nextBlink(0),
m_blinkStandby(0)
#ifdef SIMU
, m_nextTempStep(0),
m_nextLowering(0)
#endif // SIMU
#if CHANNEL_COUNTING
, m_channel(channelCount++)
#endif // 0
, m_isPlugged(true),
m_brother(NULL)
{
//ctor
m_pwmPin.pin = NOT_A_PIN;
m_sensorPin.pin = NOT_A_PIN;
m_ledPin.pin = NOT_A_PIN;
m_cal.slope = DEFAULT_TEMPERATURE_SLOPE;
m_cal.offset = DEFAULT_TEMPERATURE_OFFSET;
}
/// \brief aDSChannel destructor
///
aDSChannel::~aDSChannel()
{
//dtor
#if CHANNEL_COUNTING
channelCount--;
#endif
}
/// \brief Setup member function, should be called before any other member
///
/// Pins will be embedded into aPin_t object, timer, mask, port and output register will be set also here,
/// preventing using Arduino analog/digital{Read/Write}() calls, which are quite slow.
///
/// \param pwmPin uint8_t : PWM pin, used to drive the output MosFET
/// \param sensorPin uint8_t : Sensor pin, used to get analog temperature value.
/// \param ledPin uint8_t : LED pin, used to reflect Heating/Cooling state
/// \return void
///
///
void aDSChannel::setup(uint8_t pwmPin, uint8_t sensorPin, uint8_t ledPin)
{
//
// PWM
//
m_pwmPin.pin = pwmPin;
pinMode(m_pwmPin.pin, OUTPUT);
m_pwmPin.timer = digitalPinToTimer(m_pwmPin.pin);
m_pwmPin.mask = digitalPinToBitMask(m_pwmPin.pin);
m_pwmPin.port = digitalPinToPort(m_pwmPin.pin);
m_pwmPin.outputRegister = portOutputRegister(m_pwmPin.port);
//
// Turn PWM OFF
//
_analogWrite(m_pwmPin, m_pwmValue);
//
// Sensor
//
m_sensorPin.pin = sensorPin;
pinMode(m_sensorPin.pin, INPUT),
//
// LED
//
m_ledPin.pin = ledPin;
pinMode(m_ledPin.pin, OUTPUT);
m_ledPin.timer = digitalPinToTimer(m_ledPin.pin);
m_ledPin.mask = digitalPinToBitMask(m_ledPin.pin);
m_ledPin.port = digitalPinToPort(m_ledPin.pin);
m_ledPin.outputRegister = portOutputRegister(m_ledPin.port);
//
// Turn LED OFF
//
_digitalWrite(m_ledPin, HIGH);
}
/// \brief Set the focus, as display point of view.
///
/// \param v bool : focus state
/// \return void
///
///
void aDSChannel::setFocus(bool v)
{
m_hasFocus = v;
}
/// \brief Get the focus state.
///
/// \return bool : focus state
///
///
bool aDSChannel::hasFocus()
{
return m_hasFocus;
}
/// \brief Get current temperature accordingly from the given mode (SET/READ)
///
/// \param mode OperationMode_t : operation mode
/// \return uint16_t : temperature, in Celcius
///
///
uint16_t aDSChannel::getTemperature(OperationMode_t mode)
{
return (mode == OPERATION_MODE_READ) ?
#ifdef DOUBLE_AVERAGE
m_avrTemp.GetValue<int16_t>()
#else
m_avrTemp.GetValue()
#endif // DOUBLE_AVERAGE
: m_targetTemp;
}
/// \brief Set current temperature accordingly from the given mode (SET/READ)
///
/// \param mode OperationMode_t : operation mode
/// \param temp int16_t : temperature, in Celcius
/// \return bool : true if temperature has been changed, otherwise false
///
///
bool aDSChannel::setTemperature(OperationMode_t mode, int16_t temp)
{
int16_t p = (mode == OPERATION_MODE_SET) ? m_targetTemp : m_currentTemp;
if (mode == OPERATION_MODE_SET)
{
m_tempHasChanged = (m_targetTemp != temp); // Set bool flag if target temp has changed
m_targetTemp = temp;
}
else
{
m_currentTemp = temp;
m_avrTemp.StackValue(m_currentTemp);
}
return (p != ((mode == OPERATION_MODE_SET) ? m_targetTemp : m_currentTemp));
}
/// \brief This member should be called often, it manage heating/cooling of the Channel
///
/// \param m unsigned long : current timestamp
/// \return bool : return true if readed temperature has changed since last call, otherwise false
///
///
bool aDSChannel::service(unsigned long m)
{
int16_t otemp = m_currentTemp;
int16_t target = m_targetTemp;
//
// If temperature setting is < TEMPERATURE_STANDBY, then define it to TEMPERATURE_MIN insteaf of TEMPERATURE_STANDBY
//
if (m_inStandby)
target = (m_targetTemp < TEMPERATURE_STANDBY) ? TEMPERATURE_MIN : TEMPERATURE_STANDBY;
//
// Get current temperature
//
// Turn OFF brother's heater, since that introduce some ADC noise
//
if (m_brother)
m_brother->_turnPWM(false);
//
// Switch OFF the heater
//
_analogWrite(m_pwmPin, 0);
//
// Wait for some time (to get low pass filter in steady state)
//
delay(20);
//
// Read the ADC value
//
m_adcValue = _analogRead(m_sensorPin);
//
// Switch the heater back ON, to previous value
//
_analogWrite(m_pwmPin, m_pwmValue);
//
// Restore brother's heater state
//
if (m_brother)
m_brother->_turnPWM(true);
//
// Detect if iron tip is plugged or not
//
m_isPlugged = (m_adcValue > 80);
//
// Compute temperature from ADC value
//
int16_t currTemp = round((static_cast<float>(m_adcValue) * m_cal.slope) + m_cal.offset);
#ifdef SIMU // DEV SIMULATION MODE
currTemp = m_currentTemp;
#else
m_currentTemp = currTemp;
m_avrTemp.StackValue(m_currentTemp);
#endif
//
// Heating/Cooling
//
// Compute difference between target and actual temperature
//
int16_t diff = target - currTemp;
int16_t pwm = 0;
//
// Limit PWM value to 0...PWM_MAX_VALUE
//
if (diff > 0)
{
pwm = constrain((diff * 10), 0, PWM_MAX_VALUE);
//
// Slightly increase PWM width for fine temp control
//
if (diff <= TEMPERATURE_TOLERANCE)
pwm += (diff * 15);
}
#if 0
Serial.print(m_channel, DEC);
Serial.print(F(": [ "));
Serial.print(m_adcValue, DEC);
Serial.print(F(" ], "));
Serial.print(currTemp, DEC);
Serial.print(F(" deg"));
Serial.print(F(", diff: "));
Serial.print(diff, DEC);
Serial.print(F(", PWM: "));
Serial.print(pwm, DEC);
Serial.print(F(", "));
Serial.println(m_isPlugged ? F("PLUGGED") : F("NOT PLUGGED"));
#endif
//
// Reflect Heating/Cooling/Reached on state LED
//
if (m_isPlugged)
{
if (m_inStandby)
m_heatState = HEATING_STATE_STANDBY;
else
{
if (currTemp > TEMPERATURE_TOLERANCE)
{
if ((currTemp >= (target - TEMPERATURE_TOLERANCE)) && (currTemp <= (target + TEMPERATURE_TOLERANCE)))
m_heatState = HEATING_STATE_REACHED;
else
m_heatState = (pwm == 0) ? HEATING_STATE_COOLING : HEATING_STATE_HEATING;
}
}
}
else
m_heatState = HEATING_STATE_COOLING;
#ifdef SIMU // DEV SIMULATION MODE
//static unsigned long m_nextTempStep = 0;
if ((m - m_nextTempStep) > 400)
{
m_nextTempStep = m;
if (m_currentTemp > target)
m_currentTemp--;
else if (m_currentTemp < target)
m_currentTemp++;
if (((m_heatState == HEATING_STATE_REACHED) || (m_heatState == HEATING_STATE_STANDBY)) && ((m - m_nextLowering) > 1500))
{
m_nextLowering = m;
m_currentTemp -= 2;
}
}
#endif
//
// Update PWM if necessary
//
if (m_pwmValue != pwm)
{
m_pwmValue = pwm;
_analogWrite(m_pwmPin, m_pwmValue);
}
return (otemp != m_currentTemp);
}
/// \brief Enable or disable channel's standby
///
/// \param enable bool : enability
/// \return void
///
///
void aDSChannel::setStandbyMode(bool enable)
{
m_inStandby = enable;
}
/// \brief Get channel's standby enability
///
/// \return bool
///
///
bool aDSChannel::getStandbyMode()
{
return m_inStandby;
}
/// \brief Is target temperature has changed (use for EEPROM storage)
///
/// \return bool : return true if target temperature has changed, otherwise false
///
///
bool aDSChannel::isTempHasChanged()
{
return m_tempHasChanged;
}
/// \brief Reset temperature change flag (use for EEPROM storage)
///
/// \return void
///
///
void aDSChannel::syncTempChange()
{
m_tempHasChanged = false;
}
/// \brief Change LED state according for Heating/Cooling/Standby status
///
/// \param m unsigned long : timestamp
/// \return uint8_t : HIGH or LOW (LED on or off, accordingly)
///
///
uint8_t aDSChannel::updateLEDState(unsigned long m)
{
if (((m - m_nextBlink) > BLINK_UPDATE_RATE) || (m_inStandby && ((m - m_nextBlink) > (BLINK_UPDATE_RATE / 3))))
{
m_nextBlink = m;
switch (m_heatState)
{
case HEATING_STATE_HEATING:
//
// LED ON
//
if (_digitalRead(m_ledPin) == HIGH)
m_ledState = HIGH;
break;
case HEATING_STATE_COOLING:
//
// LED OFF
//
if (_digitalRead(m_ledPin) == LOW)
m_ledState = LOW;
break;
case HEATING_STATE_REACHED:
//