Advertisement
pyro1son

blf-af pin3 turbo

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