Advertisement
Guest User

Untitled

a guest
Sep 8th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.56 KB | None | 0 0
  1. /*
  2.  * BLF EE A6 firmware (special-edition group buy light)
  3.  * This light uses a FET+1 style driver, with a FET on the main PWM channel
  4.  * for the brightest high modes and a single 7135 chip on the secondary PWM
  5.  * channel so we can get stable, efficient low / medium modes.  It also
  6.  * includes a capacitor for measuring off time.
  7.  *
  8.  * Copyright (C) 2015 Selene Scriven
  9.  *
  10.  * This program is free software: you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation, either version 3 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  22.  *
  23.  *
  24.  * NANJG 105C Diagram
  25.  *           ---
  26.  *         -|   |- VCC
  27.  *     OTC -|   |- Voltage ADC
  28.  *  Star 3 -|   |- PWM (FET)
  29.  *     GND -|   |- PWM (1x7135)
  30.  *           ---
  31.  *
  32.  * FUSES
  33.  *      I use these fuse settings
  34.  *      Low:  0x75  (4.8MHz CPU without 8x divider, 9.4kHz phase-correct PWM or 18.75kHz fast-PWM)
  35.  *      High: 0xfd  (to enable brownout detection)
  36.  *
  37.  *      For more details on these settings, visit http://github.com/JCapSolutions/blf-firmware/wiki/PWM-Frequency
  38.  *
  39.  * STARS
  40.  *      Star 2 = second PWM output channel
  41.  *      Star 3 = mode memory if soldered, no memory by default
  42.  *      Star 4 = Capacitor for off-time
  43.  *
  44.  * VOLTAGE
  45.  *      Resistor values for voltage divider (reference BLF-VLD README for more info)
  46.  *      Reference voltage can be anywhere from 1.0 to 1.2, so this cannot be all that accurate
  47.  *
  48.  *           VCC
  49.  *            |
  50.  *           Vd (~.25 v drop from protection diode)
  51.  *            |
  52.  *          1912 (R1 19,100 ohms)
  53.  *            |
  54.  *            |---- PB2 from MCU
  55.  *            |
  56.  *          4701 (R2 4,700 ohms)
  57.  *            |
  58.  *           GND
  59.  *
  60.  *   To find out what values to use, flash the driver with battcheck.hex
  61.  *   and hook the light up to each voltage you need a value for.  This is
  62.  *   much more reliable than attempting to calculate the values from a
  63.  *   theoretical formula.
  64.  *
  65.  *   Same for off-time capacitor values.  Measure, don't guess.
  66.  */
  67. #define F_CPU 4800000UL
  68.  
  69. /*
  70.  * =========================================================================
  71.  * Settings to modify per driver
  72.  */
  73.  
  74. //#define FAST 0x23           // fast PWM channel 1 only
  75. //#define PHASE 0x21          // phase-correct PWM channel 1 only
  76. #define FAST 0xA3           // fast PWM both channels
  77. #define PHASE 0xA1          // phase-correct PWM both channels
  78.  
  79. #define VOLTAGE_MON         // Comment out to disable LVP
  80. #define OWN_DELAY           // Should we use the built-in delay or our own?
  81. // Adjust the timing per-driver, since the hardware has high variance
  82. // Higher values will run slower, lower values run faster.
  83. #define DELAY_TWEAK         950
  84.  
  85. #define OFFTIM3             // Use short/med/long off-time presses
  86.                             // instead of just short/long
  87.  
  88. // comment out to use extended config mode instead of a solderable star
  89. // (controls whether mode memory is on the star or if it's a setting in config mode)
  90. //#define CONFIG_STARS
  91.  
  92. // output to use for blinks on battery check mode (primary PWM level, alt PWM level)
  93. // Use 20,0 for a single-channel driver or 0,20 for a two-channel driver
  94. #define BLINK_BRIGHTNESS    0,20
  95.  
  96. // Mode group 1
  97. #define NUM_MODES1          7
  98. // PWM levels for the big circuit (FET or Nx7135)
  99. #define MODESNx1            0,0,0,7,56,137,255
  100. // PWM levels for the small circuit (1x7135)
  101. #define MODES1x1            2,20,110,255,255,255,0
  102. // My sample:     6=0..6,  7=2..11,  8=8..21(15..32)
  103. // Krono sample:  6=5..21, 7=17..32, 8=33..96(50..78)
  104. // Manker2:       2=21, 3=39, 4=47, ... 6?=68
  105. // PWM speed for each mode
  106. #define MODES_PWM1          PHASE,FAST,FAST,FAST,FAST,FAST,PHASE
  107. // Mode group 2
  108. #define NUM_MODES2          4
  109. #define MODESNx2            0,0,90,255
  110. #define MODES1x2            20,230,255,0
  111. #define MODES_PWM2          FAST,FAST,FAST,PHASE
  112. // Hidden modes are *before* the lowest (moon) mode, and should be specified
  113. // in reverse order.  So, to go backward from moon to turbo to strobe to
  114. // battcheck, use BATTCHECK,STROBE,TURBO .
  115. #define NUM_HIDDEN          4
  116. #define HIDDENMODES         BIKING_STROBE,BATTCHECK,STROBE,TURBO
  117. #define HIDDENMODES_PWM     PHASE,PHASE,PHASE,PHASE
  118. #define HIDDENMODES_ALT     0,0,0,0   // Zeroes, same length as NUM_HIDDEN
  119.  
  120. #define TURBO     255       // Convenience code for turbo mode
  121. #define BATTCHECK 254       // Convenience code for battery check mode
  122. // Uncomment to enable tactical strobe mode
  123. #define STROBE    253       // Convenience code for strobe mode
  124. // Uncomment to unable a 2-level stutter beacon instead of a tactical strobe
  125. #define BIKING_STROBE 252   // Convenience code for biking strobe mode
  126. // comment out to use minimal version instead (smaller)
  127. #define FULL_BIKING_STROBE
  128.  
  129. #define NON_WDT_TURBO            // enable turbo step-down without WDT
  130. // How many timer ticks before before dropping down.
  131. // Each timer tick is 500ms, so "60" would be a 30-second stepdown.
  132. // Max value of 255 unless you change "ticks"
  133. #define TURBO_TIMEOUT       120
  134.  
  135. // These values were measured using wight's "A17HYBRID-S" driver built by DBCstm.
  136. // Your mileage may vary.
  137. #define ADC_42          197 // the ADC value we expect for 4.20 volts
  138. #define ADC_100         197 // the ADC value for 100% full (4.2V resting)
  139. #define ADC_75          187 // the ADC value for 75% full (4.0V resting)
  140. #define ADC_50          177 // the ADC value for 50% full (3.8V resting)
  141. #define ADC_25          163 // the ADC value for 25% full (3.5V resting)
  142. #define ADC_0           141 // the ADC value for 0% full (3.0V resting)
  143. #define ADC_LOW         131 // When do we start ramping down (2.8V)
  144. #define ADC_CRIT        126 // When do we shut the light off (2.7V)
  145. // These values were copied from s7.c.
  146. // Your mileage may vary.
  147. //#define ADC_42          185 // the ADC value we expect for 4.20 volts
  148. //#define ADC_100         185 // the ADC value for 100% full (4.2V resting)
  149. //#define ADC_75          175 // the ADC value for 75% full (4.0V resting)
  150. //#define ADC_50          164 // the ADC value for 50% full (3.8V resting)
  151. //#define ADC_25          154 // the ADC value for 25% full (3.5V resting)
  152. //#define ADC_0           139 // the ADC value for 0% full (3.0V resting)
  153. //#define ADC_LOW         123 // When do we start ramping down (2.8V)
  154. //#define ADC_CRIT        113 // When do we shut the light off (2.7V)
  155. // Values for testing only:
  156. //#define ADC_LOW         125 // When do we start ramping down (2.8V)
  157. //#define ADC_CRIT        124 // When do we shut the light off (2.7V)
  158.  
  159. // the BLF EE A6 driver may have different offtime cap values than most other drivers
  160. // Values are between 1 and 255, and can be measured with offtime-cap.c
  161. // These #defines are the edge boundaries, not the center of the target.
  162. #ifdef OFFTIM3
  163. #define CAP_SHORT           235  // Anything higher than this is a short press
  164. #define CAP_MED             175  // Between CAP_MED and CAP_SHORT is a medium press
  165.                                  // Below CAP_MED is a long press
  166. #else
  167. #define CAP_SHORT           180  // Anything higher than this is a short press, lower is a long press
  168. #endif
  169.  
  170. /*
  171.  * =========================================================================
  172.  */
  173.  
  174. // Ignore a spurious warning, we did the cast on purpose
  175. #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
  176.  
  177. #ifdef OWN_DELAY
  178. #include <util/delay_basic.h>
  179. // Having own _delay_ms() saves some bytes AND adds possibility to use variables as input
  180. void _delay_ms(uint16_t n)
  181. {
  182.     // TODO: make this take tenths of a ms instead of ms,
  183.     // for more precise timing?
  184.     while(n-- > 0) _delay_loop_2(DELAY_TWEAK);
  185. }
  186. void _delay_s()  // because it saves a bit of ROM space to do it this way
  187. {
  188.     _delay_ms(1000);
  189. }
  190. #else
  191. #include <util/delay.h>
  192. #endif
  193.  
  194. #include <avr/pgmspace.h>
  195. //#include <avr/io.h>
  196. //#include <avr/interrupt.h>
  197. #include <avr/eeprom.h>
  198. #include <avr/sleep.h>
  199. //#include <avr/power.h>
  200.  
  201. #define STAR2_PIN   PB0     // But note that there is no star 2.
  202. #define STAR3_PIN   PB4
  203. #define CAP_PIN     PB3
  204. #define CAP_CHANNEL 0x03    // MUX 03 corresponds with PB3 (Star 4)
  205. #define CAP_DIDR    ADC3D   // Digital input disable bit corresponding with PB3
  206. #define PWM_PIN     PB1
  207. #define ALT_PWM_PIN PB0
  208. #define VOLTAGE_PIN PB2
  209. #define ADC_CHANNEL 0x01    // MUX 01 corresponds with PB2
  210. #define ADC_DIDR    ADC1D   // Digital input disable bit corresponding with PB2
  211. #define ADC_PRSCL   0x06    // clk/64
  212.  
  213. #define PWM_LVL     OCR0B   // OCR0B is the output compare register for PB1
  214. #define ALT_PWM_LVL OCR0A   // OCR0A is the output compare register for PB0
  215.  
  216. /*
  217.  * global variables
  218.  */
  219.  
  220. // Config / state variables
  221. uint8_t eepos = 0;
  222. uint8_t memory = 0;        // mode memory, or not (set via soldered star)
  223. uint8_t modegroup = 0;     // which mode group (set above in #defines)
  224. uint8_t mode_idx = 0;      // current or last-used mode number
  225. // counter for entering config mode
  226. // (needs to be remembered while off, but only for up to half a second)
  227. uint8_t fast_presses __attribute__ ((section (".noinit")));
  228.  
  229. // NOTE: Only '1' is known to work; -1 will probably break and is untested.
  230. // In other words, short press goes to the next (higher) mode,
  231. // medium press goes to the previous (lower) mode.
  232. #define mode_dir 1
  233. // total length of current mode group's array
  234. uint8_t mode_cnt;
  235. // number of regular non-hidden modes in current mode group
  236. uint8_t solid_modes;
  237. // number of hidden modes in the current mode group
  238. // (hardcoded because both groups have the same hidden modes)
  239. //uint8_t hidden_modes = NUM_HIDDEN;  // this is never used
  240.  
  241.  
  242. // Modes (gets set when the light starts up based on saved config values)
  243. PROGMEM const uint8_t modesNx1[] = { MODESNx1, HIDDENMODES };
  244. PROGMEM const uint8_t modesNx2[] = { MODESNx2, HIDDENMODES };
  245. const uint8_t *modesNx;  // gets pointed at whatever group is current
  246.  
  247. PROGMEM const uint8_t modes1x1[] = { MODES1x1, HIDDENMODES_ALT };
  248. PROGMEM const uint8_t modes1x2[] = { MODES1x2, HIDDENMODES_ALT };
  249. const uint8_t *modes1x;
  250.  
  251. PROGMEM const uint8_t modes_pwm1[] = { MODES_PWM1, HIDDENMODES_PWM };
  252. PROGMEM const uint8_t modes_pwm2[] = { MODES_PWM2, HIDDENMODES_PWM };
  253. const uint8_t *modes_pwm;
  254.  
  255. PROGMEM const uint8_t voltage_blinks[] = {
  256.     ADC_0,    // 1 blink  for 0%-25%
  257.     ADC_25,   // 2 blinks for 25%-50%
  258.     ADC_50,   // 3 blinks for 50%-75%
  259.     ADC_75,   // 4 blinks for 75%-100%
  260.     ADC_100,  // 5 blinks for >100%
  261.     255,      // Ceiling, don't remove
  262. };
  263.  
  264. void save_state() {  // central method for writing (with wear leveling)
  265.     // a single 16-bit write uses less ROM space than two 8-bit writes
  266.     uint8_t eep;
  267.     uint8_t oldpos=eepos;
  268.  
  269.     eepos = (eepos+1) & 63;  // wear leveling, use next cell
  270.  
  271. #ifdef CONFIG_STARS
  272.     eep = mode_idx | (modegroup << 5);
  273. #else
  274.     eep = mode_idx | (modegroup << 5) | (memory << 6);
  275. #endif
  276.     eeprom_write_byte((uint8_t *)(eepos), eep);      // save current state
  277.     eeprom_write_byte((uint8_t *)(oldpos), 0xff);    // erase old state
  278. }
  279.  
  280. void restore_state() {
  281.     uint8_t eep;
  282.     // find the config data
  283.     for(eepos=0; eepos<64; eepos++) {
  284.         eep = eeprom_read_byte((const uint8_t *)eepos);
  285.         if (eep != 0xff) break;
  286.     }
  287.     // unpack the config data
  288.     if (eepos < 64) {
  289.         mode_idx = eep & 0x0f;
  290.         modegroup = (eep >> 5) & 1;
  291. #ifndef CONFIG_STARS
  292.         memory = (eep >> 6) & 1;
  293. #endif
  294.     }
  295.     // unnecessary, save_state handles wrap-around
  296.     // (and we don't really care about it skipping cell 0 once in a while)
  297.     //else eepos=0;
  298. }
  299.  
  300. inline void next_mode() {
  301.     mode_idx += 1;
  302.     if (mode_idx >= solid_modes) {
  303.         // Wrap around, skipping the hidden modes
  304.         // (note: this also applies when going "forward" from any hidden mode)
  305.         mode_idx = 0;
  306.     }
  307. }
  308.  
  309. #ifdef OFFTIM3
  310. inline void prev_mode() {
  311.     if (mode_idx == solid_modes) {
  312.         // If we hit the end of the hidden modes, go back to moon
  313.         mode_idx = 0;
  314.     } else if (mode_idx > 0) {
  315.         // Regular mode: is between 1 and TOTAL_MODES
  316.         mode_idx -= 1;
  317.     } else {
  318.         // Otherwise, wrap around (this allows entering hidden modes)
  319.         mode_idx = mode_cnt - 1;
  320.     }
  321. }
  322. #endif
  323.  
  324. #ifdef CONFIG_STARS
  325. inline void check_stars() {
  326.     // Configure options based on stars
  327.     // 0 being low for soldered, 1 for pulled-up for not soldered
  328. #if 0  // not implemented, STAR2_PIN is used for second PWM channel
  329.     // Moon
  330.     // enable moon mode?
  331.     if ((PINB & (1 << STAR2_PIN)) == 0) {
  332.         modes[mode_cnt++] = MODE_MOON;
  333.     }
  334. #endif
  335. #if 0  // Mode order not as important as mem/no-mem
  336.     // Mode order
  337.     if ((PINB & (1 << STAR3_PIN)) == 0) {
  338.         // High to Low
  339.         mode_dir = -1;
  340.     } else {
  341.         mode_dir = 1;
  342.     }
  343. #endif
  344.     // Memory
  345.     if ((PINB & (1 << STAR3_PIN)) == 0) {
  346.         memory = 1;  // solder to enable memory
  347.     } else {
  348.         memory = 0;  // unsolder to disable memory
  349.     }
  350. }
  351. #endif  // ifdef CONFIG_STARS
  352.  
  353. void count_modes() {
  354.     /*
  355.      * Determine how many solid and hidden modes we have.
  356.      * The modes_pwm array should have several values for regular modes
  357.      * then some values for hidden modes.
  358.      *
  359.      * (this matters because we have more than one set of modes to choose
  360.      *  from, so we need to count at runtime)
  361.      */
  362.     if (modegroup == 0) {
  363.         solid_modes = NUM_MODES1;
  364.         modesNx = modesNx1;
  365.         modes1x = modes1x1;
  366.         modes_pwm = modes_pwm1;
  367.     } else {
  368.         solid_modes = NUM_MODES2;
  369.         modesNx = modesNx2;
  370.         modes1x = modes1x2;
  371.         modes_pwm = modes_pwm2;
  372.     }
  373.     mode_cnt = solid_modes + NUM_HIDDEN;
  374. }
  375.  
  376. #ifdef VOLTAGE_MON
  377. inline void ADC_on() {
  378.     DIDR0 |= (1 << ADC_DIDR);                           // disable digital input on ADC pin to reduce power consumption
  379.     ADMUX  = (1 << REFS0) | (1 << ADLAR) | ADC_CHANNEL; // 1.1v reference, left-adjust, ADC1/PB2
  380.     ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;   // enable, start, prescale
  381. }
  382. #else
  383. inline void ADC_off() {
  384.     ADCSRA &= ~(1<<7); //ADC off
  385. }
  386. #endif
  387.  
  388. void set_output(uint8_t pwm1, uint8_t pwm2) {
  389.     // Need PHASE to properly turn off the light
  390.     if ((pwm1==0) && (pwm2==0)) {
  391.         TCCR0A = PHASE;
  392.     }
  393.     PWM_LVL = pwm1;
  394.     ALT_PWM_LVL = pwm2;
  395. }
  396.  
  397. void set_mode(uint8_t mode) {
  398.     TCCR0A = pgm_read_byte(modes_pwm + mode);
  399.     set_output(pgm_read_byte(modesNx + mode), pgm_read_byte(modes1x + mode));
  400.     /*
  401.     // Only set output for solid modes
  402.     uint8_t out = pgm_read_byte(modesNx + mode);
  403.     if ((out < 250) || (out == 255)) {
  404.         set_output(pgm_read_byte(modesNx + mode), pgm_read_byte(modes1x + mode));
  405.     }
  406.     */
  407. }
  408.  
  409. #ifdef VOLTAGE_MON
  410. uint8_t get_voltage() {
  411.     // Start conversion
  412.     ADCSRA |= (1 << ADSC);
  413.     // Wait for completion
  414.     while (ADCSRA & (1 << ADSC));
  415.     // See if voltage is lower than what we were looking for
  416.     return ADCH;
  417. }
  418. #endif
  419.  
  420. void blink(uint8_t val)
  421. {
  422.     for (; val>0; val--)
  423.     {
  424.         set_output(BLINK_BRIGHTNESS);
  425.         _delay_ms(100);
  426.         set_output(0,0);
  427.         _delay_ms(400);
  428.     }
  429. }
  430.  
  431. #ifndef CONFIG_STARS
  432. void toggle(uint8_t *var) {
  433.     // Used for extended config mode
  434.     // Changes the value of a config option, waits for the user to "save"
  435.     // by turning the light off, then changes the value back in case they
  436.     // didn't save.  Can be used repeatedly on different options, allowing
  437.     // the user to change and save only one at a time.
  438.     *var ^= 1;
  439.     save_state();
  440.     blink(2);
  441.     *var ^= 1;
  442.     save_state();
  443.     _delay_s();
  444. }
  445. #endif // ifndef CONFIG_STARS
  446.  
  447. int main(void)
  448. {
  449.     uint8_t cap_val;
  450.  
  451.     // Read the off-time cap *first* to get the most accurate reading
  452.     // Start up ADC for capacitor pin
  453.     DIDR0 |= (1 << CAP_DIDR);                           // disable digital input on ADC pin to reduce power consumption
  454.     ADMUX  = (1 << REFS0) | (1 << ADLAR) | CAP_CHANNEL; // 1.1v reference, left-adjust, ADC3/PB3
  455.     ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;   // enable, start, prescale
  456.  
  457.     // Wait for completion
  458.     while (ADCSRA & (1 << ADSC));
  459.     // Start again as datasheet says first result is unreliable
  460.     ADCSRA |= (1 << ADSC);
  461.     // Wait for completion
  462.     while (ADCSRA & (1 << ADSC));
  463.     cap_val = ADCH; // save this for later
  464.  
  465. #ifdef CONFIG_STARS
  466.     // All ports default to input, but turn pull-up resistors on for the stars (not the ADC input!  Made that mistake already)
  467.     // only one star, because one is used for PWM channel 2
  468.     // and the other is used for the off-time capacitor
  469.     PORTB = (1 << STAR3_PIN);
  470. #endif
  471.  
  472.     // Set PWM pin to output
  473.     DDRB |= (1 << PWM_PIN);     // enable main channel
  474.     DDRB |= (1 << ALT_PWM_PIN); // enable second channel
  475.  
  476.     // Set timer to do PWM for correct output pin and set prescaler timing
  477.     //TCCR0A = 0x23; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
  478.     //TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
  479.     TCCR0A = PHASE;
  480.     // Set timer to do PWM for correct output pin and set prescaler timing
  481.     TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
  482.  
  483.     // Read config values and saved state
  484. #ifdef CONFIG_STARS
  485.     check_stars();
  486. #endif
  487.     restore_state();
  488.     // Enable the current mode group
  489.     count_modes();
  490.  
  491.  
  492.     // memory decayed, reset it
  493.     // (should happen on med/long press instead
  494.     //  because mem decay is *much* slower when the OTC is charged
  495.     //  so let's not wait until it decays to reset it)
  496.     //if (fast_presses > 0x20) { fast_presses = 0; }
  497.  
  498.     if (cap_val > CAP_SHORT) {
  499.         // We don't care what the value is as long as it's over 15
  500.         fast_presses = (fast_presses+1) & 0x1f;
  501.         // Indicates they did a short press, go to the next mode
  502.         next_mode(); // Will handle wrap arounds
  503. #ifdef OFFTIM3
  504.     } else if (cap_val > CAP_MED) {
  505.         fast_presses = 0;
  506.         // User did a medium press, go back one mode
  507.         prev_mode(); // Will handle "negative" modes and wrap-arounds
  508. #endif
  509.     } else {
  510.         // Long press, keep the same mode
  511.         // ... or reset to the first mode
  512.         fast_presses = 0;
  513.         if (! memory) {
  514.             // Reset to the first mode
  515.             mode_idx = 0;
  516.         }
  517.     }
  518.     save_state();
  519.  
  520.     // Turn off ADC
  521.     //ADC_off();
  522.  
  523.     // Charge up the capacitor by setting CAP_PIN to output
  524.     DDRB  |= (1 << CAP_PIN);    // Output
  525.     PORTB |= (1 << CAP_PIN);    // High
  526.  
  527.     // Turn features on or off as needed
  528.     #ifdef VOLTAGE_MON
  529.     ADC_on();
  530.     #else
  531.     ADC_off();
  532.     #endif
  533.     //ACSR   |=  (1<<7); //AC off
  534.  
  535.     // Enable sleep mode set to Idle that will be triggered by the sleep_mode() command.
  536.     // Will allow us to go idle between WDT interrupts
  537.     //set_sleep_mode(SLEEP_MODE_IDLE);  // not used due to blinky modes
  538.  
  539.     uint8_t output;
  540. #ifdef NON_WDT_TURBO
  541.     uint8_t ticks = 0;
  542. #endif
  543. #ifdef VOLTAGE_MON
  544.     uint8_t lowbatt_cnt = 0;
  545.     uint8_t i = 0;
  546.     uint8_t voltage;
  547.     // Make sure voltage reading is running for later
  548.     ADCSRA |= (1 << ADSC);
  549. #endif
  550.     while(1) {
  551.         output = pgm_read_byte(modesNx + mode_idx);
  552.         if (fast_presses > 0x0f) {  // Config mode
  553.             _delay_s();       // wait for user to stop fast-pressing button
  554.             fast_presses = 0; // exit this mode after one use
  555.             mode_idx = 0;
  556.  
  557. #ifdef CONFIG_STARS
  558.             // Short/small version of the config mode
  559.             // Toggle the mode group, blink, then exit
  560.             modegroup ^= 1;
  561.             save_state();
  562.             count_modes();  // reconfigure without a power cycle
  563.             blink(1);
  564. #else
  565.             // Longer/larger version of the config mode
  566.             // Toggle the mode group, blink, un-toggle, continue
  567.             toggle(&modegroup);
  568.  
  569.             // Toggle memory, blink, untoggle, exit
  570.             toggle(&memory);
  571. #endif  // ifdef CONFIG_STARS
  572.         }
  573. #ifdef STROBE
  574.         else if (output == STROBE) {
  575.             // 10Hz tactical strobe
  576.             set_output(255,0);
  577.             _delay_ms(50);
  578.             set_output(0,0);
  579.             _delay_ms(50);
  580.         }
  581. #endif // ifdef STROBE
  582. #ifdef BIKING_STROBE
  583.         else if (output == BIKING_STROBE) {
  584.             // 2-level stutter beacon for biking and such
  585. #ifdef FULL_BIKING_STROBE
  586.             // normal version
  587.             for(i=0;i<4;i++) {
  588.                 set_output(255,0);
  589.                 _delay_ms(5);
  590.                 set_output(0,255);
  591.                 _delay_ms(65);
  592.             }
  593.             _delay_ms(720);
  594. #else
  595.             // small/minimal version
  596.             set_output(255,0);
  597.             _delay_ms(10);
  598.             set_output(0,255);
  599.             _delay_s();
  600. #endif
  601.         }
  602. #endif  // ifdef BIKING_STROBE
  603. #ifdef BATTCHECK
  604.         else if (output == BATTCHECK) {
  605.             voltage = get_voltage();
  606.             // figure out how many times to blink
  607.             for (i=0;
  608.                     voltage > pgm_read_byte(voltage_blinks + i);
  609.                     i ++) {}
  610.  
  611.             // blink zero to five times to show voltage
  612.             // (~0%, ~25%, ~50%, ~75%, ~100%, >100%)
  613.             blink(i);
  614.             // wait between readouts
  615.             _delay_s(); _delay_s();
  616.         }
  617. #endif // ifdef BATTCHECK
  618.         else {  // Regular non-hidden solid mode
  619.             set_mode(mode_idx);
  620.             // This part of the code will mostly replace the WDT tick code.
  621. #ifdef NON_WDT_TURBO
  622.             // Do some magic here to handle turbo step-down
  623.             //if (ticks < 255) ticks++;  // don't roll over
  624.             ticks ++;  // actually, we don't care about roll-over prevention
  625.             if ((ticks > TURBO_TIMEOUT)
  626.                     && (output == TURBO)) {
  627.                 mode_idx = solid_modes - 2; // step down to second-highest mode
  628.                 set_mode(mode_idx);
  629.                 save_state();
  630.             }
  631. #endif
  632.             // Otherwise, just sleep.
  633.             _delay_ms(500);
  634.  
  635.             // If we got this far, the user has stopped fast-pressing.
  636.             // So, don't enter config mode.
  637.             fast_presses = 0;
  638.         }
  639. #ifdef VOLTAGE_MON
  640. #if 1
  641.         if (ADCSRA & (1 << ADIF)) {  // if a voltage reading is ready
  642.             voltage = ADCH; // get_voltage();
  643.             // See if voltage is lower than what we were looking for
  644.             //if (voltage < ((mode_idx <= 1) ? ADC_CRIT : ADC_LOW)) {
  645.             if (voltage < ADC_LOW) {
  646.                 lowbatt_cnt ++;
  647.             } else {
  648.                 lowbatt_cnt = 0;
  649.             }
  650.             // See if it's been low for a while, and maybe step down
  651.             if (lowbatt_cnt >= 8) {
  652.                 // DEBUG: blink on step-down:
  653.                 //set_output(0,0);  _delay_ms(100);
  654.                 i = mode_idx; // save space by not accessing mode_idx more than necessary
  655.                 // properly track hidden vs normal modes
  656.                 if (i >= solid_modes) {
  657.                     // step down from blinky modes to medium
  658.                     i = 2;
  659.                 } else if (i > 0) {
  660.                     // step down from solid modes one at a time
  661.                     i -= 1;
  662.                 } else { // Already at the lowest mode
  663.                     i = 0;
  664.                     // Turn off the light
  665.                     set_output(0,0);
  666.                     // Power down as many components as possible
  667.                     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  668.                     sleep_mode();
  669.                 }
  670.                 set_mode(i);
  671.                 mode_idx = i;
  672.                 save_state();
  673.                 lowbatt_cnt = 0;
  674.                 // Wait at least 2 seconds before lowering the level again
  675.                 _delay_ms(250);  // this will interrupt blinky modes
  676.             }
  677.  
  678.             // Make sure conversion is running for next time through
  679.             ADCSRA |= (1 << ADSC);
  680.         }
  681. #endif
  682. #endif  // ifdef VOLTAGE_MON
  683.         //sleep_mode();  // incompatible with blinky modes
  684.  
  685.         // If we got this far, the user has stopped fast-pressing.
  686.         // So, don't enter config mode.
  687.         //fast_presses = 0;  // doesn't interact well with strobe, too fast
  688.     }
  689.  
  690.     //return 0; // Standard Return Code
  691. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement