~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to Tom_E/narsil/NarsilMulti/NarsilM.c

  • Committer: Selene Scriven
  • Date: 2018-05-09 05:27:55 UTC
  • mfrom: (188.1.17 trunk)
  • Revision ID: bzr@toykeeper.net-20180509052755-06y2fnsgrq96k8nc
merged trunk, prep for merging back

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
 *  The higher values reduce parasitic drain - important for e-switch lights.
59
59
 *      
60
60
 */
61
 
#define FIRMWARE_VERS (10)      // v1.0 (in 10ths)
 
61
#define FIRMWARE_VERS (12)      // v1.2 (in 10ths)
62
62
 
63
63
#define FET_7135_LAYOUT
64
64
#define ATTINY 85
65
65
 
66
 
#include "setups.h"
 
66
//-----------------------------------------------------------------------------
 
67
// SETUPS Header File defined here:
 
68
//-----------------------------------------------------------------------------
 
69
//#include "setups_2C2S.h"
 
70
//#include "setups_GTBuck.h"
 
71
#include "setups_Q8_2C1S.h"
 
72
//#include "setups_3C1S.h"
 
73
//#include "setups_3C2S.h"
67
74
 
68
75
#if OUT_CHANNELS == 3
69
76
 #ifdef VOLT_MON_R1R2
75
82
 
76
83
#include "RegisterSettings.h"
77
84
 
78
 
#define byte uint8_t
79
 
#define word uint16_t
 
85
#define byte uint8_t
 
86
#define word uint16_t
80
87
 
81
 
// Ignore a spurious warning, we did the cast on purpose
82
 
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
 
88
// Ignore a spurious warning, we did the cast on purpose
 
89
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
83
90
 
84
91
#include <avr/pgmspace.h>
85
92
#include <avr/io.h>
91
98
//#include <avr/power.h>
92
99
 
93
100
 
94
 
#define F_CPU 8000000UL                 // 8 Mhz
95
 
#define BOGOMIPS (F_CPU/4000)   // set to 2000 for 8 Mhz
 
101
#define F_CPU 8000000UL                 // 8 Mhz
 
102
#define BOGOMIPS (F_CPU/4000)   // set to 2000 for 8 Mhz
96
103
#define OWN_DELAY                                       // Don't use stock delay functions.
97
104
#define USE_DELAY_S                             // Also use _delay_s(), not just _delay_ms()
98
105
 
99
106
#include "tk-delay.h"
100
107
#include "tk-random.h"
101
108
 
102
 
#include "tk-calibWight.h"              // use this for the BLF Q8 driver (and wight drivers)
103
 
//#include "tk-calibMTN17DDm.h"         // use this for the MtnE MTN17DDm v1.1 driver
104
 
 
105
 
 
 
109
// Comment out all tk-calib headers if not using R1/R2 (for example: for the BLF Q8)
 
110
//#include "tk-calibGT.h"                               // use this for the GT-buck driver 
 
111
//#include "tk-calibWight.h"                    // use this for the wight style drivers using external R1/R2's
 
112
//#include "tk-calibMTN17DDm.h"         // use this for the MtnE MTN17DDm v1.1 driver with external R1/R2
 
113
 
 
114
 
 
115
//#include "RampingTables_HiPerf.h"     // for very high performance, high output custom mods
 
116
//#include "ModeSets_HiPerf.h"          // for very high performance, high output custom mods
106
117
#include "RampingTables.h"
107
118
#include "ModeSets.h"
108
119
 
123
134
// Config Settings via UI, with default values:
124
135
//----------------------------------------------------------------
125
136
volatile byte ramping;                                  // 1: ramping mode in effect, 0: mode sets
126
 
#if USING_3807135_BANK
 
137
#if USING_3807135_BANK
127
138
volatile byte moonlightLevel;                   // 0..7, 0: disabled, usually set to 3 (350 mA) or 5 (380 mA) - 2 might work on a 350 mA
128
139
#else
129
140
volatile byte moonlightLevel;                   // 0..7, 0: disabled, usually set to 3 (350 mA) or 5 (380 mA) - 2 might work on a 350 mA
142
153
//----------------------------------------------------------------
143
154
 
144
155
volatile byte OffTimeEnable = OFFTIME_ENABLE;   // 1: Do OFF time mode memory for Mode Sets on power switching (tailswitch), 0: disabled
 
156
volatile byte momentaryState = 0;                                       // 1: momentary mode in effect, 0: ramping or mode sets
145
157
 
146
158
 
147
159
// Ramping :
148
 
#define MAX_RAMP_LEVEL (RAMP_SIZE)
 
160
#define MAX_RAMP_LEVEL (RAMP_SIZE)
 
161
 
 
162
#ifdef TURBO_LEVEL_SUPPORT
 
163
 #define TURBO_LEVEL (MAX_RAMP_LEVEL+1)
 
164
#else
 
165
 #define TURBO_LEVEL (MAX_RAMP_LEVEL)
 
166
#endif
 
167
 
149
168
 
150
169
volatile byte rampingLevel = 0;         // 0=OFF, 1..MAX_RAMP_LEVEL is the ramping table index, 255=moon mode
151
170
byte rampState = 0;                                             // 0=OFF, 1=in lowest mode (moon) delay, 2=ramping Up, 3=Ramping Down, 4=ramping completed (Up or Dn)
205
224
                                                                                                //   in calib, use it to timeout a click response
206
225
byte byStepdownTemp = DEFAULT_STEPDOWN_TEMP;    // the current temperature in C for doing the stepdown
207
226
 
208
 
byte byBlinkActiveChan = 0;             // schedule a onboard LED blink to occur for showing the active channel
209
 
 
 
227
byte byBlinkActiveChan = 0;             // schedule a onboard LED blink to occur for showing the active channel
 
228
 
210
229
 
211
230
// Configuration settings storage in EEPROM
212
231
word eepos = 0; // (0..n) position of mode/settings stored in EEPROM (128/256/512)
269
288
void Strobe(byte ontime, byte offtime)
270
289
{
271
290
        FET_PWM_LVL = 255;
 
291
 
 
292
#ifdef GT_BUCK
 
293
        ONE7135_PWM_LVL = 255;  //  for GT-buck
 
294
        _delay_ms(ontime);
 
295
        ONE7135_PWM_LVL = 0;            //  for GT-buck
 
296
#else   
272
297
        _delay_ms(ontime);
273
298
        FET_PWM_LVL = 0;
 
299
#endif
274
300
        _delay_ms(offtime);
275
301
}
276
302
 
311
337
        specModeIdx = 0;
312
338
        modeIdx = specialModes[specModeIdx];
313
339
                                                                                
314
 
  #ifdef ONBOARD_LED
 
340
  #ifdef ONBOARD_LED
315
341
        TurnOnBoardLed(0);      // be sure the on board LED is OFF here
316
342
  #endif
317
343
}
417
443
        if (BlinkCnt > 5)
418
444
                nMsecs = 150;
419
445
                
420
 
        // Flash 'n' times before lowering
421
 
        while (BlinkCnt-- > 0)
422
 
        {
423
 
                SetOutput(OFF_OUTPUT);
424
 
                _delay_ms(nMsecs);
 
446
        // Flash 'n' times before lowering
 
447
        while (BlinkCnt-- > 0)
 
448
        {
 
449
                SetOutput(OFF_OUTPUT);
 
450
                _delay_ms(nMsecs);
425
451
 
426
452
#ifdef ONBOARD_LED
427
 
                TurnOnBoardLed(1);
 
453
                TurnOnBoardLed(1);
428
454
#endif
429
455
 
430
 
                if (ramping)
431
 
                        SetLevel(outLevel);
432
 
                else
433
 
                {
 
456
                if (ramping)
 
457
                        SetLevel(outLevel);
 
458
                else
 
459
                {
434
460
                  #if OUT_CHANNELS == 3         // Triple Channel
435
 
                        SetOutput(currOutLvl1, currOutLvl2, currOutLvl3);
 
461
                        SetOutput(currOutLvl1, currOutLvl2, currOutLvl3);
436
462
                  #elif OUT_CHANNELS == 2       // two channels
437
 
                        SetOutput(currOutLvl1, currOutLvl2);
438
 
                  #else                 
439
 
                        SetOutput(currOutLvl);
440
 
                  #endif
441
 
                }
442
 
                _delay_ms(nMsecs*2);
 
463
                        SetOutput(currOutLvl1, currOutLvl2);
 
464
                  #else                 
 
465
                        SetOutput(currOutLvl);
 
466
                  #endif
 
467
                }
 
468
                _delay_ms(nMsecs*2);
443
469
 
444
470
#ifdef ONBOARD_LED
445
 
                TurnOnBoardLed(0);
 
471
                TurnOnBoardLed(0);
446
472
#endif
447
 
        }
 
473
        }
448
474
}
449
475
 
450
476
#ifdef ONBOARD_LED
483
509
**************************************************************************************/
484
510
inline static void ClickBlink()
485
511
{
486
 
  #if OUT_CHANNELS == 3         // Triple Channel
487
 
        SetOutput(20,0,0);
488
 
  #elif OUT_CHANNELS == 2       // two channels
489
 
        SetOutput(20,0);
490
 
  #else
491
 
        SetOutput(16);
492
 
  #endif
 
512
        SetOutput(CLICK_BRIGHTNESS);
493
513
        _delay_ms(100);
494
514
        SetOutput(OFF_OUTPUT);
495
515
}
496
516
 
497
 
/**************************************************************************************
498
 
* BlinkActiveChannel - blink 1 or 2 times to show which of 2 output channels is
499
 
*               active. Only the Ind. LED is used.
500
 
**************************************************************************************/
501
 
inline static void BlinkActiveChannel()
502
 
{
 
517
/**************************************************************************************
 
518
* BlinkActiveChannel - blink 1 or 2 times to show which of 2 output channels is
 
519
*               active. Only the Ind. LED is used.
 
520
**************************************************************************************/
 
521
inline static void BlinkActiveChannel()
 
522
{
503
523
#ifdef ONBOARD_LED
504
 
        byte cnt = 1;
505
 
        
506
 
        if (outLevel == 0)      // if invalid, bail. outLevel should be 1..RAMP_SIZE or 255
507
 
                return;
508
 
 
509
 
  #if OUT_CHANNELS == 2
510
 
        if ((outLevel >= FET_START_LVL) && (outLevel < 255))    // 255 is special value for moon mode (use 1)
511
 
                cnt = 2;
512
 
  #endif
513
 
                
514
 
  #if OUT_CHANNELS == 3
515
 
        if (outLevel < BANK_START_LVL)
516
 
                cnt = 1;
517
 
        else if (outLevel < FET_START_LVL)
518
 
                cnt = 2;
519
 
        else if (outLevel < 255)        // 255 is special value for moon mode (use 1)
520
 
                cnt = 3;
521
 
  #endif
522
 
        
523
 
        _delay_ms(300);                         // delay initially so a button LED can be seen
524
 
        for (; cnt>0; cnt--)
525
 
        {
526
 
                TurnOnBoardLed(1);
527
 
                _delay_ms(175);
528
 
                TurnOnBoardLed(0);
529
 
                if (cnt > 1)
530
 
                        _delay_ms(175);
531
 
        }
 
524
        byte cnt = 1;
 
525
        
 
526
        if (outLevel == 0)      // if invalid, bail. outLevel should be 1..RAMP_SIZE or 255
 
527
                return;
 
528
 
 
529
  #if OUT_CHANNELS == 2
 
530
        if ((outLevel >= FET_START_LVL) && (outLevel < 255))    // 255 is special value for moon mode (use 1)
 
531
                cnt = 2;
 
532
  #endif
 
533
                
 
534
  #if OUT_CHANNELS == 3
 
535
        if (outLevel < BANK_START_LVL)
 
536
                cnt = 1;
 
537
        else if (outLevel < FET_START_LVL)
 
538
                cnt = 2;
 
539
        else if (outLevel < 255)        // 255 is special value for moon mode (use 1)
 
540
                cnt = 3;
 
541
  #endif
 
542
        
 
543
        _delay_ms(300);                         // delay initially so a button LED can be seen
 
544
        for (; cnt>0; cnt--)
 
545
        {
 
546
                TurnOnBoardLed(1);
 
547
                _delay_ms(175);
 
548
                TurnOnBoardLed(0);
 
549
                if (cnt > 1)
 
550
                        _delay_ms(175);
 
551
        }
532
552
#endif
533
 
}
534
 
 
 
553
}
 
554
 
535
555
/**************************************************************************************
536
556
* IsPressed - debounce the switch release, not the switch press
537
557
* ==========
632
652
        cli();                                                  // Disable interrupts
633
653
        wdt_reset();                                    // Reset the WDT
634
654
        WDTCR |= (1<<WDCE) | (1<<WDE);  // Start timed sequence
635
 
        WDTCR = (1<<WDIE);                              // Enable interrupt every 16ms (was 1<<WDTIE)
 
655
        WDTCR = (1<<WDIE);                              // Enable interrupt every 16ms (was 1<<WDTIE)
636
656
        sei();                                                  // Enable interrupts
637
657
}
638
658
 
662
682
  #else
663
683
        ADMUX  = ADCMUX_VCC_INTREF;             // not left-adjust, Vbg
664
684
  #endif
665
 
        DIDR0 |= (1 << ADC1D);                                  // disable digital input on ADC1 pin to reduce power consumption
666
 
        ADCSRA = (1 << ADEN ) | (1 << ADSC ) | 0x07;// enable, start, ADC clock prescale = 128 for 62.5 kHz
 
685
        DIDR0 |= (1 << ADC1D);                                  // disable digital input on ADC1 pin to reduce power consumption
 
686
        ADCSRA = (1 << ADEN ) | (1 << ADSC ) | 0x07;// enable, start, ADC clock prescale = 128 for 62.5 kHz
667
687
}
668
688
 
669
689
/**************************************************************************************
705
725
        
706
726
        //-----------------------------------------
707
727
   sleep_enable();
708
 
        sleep_bod_disable();            // try to disable Brown-Out Detection to reduce parasitic drain
 
728
        sleep_bod_disable();            // try to disable Brown-Out Detection to reduce parasitic drain
709
729
        sleep_cpu();                            // go to sleepy beddy bye
710
730
        sleep_disable();
711
731
        //-----------------------------------------
747
767
{
748
768
        byte byMarker;
749
769
 
750
 
   // find the config data
751
 
   for (eepos=0; eepos < 128; eepos+=4)
 
770
   // find the config data
 
771
   for (eepos=0; eepos < 128; eepos+=4)
752
772
        {
753
773
           config1 = eeprom_read_byte((const byte *)eepos);
754
774
                config2 = eeprom_read_byte((const byte *)eepos+1);
760
780
                break;
761
781
   }
762
782
 
763
 
   // unpack the config data
764
 
   if (eepos < 128)
 
783
   // unpack the config data
 
784
   if (eepos < 128)
765
785
        {
766
786
           modeIdx = config1 & 0x7;
767
787
                modeSetIdx = (config1 >> 3) & 0x0f;
830
850
        
831
851
        byte oldpos = eepos;
832
852
        
833
 
        eepos = (eepos+4) & 127;  // wear leveling, use next cell
 
853
        eepos = (eepos+4) & 127;  // wear leveling, use next cell
834
854
        
835
855
        // Write the current settings (3 bytes)
836
856
        WrEEPROM (eepos, config1);
874
894
          #ifdef VOLT_MON_R1R2
875
895
                byVoltage = (byte)(wAdcVal >> 2);               // convert to 8 bits, throw away 2 LSB's
876
896
          #else
877
 
                // Read cell voltage, applying the 
 
897
                // Read cell voltage, applying the equation
878
898
                wAdcVal = (11264 + (wAdcVal >> 1))/wAdcVal + D1_DIODE;          // in volts * 10: 10 * 1.1 * 1024 / ADC + D1_DIODE, rounded
879
899
                if (byVoltage > 0)
880
900
                {
931
951
{
932
952
        static word wStepdownTicks = 0;
933
953
        static byte byModeForClicks = 0;
 
954
        static byte initialLevelForClicks = 0;
 
955
        static byte turboRestoreLevel = 0;
934
956
        
935
957
  #ifdef VOLTAGE_MON
936
958
        static word wLowBattBlinkTicks = 0;
962
984
                //---------------------------------------------------------------------------------------
963
985
                // Handle "button stuck" safety timeout
964
986
                //---------------------------------------------------------------------------------------
965
 
                if (wPressDuration == 1250)     // 20 seconds
 
987
                if ((wPressDuration == 1250) && !momentaryState)        // 20 seconds
966
988
                {
967
989
                        modeIdx = outLevel = rampingLevel = 0;
968
990
                        rampState = 0;
995
1017
 
996
1018
                if (!holdHandled)
997
1019
                {
 
1020
                        // For Tactical, turn on MAX while button is depressed
 
1021
                        if (momentaryState)
 
1022
                        {
 
1023
                                if ((wPressDuration == 1) && !byLockOutSet)
 
1024
                                {
 
1025
                                        outLevel = rampingLevel = TURBO_LEVEL;
 
1026
                                        SetLevel(outLevel);
 
1027
                                }
 
1028
                        }
 
1029
                        
998
1030
                        //------------------------------------------------------------------------------
999
1031
                        //      Ramping - pressed button
1000
1032
                        //------------------------------------------------------------------------------
1001
 
                        if (ramping)
 
1033
                        else if (ramping)
1002
1034
                        {
1003
1035
                                if ((wPressDuration >= SHORT_CLICK_DUR) && !byLockOutSet && !byDelayRamping && (modeIdx < BATT_CHECK_MODE))
1004
1036
                                {
 
1037
                                        turboRestoreLevel = 0;  // reset on press&holds (for 2X click exit of turbo feature)
 
1038
                                        
1005
1039
                                        switch (rampState)
1006
1040
                                        {
1007
1041
                                         case 0:                // ramping not initialized yet
1009
1043
                                                {
1010
1044
                                                        rampState = 1;
1011
1045
                                                        rampPauseCntDn = RAMP_MOON_PAUSE;       // delay a little on moon
1012
 
 
1013
 
                                                  #if OUT_CHANNELS == 3         // Triple Channel
1014
 
                                                        SetOutput(moonlightLevel,0,0);
 
 
b'\t\t\t\t\t\t  #elif OUT_CHANNELS == 2\t// two channels'
1015
 
                                                        SetOutput(moonlightLevel,0);
1016
 
                                                  #else
1017
 
                                                        SetOutput(moonlightLevel);
1018
 
                                                  #endif
 
1046
 
 
1047
                                                 #if OUT_CHANNELS == 3          // Triple Channel
 
1048
                                                        SetOutput(moonlightLevel,0,0);
 
1049
                                                 #elif OUT_CHANNELS == 2        // two channels
 
1050
                                                  #ifdef GT_BUCK
 
1051
                                                        SetOutput(moonlightLevel,25);    // 10% for GT-buck analog control channel
 
1052
                                                  #else
 
1053
                                                        SetOutput(moonlightLevel,0);
 
1054
                                                  #endif
 
1055
                                                 #else
 
1056
                                                        SetOutput(moonlightLevel);
 
1057
                                                 #endif
1019
1058
 
1020
1059
#ifdef ONBOARD_LED
1021
 
                                                        if (locatorLed)
 
1060
                                                        if (locatorLed)
1022
1061
                                                                TurnOnBoardLed(0);
1023
1062
#endif
1024
1063
                                                                
1043
1082
                                                        #else
1044
1083
                                                                rampState = 2;  // lo->hi
1045
1084
                                                        #endif
1046
 
                                                        if (rampingLevel == MAX_RAMP_LEVEL)
 
1085
 
 
1086
                                                        if (rampingLevel == 255)        // If stopped in ramping moon mode, start from lowest
 
1087
                                                        {
 
1088
                                                                rampState = 2; // lo->hi
 
1089
                                                                outLevel = rampingLevel = 1;
 
1090
                                                                SetLevel(outLevel);
 
1091
                                                        }
 
1092
                                                        else if (rampingLevel >= MAX_RAMP_LEVEL)
1047
1093
                                                        {
1048
1094
                                                                rampState = 3;  // hi->lo
1049
1095
                                                                outLevel = MAX_RAMP_LEVEL;
1050
1096
                                                                SetLevel(outLevel);
1051
1097
                                                        }
1052
 
                                                        else if (rampingLevel == 255)   // If stopped in ramping moon mode, start from lowest
1053
 
                                                        {
1054
 
                                                                rampState = 2; // lo->hi
1055
 
                                                                outLevel = rampingLevel = 1;
1056
 
                                                                SetLevel(outLevel);
1057
 
                                                        }
1058
1098
                                                        else if (rampingLevel == 1)
1059
1099
                                                                rampState = 2;  // lo->hi
1060
1100
                                                }
1115
1155
                        //---------------------------------------------------------------------------------------
1116
1156
                        if (!ramping && (wPressDuration == LONG_PRESS_DUR) && !byLockOutSet)
1117
1157
                        {
1118
 
                                if (modeIdx < 16)
 
1158
                                if ((modeIdx < 16) && !momentaryState)
1119
1159
                                {
1120
1160
                                        // Long press
1121
1161
                                        if (highToLow)
1122
1162
                                                NextMode();
1123
1163
                                        else
 
1164
                                        {
 
1165
                                                if (modeIdx == 1)       // wrap around from lowest to highest
 
1166
                                                        modeIdx = 0;
1124
1167
                                                PrevMode();
 
1168
                                        }
1125
1169
                                }
1126
1170
                                else if (modeIdx > SPECIAL_MODES)
1127
1171
                                {
1128
1172
                                        if (specModeIdx > 0)
1129
 
                                        {
1130
1173
                                                --specModeIdx;
1131
 
                                                modeIdx = specialModes[specModeIdx];
1132
 
                                        }
1133
1174
                                        else
1134
 
                                        {
1135
 
                                                byDelayRamping = 1;             // ensure no ramping handling takes place for this hold
1136
 
                                                ExitSpecialModes();             // bail out of special modes
1137
 
                                        }
 
1175
                                                specModeIdx = specialModesCnt - 1;      // wrap around to last special mode
 
1176
                                        modeIdx = specialModes[specModeIdx];
1138
1177
                                }
1139
1178
                        }
1140
1179
 
1168
1207
                                                        modeIdx = BATT_CHECK_MODE;
1169
1208
                                                        byDelayRamping = 1;             // don't act on ramping button presses
1170
1209
                                                        SetOutput(OFF_OUTPUT);  // suppress main LED output
1171
 
                                                }
 
1210
                                                }
1172
1211
                                        }
1173
1212
                                        else if (modeIdx > SPECIAL_MODES)
1174
1213
                                        {
1178
1217
                                        {
1179
1218
                                                modeIdx = 0;            // clear main mode
1180
1219
                                                SetOutput(OFF_OUTPUT);  // suppress main LED output
1181
 
                        
 
1220
                        
1182
1221
                                                ConfigMode = 21;                // Initialize Advanced Config mode
1183
1222
                                                configClicks = 0;
1184
1223
                                                holdHandled = 1;                // suppress more hold events on this hold
1187
1226
                                        {
1188
1227
                                                modeIdx = 0;                    // clear main mode
1189
1228
                                                SetOutput(OFF_OUTPUT);  // suppress main LED output
1190
 
                                                
 
1229
                                                
1191
1230
                                                ConfigMode = 14;                // Reset to Factory Defaults
1192
1231
                                                configClicks = 0;
1193
1232
                                                holdHandled = 1;                // suppress more hold events on this hold
1194
1233
                                        }
1195
 
                                        else if (!ramping && (blinkyMode > 0) && (modeIdx != BATT_CHECK_MODE))
 
1234
                                        else if (!ramping && (blinkyMode > 0) && (modeIdx != BATT_CHECK_MODE) && !momentaryState)
1196
1235
                                        {
1197
1236
                                                // Engage first special mode!
1198
1237
                                                EnterSpecialModes ();
1203
1242
                        //---------------------------------------------------------------------------------------
1204
1243
                        // CONFIG hold - if it is not locked out or lock-out was just exited on this hold
1205
1244
                        //---------------------------------------------------------------------------------------
1206
 
                        if (byLockOutSet == 0)
1207
 
                        {
 
1245
                        if ((byLockOutSet == 0) && !momentaryState)
 
1246
                        {
1208
1247
                                if ((!ramping && (wPressDuration == CONFIG_ENTER_DUR) && (fastClicks != 2) && (modeIdx != BATT_CHECK_MODE))
1209
 
                                                                                        ||
1210
 
                                         (ramping && (wPressDuration == 500)))  // 8 secs in ramping mode
 
1248
                                                                                        ||
 
1249
                                         (ramping && (wPressDuration == 500)))  // 8 secs in ramping mode
1211
1250
                                {
1212
1251
                                        modeIdx = 0;
1213
1252
                                        rampingLevel = 0;
1214
1253
                                        rampState = 0;
1215
1254
                                        
1216
 
                                        // turn the light off initially
 
1255
                                        // turn the light off initially
1217
1256
                                        SetOutput(OFF_OUTPUT);  // suppress main LED output
1218
 
                                
 
1257
                                
1219
1258
                                        ConfigMode = 1;
1220
1259
                                        configClicks = 0;
1221
1260
                                }
1222
 
                        }
 
1261
                        }
1223
1262
                }
1224
1263
 
1225
1264
                wStepdownTicks = 0;             // Just always reset stepdown timer whenever the button is pressed
1259
1298
                {
1260
1299
                // Was previously pressed
1261
1300
 
 
1301
                        if (momentaryState)
 
1302
                        {
 
1303
                                outLevel = rampingLevel = 0;            // turn off output as soon as the user lets the switch go
 
1304
                                SetLevel(outLevel);
 
1305
                        }
 
1306
                        
1262
1307
                        //------------------------------------------------------------------------------
1263
1308
                        //      Ramping - button released
1264
1309
                        //------------------------------------------------------------------------------
1275
1320
                                        if (fastClicks == 1)
1276
1321
                                        {
1277
1322
                                                byModeForClicks = modeIdx;              // save current mode
1278
 
                                                if ((modeIdx < 16) && (rampingLevel == MAX_RAMP_LEVEL))
 
1323
                                                if ((modeIdx < 16) && (rampingLevel == TURBO_LEVEL))
1279
1324
                                                        byModeForClicks = 255;
1280
1325
                                                
1281
1326
                                                if ((modeIdx == BATT_CHECK_MODE) || (modeIdx == TEMP_CHECK_MODE) || (modeIdx == FIRM_VERS_MODE))
1284
1329
                                                        outLevel = rampingLevel = 0;    // 08/28/16 TE: zero out outLevel here as well (used in LVP)
1285
1330
                                                        byDelayRamping = 1;             // don't act on ramping button presses
1286
1331
                                                }
 
1332
                                                
 
1333
                                                initialLevelForClicks = rampingLevel;
1287
1334
 
1288
 
                                                if (!byLockOutSet && !byDelayRamping)
 
1335
                                                if (!byLockOutSet && !byDelayRamping && !momentaryState)
1289
1336
                                                {
1290
1337
                                                        //---------------------------------
1291
1338
                                                        // Normal click ON in ramping mode
1303
1350
 
1304
1351
                                                          #ifdef ONBOARD_LED
1305
1352
                                                                // Blink the Ind. LED from restoring last level
1306
 
                                                                if (onboardLedEnable)
1307
 
                                                                        byBlinkActiveChan = 1;
 
1353
                                                                if (onboardLedEnable)
 
1354
                                                                        byBlinkActiveChan = 1;
1308
1355
                                                          #endif
 
1356
                                                          
 
1357
                                                                wThermalTicks = TEMP_ADJ_PERIOD/3;      // Delay a temperature step down at least 15 secs or so
1309
1358
                                                        }
1310
1359
                                                        else                                    // light is ON, turn it OFF
1311
1360
                                                        {
1314
1363
 
1315
1364
                                                        if (outLevel == 255)
1316
1365
                                                        {
1317
 
                                                          #if OUT_CHANNELS == 3         // Triple Channel
 
1366
                                                         #if OUT_CHANNELS == 3          // Triple Channel
1318
1367
                                                                SetOutput(moonlightLevel,0,0);
1319
 
                                                          #elif OUT_CHANNELS == 2       // two channels
1320
 
                                                                SetOutput(moonlightLevel,0);
1321
 
                                                          #else
1322
 
                                                                SetOutput(moonlightLevel);
1323
 
                                                          #endif
 
1368
                                                         #elif OUT_CHANNELS == 2        // two channels
 
1369
                                                          #ifdef GT_BUCK
 
1370
                                                                SetOutput(moonlightLevel,25); // for GT-buck
 
1371
                                                          #else
 
1372
                                                                SetOutput(moonlightLevel,0);
 
1373
                                                          #endif
 
1374
                                                         #else
 
1375
                                                                SetOutput(moonlightLevel);
 
1376
                                                         #endif
1324
1377
 
1325
1378
                                                          #ifdef ONBOARD_LED
1326
 
                                                                if (locatorLed)
 
1379
                                                                if (locatorLed)
1327
1380
                                                                        TurnOnBoardLed(0);
1328
1381
                                                          #endif
1329
1382
                                                        }
1338
1391
                                        
1339
1392
                                  #ifdef ONBOARD_LED
1340
1393
                                        // Blink the Ind. LED from a ramping hold
1341
 
                                        if (onboardLedEnable)
1342
 
                                                byBlinkActiveChan = 1;
 
1394
                                        if (onboardLedEnable)
 
1395
                                                byBlinkActiveChan = 1;
1343
1396
                                  #endif
1344
1397
                                }
1345
1398
                                
1348
1401
                                byDelayRamping = 0;     // restore acting on ramping button presses, if disabled
1349
1402
                        } // ramping
1350
1403
 
1351
 
                        //------------------------------------------------------------------------------
1352
 
                        //      Non-Ramping - button released
1353
 
                        //------------------------------------------------------------------------------
 
1404
                        //------------------------------------------------------------------------------
 
1405
                        //      Non-Ramping - button released
 
1406
                        //------------------------------------------------------------------------------
1354
1407
                        else
1355
1408
                        {
1356
1409
                                if (wPressDuration < LONG_PRESS_DUR)
1374
1427
                                                {
1375
1428
                                                        if (modeMemoryEnabled && (modeMemoryLastModeIdx > 0) && (modeIdx == 0))
1376
1429
                                                        {
1377
 
                                                                modeIdx = modeMemoryLastModeIdx;
1378
 
                                                                modeMemoryLastModeIdx = 0;
 
1430
                                                                if (!momentaryState)
 
1431
                                                                {
 
1432
                                                                        modeIdx = modeMemoryLastModeIdx;
 
1433
                                                                        modeMemoryLastModeIdx = 0;
 
1434
                                                                }
1379
1435
                                                        }
1380
1436
                                                        else if (modeIdx < 16)
1381
1437
                                                        {
1382
 
                                                                if ((modeIdx > 0) && (wIdleTicks >= IDLE_TIME))
1383
 
                                                                {
1384
 
                                                                        modeMemoryLastModeIdx = modeIdx;
1385
 
                                                                        prevModeIdx = modeIdx;
1386
 
                                                                        modeIdx = 0;    // Turn OFF the light
1387
 
                                                                }
1388
 
                                                                else
1389
 
                                                                {
1390
 
                                                                        // Short press - normal modes
1391
 
                                                                        if (highToLow)
1392
 
                                                                                PrevMode();
 
1438
                                                                if (!momentaryState)
 
1439
                                                                {
 
1440
                                                                        if ((modeIdx > 0) && (wIdleTicks >= IDLE_TIME))
 
1441
                                                                        {
 
1442
                                                                                modeMemoryLastModeIdx = modeIdx;
 
1443
                                                                                prevModeIdx = modeIdx;
 
1444
                                                                                modeIdx = 0;    // Turn OFF the light
 
1445
                                                                        }
1393
1446
                                                                        else
1394
 
                                                                                NextMode();
 
1447
                                                                        {
 
1448
                                                                                // Short press - normal modes
 
1449
                                                                                if (highToLow)
 
1450
                                                                                        PrevMode();
 
1451
                                                                                else
 
1452
                                                                                        NextMode();
 
1453
                                                                        }
1395
1454
                                                                }
1396
1455
                                                        }
1397
1456
                                                        else  // special modes
1454
1513
                                // Process multi clicks here, after it times out
1455
1514
                                //-----------------------------------------------------------------------------------
1456
1515
 
 
1516
                                if (fastClicks == 5)    // --> 5X clicks: toggle momentary mode (click&hold - light ON at max)
 
1517
                                {
 
1518
                                        if (ramping && !byLockOutSet && !momentaryState)
 
1519
                                        {
 
1520
                                                momentaryState = 1;
 
1521
                                                modeIdx = outLevel = rampingLevel = 0;
 
1522
                                                SetOutput(OFF_OUTPUT);  // suppress main LED output
 
1523
                                        }
 
1524
                                }
 
1525
 
1457
1526
                                // For Lock-Out, always support 4 quick clicks regardless of mode/state
1458
1527
                          #ifdef LOCKOUT_ENABLE
1459
 
                                if (fastClicks == 4)    // --> 4X clicks: do a lock-out
 
1528
                                else if (fastClicks == 4)       // --> 4X clicks: do a lock-out
1460
1529
                                {
1461
 
                                        modeIdx = outLevel = rampingLevel = 0;
1462
 
                                        rampState = 0;
1463
 
                                        byLockOutSet = 1 - byLockOutSet;                // invert "LOCK OUT"
1464
 
                                        byDelayRamping = 1;             // don't act on ramping button presses (ramping ON/OFF code below)
 
1530
                                        if ((ramping || byLockOutSet) && !momentaryState)       // only for ramping, or un-locking out
 
1531
                                        {
 
1532
                                                modeIdx = outLevel = rampingLevel = 0;
 
1533
                                                rampState = 0;
 
1534
                                                byLockOutSet = 1 - byLockOutSet;                // invert "LOCK OUT"
 
1535
                                                byDelayRamping = 1;             // don't act on ramping button presses (ramping ON/OFF code below)
 
1536
                                        }
1465
1537
                                }
1466
 
                                else
1467
1538
                          #endif
1468
1539
 
1469
1540
                                // Support 2X clicks for Batt and Temp blink outs in ramping
1470
 
                                if ((byModeForClicks == BATT_CHECK_MODE) || (byModeForClicks == TEMP_CHECK_MODE))       // battery check - multi-click checks
 
1541
                                else if ((byModeForClicks == BATT_CHECK_MODE) || (byModeForClicks == TEMP_CHECK_MODE))  // battery check - multi-click checks
1471
1542
                                {
1472
1543
                                        if (fastClicks == 2)                    // --> double click: blink out the firmware vers #
1473
1544
                                        {
1479
1550
                                                SetOutput(OFF_OUTPUT);  // suppress main LED output
1480
1551
                                        }
1481
1552
                                }
1482
 
                                else if (ramping && !byLockOutSet)
 
1553
                                else if (ramping && !byLockOutSet  && !momentaryState)
1483
1554
                                {
1484
1555
                                  #ifdef TRIPLE_CLICK_BATT
1485
1556
                                        if (fastClicks == 3)                                            // --> triple click: display battery check/status
1492
1563
 
1493
1564
                                        if (fastClicks == 2)                                            // --> double click: set to MAX level
1494
1565
                                        {
1495
 
                                                if (byModeForClicks == 255)
 
1566
                                                if (byModeForClicks == 255)     // Turbo
1496
1567
                                                {
1497
1568
                                                        if (blinkyMode > 0) // must be enabled
1498
1569
                                                                EnterSpecialModes ();   // Engage first special mode!
 
1570
                                                        else
 
1571
                                                        {
 
1572
                                                                // Restore the saved level (last mode memory)
 
1573
                                                                if (savedLevel != MAX_RAMP_LEVEL)
 
1574
                                                                        outLevel = rampingLevel = turboRestoreLevel;
 
1575
                                                                else
 
1576
                                                                        outLevel = rampingLevel = 0;
 
1577
                                                                SetLevel(outLevel);
 
1578
                                                        }
1499
1579
                                                }
1500
1580
                                                else
1501
1581
                                                {
1502
 
                                                        rampingLevel = MAX_RAMP_LEVEL;
 
1582
                                                        rampingLevel = TURBO_LEVEL;
1503
1583
                                                        outLevel = rampingLevel;
1504
1584
                                                        SetLevel(outLevel);
 
1585
                                                        turboRestoreLevel = initialLevelForClicks;
 
1586
 
 
1587
                                                        wThermalTicks = TEMP_ADJ_PERIOD/3;      // Delay a temperature step down at least 15 secs or so
1505
1588
                                                }
1506
1589
                                        }
1507
 
                                }
1508
 
 
 
1590
                                } // ramping && !byLockOutSet
1509
1591
 
1510
1592
                                fastClicks = 0; // clear it after processing it
1511
 
                        }
 
1593
                                
 
1594
                        } // wIdleTicks > DBL_CLICK_TICKS
1512
1595
 
1513
1596
                        // Only do timed stepdown check when switch isn't pressed
1514
1597
                        if (stepdownMode > 1)
1658
1741
        // Load config settings: mode, mode set, lo-hi, mode memory
1659
1742
        LoadConfig();
1660
1743
        
1661
 
        // Load the configured temperature to use as the threshold to step down output
 
1744
        // Load the configured temperature to use as the threshold to step down output
1662
1745
   byte inValue = eeprom_read_byte((const byte *)250);  // use address 250 in the EEPROM space
1663
1746
        if ((inValue > 0) && (inValue < 255))
1664
 
                byStepdownTemp = inValue;
 
1747
                byStepdownTemp = inValue;
1665
1748
 
1666
1749
        DefineModeSet();
1667
1750
 
1684
1767
                modeIdx = modesCnt - 1;
1685
1768
                if (ramping)
1686
1769
                {
1687
 
                        outLevel = rampingLevel = MAX_RAMP_LEVEL;
 
1770
                        outLevel = rampingLevel = TURBO_LEVEL;
1688
1771
                        SetLevel(outLevel);
1689
1772
                }
1690
1773
          #endif
1704
1787
 
1705
1788
        last_modeIdx = 250;     // make it invalid for first time
1706
1789
        
1707
 
   byte byPrevLockOutSet = 0;
 
1790
   byte byPrevLockOutSet = 0;                   // copy of byLockOutSet to detect transitions
 
1791
        byte byPrevMomentaryState = 0;  // copy of momentaryState to detect transitions
1708
1792
 
1709
1793
   byte prevConfigClicks = 0;
1710
1794
 
1725
1809
                if (ConfigMode == 0)                                    // Normal mode
1726
1810
      //---------------------------------------------------------------------------------------
1727
1811
                {
 
1812
                        // Handle Lockout mode transtions
1728
1813
                        if (byPrevLockOutSet != byLockOutSet)
1729
1814
                        {
1730
1815
                                byPrevLockOutSet = byLockOutSet;
1731
1816
 
1732
1817
                                SetOutput(OFF_OUTPUT);
1733
 
                                _delay_ms(250);
1734
 
                                Blink(4, 60);
 
1818
                                Blink(4, 75);
1735
1819
                                
1736
1820
                                byDelayRamping = 0;             // restore acting on ramping button presses
1737
1821
                                
1741
1825
                                        last_modeIdx = 255;             // exiting - force a mode handling to turn on locator LED
1742
1826
                        }
1743
1827
 
 
1828
                        // Handle momentary/tactical mode transitions
 
1829
                        if (byPrevMomentaryState != momentaryState)
 
1830
                        {
 
1831
                                byPrevMomentaryState = momentaryState;
 
1832
 
 
1833
                                Blink(2, 60);           // Special blink sequence for momentary mode (into or out of)
 
1834
                                _delay_ms(250);
 
1835
                                Blink(3, 75);
 
1836
                        }
 
1837
 
1744
1838
                        //---------------------------------------------------
1745
1839
                        // Mode Handling - did the WDT change the mode?
1746
1840
                        //---------------------------------------------------
1908
2002
                                                {
1909
2003
                                                        // Reached critical battery level
1910
2004
                                                        outLevel = 7;   // bump it up a little for final shutoff blinks
1911
 
                                                        BlinkLVP(8);    // blink more and quicker (to get attention)
 
2005
                                                        BlinkLVP(8);    // blink more and quicker (to get attention)
1912
2006
                                                        outLevel = rampingLevel = 0;    // Shut it down
1913
2007
                                                        rampState = 0;
1914
2008
                                                }
1915
2009
                                                else
1916
2010
                                                {
1917
2011
                                                        // Drop the output level
1918
 
                                                        BlinkLVP(3);    // normal 3 blinks
 
2012
                                                        BlinkLVP(3);    // normal 3 blinks
1919
2013
                                                        if (outLevel > MAX_RAMP_LEVEL/16)
1920
2014
                                                                outLevel = ((int)outLevel * 4) / 5;     // drop the level (index) by 20%
1921
2015
                                                        else
1927
2021
                                }
1928
2022
                                else   // Not ramping
1929
2023
                                {
1930
 
                                        if (modeIdx > 0)
1931
 
                                        {
1932
 
                                                if (modeIdx == 1)
1933
 
                                                {
 
2024
                                        if (modeIdx > 0)
 
2025
                                        {
 
2026
                                                if (modeIdx == 1)
 
2027
                                                {
1934
2028
                                                        // Reached critical battery level
1935
 
                                                        BlinkLVP(8);    // blink more and quicker (to get attention)
1936
 
                                                }
1937
 
                                                else
1938
 
                                                {
 
2029
                                                        BlinkLVP(8);    // blink more and quicker (to get attention)
 
2030
                                                }
 
2031
                                                else
 
2032
                                                {
1939
2033
                                                        // Drop the output level
1940
 
                                                        BlinkLVP(3);    // normal 3 blinks
1941
 
                                                }
 
2034
                                                        BlinkLVP(3);    // normal 3 blinks
 
2035
                                                }
1942
2036
                                                if (modeIdx < 16)
1943
2037
                                                        PrevMode();
1944
 
                                        }
 
2038
                                        }
1945
2039
                                }
1946
2040
                                LowBattSignal = 0;
1947
2041
                        }
1951
2045
                                // Blink the Indicator LED twice
1952
2046
                                if (onboardLedEnable)
1953
2047
                                {
1954
 
                                        if ((modeIdx > 0) || (locatorLed == 0))
 
2048
                                        // Be sure to leave the indicator LED in it's proper state after blinking twice
 
2049
                                        if ((locatorLed != 0) &&
 
2050
                                                        ((ramping && (outLevel != 0)) || (!ramping && (modeIdx > 0))))
1955
2051
                                        {
1956
2052
                                                BlinkIndLed(500, 2);
1957
2053
                                        }
1958
2054
                                        else
1959
2055
                                        {
1960
 
                                                TurnOnBoardLed(0);
1961
 
                                                _delay_ms(500);
1962
 
                                                TurnOnBoardLed(1);
1963
 
                                                _delay_ms(500);
1964
 
                                                TurnOnBoardLed(0);
1965
 
                                                _delay_ms(500);
1966
 
                                                TurnOnBoardLed(1);
 
2056
                                                TurnOnBoardLed(0);
 
2057
                                                _delay_ms(500);
 
2058
                                                TurnOnBoardLed(1);
 
2059
                                                _delay_ms(500);
 
2060
                                                TurnOnBoardLed(0);
 
2061
                                                _delay_ms(500);
 
2062
                                                TurnOnBoardLed(1);
1967
2063
                                        }
1968
2064
                                }
1969
2065
#endif
1978
2074
                        {
1979
2075
                                if (ramping)
1980
2076
                                {
1981
 
                                        if (outLevel >= FET_START_LVL)
 
2077
                                        if ((outLevel >= FET_START_LVL) && (outLevel < 255))    // 255 is special value for moon mode (use 1)
1982
2078
                                        {
1983
2079
                                                int newLevel = outLevel - outLevel/6;   // reduce by 16.7%
1984
2080
                                        
1985
2081
                                                if (newLevel >= FET_START_LVL)
1986
 
                                                        outLevel = newLevel;    // reduce by 16.7%
1987
 
                                                else
1988
 
                                                        outLevel = FET_START_LVL - 1;   // make it the max of the 7135
 
2082
                                                        outLevel = newLevel;    // reduce by 16.7%
 
2083
                                                else
 
2084
                                                        outLevel = FET_START_LVL - 1;   // make it the max of the 7135
1989
2085
                                        
1990
2086
                                                SetLevel(outLevel);
1991
2087
                                                
1992
2088
                                                wThermalTicks = TEMP_ADJ_PERIOD;
1993
2089
                                          #ifdef ONBOARD_LED
1994
2090
                                                BlinkIndLed(500, 3);    // Flash the Ind LED to show it's lowering
1995
 
                                          #endif
 
2091
                                          #endif
1996
2092
                                        }
1997
2093
                                }
1998
2094
                                else    // modes
2004
2100
                                                wThermalTicks = TEMP_ADJ_PERIOD;
2005
2101
                                          #ifdef ONBOARD_LED
2006
2102
                                                BlinkIndLed(500, 3);    // Flash the Ind LED to show it's lowering
2007
 
                                          #endif
2008
 
                                        }
 
2103
                                          #endif
 
2104
                                        }
2009
2105
                                }
2010
2106
                        } // thermal step down
2011
2107
 
2023
2119
#ifdef ONBOARD_LED
2024
2120
                                // If the battery is currently low, then turn OFF the indicator LED before going to sleep
2025
2121
                                //  to help in saving the battery
2026
 
                          #ifdef VOLT_MON_R1R2
2027
 
                                if (byVoltage < ADC_LOW)
2028
 
                          #else
2029
 
                                if (byVoltage < BATT_LOW)
2030
 
                          #endif
 
2122
                          #ifdef VOLT_MON_R1R2
 
2123
                                if (byVoltage < ADC_LOW)
 
2124
                          #else
 
2125
                                if (byVoltage < BATT_LOW)
 
2126
                          #endif
2031
2127
                                if (locatorLed)
2032
2128
                                        TurnOnBoardLed(0);
2033
2129
#endif
2072
2168
                                        ++ConfigMode;
2073
2169
                                        break;
2074
2170
                                        
2075
 
                                 case 3:        // 1 - (exiting) ramping mode selection
 
2171
                                 case 3:        // 1 - (exiting) ramping mode selection
2076
2172
                                        if ((configClicks > 0) && (configClicks <= 8))
2077
2173
                                        {
2078
2174
                                                ramping = 1 - (configClicks & 1);
2082
2178
                                        break;
2083
2179
 
2084
2180
                                case 14:        // Reset to Factory Default settings
2085
 
                                        SetConfigDefaults();
2086
 
                                        SaveConfig();
2087
 
                                        
 
2181
                                        SetConfigDefaults();
 
2182
                                        SaveConfig();
 
2183
                                        
2088
2184
                                        locatorLed = locatorLedOn;
2089
2185
                                        DefineModeSet();
2090
2186
                                        byStepdownTemp = DEFAULT_STEPDOWN_TEMP; // the current temperature in C for doing the stepdown
2117
2213
                                        configClicks = 0;
2118
2214
                                        break;
2119
2215
                                        
2120
 
                                case 23:        // 1 - (exiting) locator LED ON selection
 
2216
                                case 23:        // 1 - (exiting) locator LED ON selection
2121
2217
                                        if (configClicks)
2122
2218
                                        {
2123
2219
                                                locatorLedOn = 1 - (configClicks & 1);
2127
2223
                                        ConfigBlink(2);
2128
2224
                                        break;
2129
2225
                                        
2130
 
                                case 24:        // 2 - (exiting) BVLD LED config selection
 
2226
                                case 24:        // 2 - (exiting) BVLD LED config selection
2131
2227
                                        if (configClicks)
2132
2228
                                        {
2133
2229
                                                bvldLedOnly = 1 - (configClicks & 1);
2136
2232
                                        ConfigBlink(3);
2137
2233
                                        break;
2138
2234
                                        
2139
 
                                case 25:        // 3 - (exiting) Indicator LED enable selection
 
2235
                                case 25:        // 3 - (exiting) Indicator LED enable selection
2140
2236
                                        if (configClicks)
2141
2237
                                        {
2142
2238
                                                onboardLedEnable = 1 - (configClicks & 1);
2145
2241
                                        ConfigMode = 15;                        // all done, go to exit
2146
2242
                                        break;
2147
2243
 
2148
 
                                case 100:       // thermal calibration in effect
2149
 
                                        Blink(3, 40);                   // 3 quick blinks
2150
 
                                        SetOutput(MAX_BRIGHTNESS);      // set max output
 
2244
                                case 100:       // thermal calibration in effect
 
2245
                                        Blink(3, 40);                   // 3 quick blinks
 
2246
                                        SetOutput(MAX_BRIGHTNESS);      // set max output
2151
2247
                                        wThermalTicks = 312;    // set for 5 seconds as the minimum time to set a new stepdown temperature
2152
2248
                                        break;
2153
2249
 
2154
 
                                case 101:       // exiting thermal calibration
2155
 
                                        SetOutput(OFF_OUTPUT);
2156
 
                                
2157
 
                                        if (wThermalTicks == 0) // min. time exceeded
2158
 
                                        {
2159
 
                                                // Save the current temperature to use as the threshold to step down output
2160
 
                                                byStepdownTemp = byTempReading;
 
2250
                                case 101:       // exiting thermal calibration
 
2251
                                        SetOutput(OFF_OUTPUT);
 
2252
                                
 
2253
                                        if (wThermalTicks == 0) // min. time exceeded
 
2254
                                        {
 
2255
                                                // Save the current temperature to use as the threshold to step down output
 
2256
                                                byStepdownTemp = byTempReading;
2161
2257
                                                WrEEPROM (250, byStepdownTemp);
2162
 
                                        }
2163
 
                                
 
2258
                                        }
 
2259
                                
2164
2260
                                        // Continue to next setting (blinky modes)
2165
2261
                                        if (ramping)
2166
2262
                                        {
2174
2270
                                        }
2175
2271
                                        break;
2176
2272
 
2177
 
                                case 40:                // timed stepdown calibration in effect (do nothing here)
 
2273
                                case 40:                // timed stepdown calibration in effect (do nothing here)
2178
2274
                                        break;
2179
2275
 
2180
 
                                case 41:        // (exiting) timed stepdown
 
2276
                                case 41:        // (exiting) timed stepdown
2181
2277
                                        if ((configClicks > 0) && (configClicks <= 6))
2182
2278
                                        {
2183
2279
                                                stepdownMode = configClicks + 1;
2208
2304
                                                //---------------------------------------------------------------------
2209
2305
                                                switch (ConfigMode)
2210
2306
                                                {
2211
 
                                                case 4: // 2 - (exiting) moonlight level selection
 
2307
                                                case 4: // 2 - (exiting) moonlight level selection
2212
2308
                                                        if ((configClicks > 0) && (configClicks <= 7))
2213
2309
                                                        {
2214
2310
                                                                moonlightLevel = configClicks;
2229
2325
                                                        {
2230
2326
                                                                stepdownMode = 1;
2231
2327
                                                                SaveConfig();
2232
 
                                                                ConfigMode = 100;               // thermal configuration in effect!!
 
2328
                                                                ConfigMode = 100;               // thermal configuration in effect!!
2233
2329
                                                        }
2234
 
                                                        else if (configClicks == 3)   // timed stepdown
 
2330
                                                        else if (configClicks == 3)   // timed stepdown
2235
2331
                                                        {
2236
 
                                                                        Blink(3, 40);                   // 3 quick blinks
2237
 
                                                                        ConfigMode = 40;
 
2332
                                                                        Blink(3, 40);                   // 3 quick blinks
 
2333
                                                                        ConfigMode = 40;
2238
2334
                                                        }
2239
2335
                                                        else
2240
2336
                                                                ConfigBlink(4);
2257
2353
                                                //---------------------------------------------------------------------
2258
2354
                                                switch (ConfigMode)     
2259
2355
                                                {
2260
 
                                                case 4: // 2 - (exiting) mode set selection
 
2356
                                                case 4: // 2 - (exiting) mode set selection
2261
2357
                                                        if ((configClicks > 0) && (configClicks <= 12))
2262
2358
                                                        {
2263
2359
                                                                modeSetIdx = configClicks - 1;
2267
2363
                                                        ConfigBlink(3);
2268
2364
                                                        break;
2269
2365
                                                        
2270
 
                                                case 5: // 3 - (exiting) moonlight enabling
 
2366
                                                case 5: // 3 - (exiting) moonlight enabling
2271
2367
                                                        if (configClicks)
2272
2368
                                                        {
2273
2369
                                                                moonLightEnable = 1 - (configClicks & 1);
2277
2373
                                                        ConfigBlink(4);
2278
2374
                                                        break;
2279
2375
 
2280
 
                                                case 6: // 4 - (exiting) mode order setting
 
2376
                                                case 6: // 4 - (exiting) mode order setting
2281
2377
                                                        if (configClicks)
2282
2378
                                                        {
2283
2379
                                                                highToLow = 1 - (configClicks & 1);
2295
2391
                                                        ConfigBlink(6);
2296
2392
                                                        break;
2297
2393
 
2298
 
                                                case 8: // 6 - (exiting) moonlight level selection
 
2394
                                                case 8: // 6 - (exiting) moonlight level selection
2299
2395
                                                        if ((configClicks > 0) && (configClicks <= 7))
2300
2396
                                                        {
2301
2397
                                                                moonlightLevel = configClicks;
2316
2412
                                                        {
2317
2413
                                                                stepdownMode = 1;
2318
2414
                                                                SaveConfig();
2319
 
                                                                ConfigMode = 100;               // thermal configuration in effect!!
 
2415
                                                                ConfigMode = 100;               // thermal configuration in effect!!
2320
2416
                                                        }
2321
 
                                                        else if (configClicks == 3)   // timed stepdown
 
2417
                                                        else if (configClicks == 3)   // timed stepdown
2322
2418
                                                        {
2323
 
                                                                        Blink(3, 40);                   // 3 quick blinks
2324
 
                                                                        ConfigMode = 40;
 
2419
                                                                        Blink(3, 40);                   // 3 quick blinks
 
2420
                                                                        ConfigMode = 40;
2325
2421
                                                        }
2326
2422
                                                        else
2327
2423
                                                                ConfigBlink(8);