Advertisement
pyro1son

BLF_A6 with RED Moon

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