~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to ToyKeeper/s7/s7.c

  • Committer: Selene Scriven
  • Date: 2015-11-24 09:48:09 UTC
  • mfrom: (150.1.16 sandbox)
  • Revision ID: ubuntu@toykeeper.net-20151124094809-gkxohuxfrre3aucx
Merged updates to s7 and brass-edc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
 *
59
59
 */
60
60
#define NANJG_LAYOUT
61
 
#include "../tk-attiny.h"
 
61
#include "tk-attiny.h"
62
62
#undef BOGOMIPS  // this particular driver runs slower
63
63
#define BOGOMIPS 890
64
64
 
79
79
#define MODE_HIGH           120     // 120: 155 lm
80
80
#define MODE_HIGHER         255     // 255: 342 lm
81
81
// If you change these, you'll probably want to change the "modes" array below
82
 
#define SOLID_MODES         5       // How many non-blinky modes will we have?
83
 
#define DUAL_BEACON_MODES   5+3     // How many beacon modes will we have (with background light on)?
84
 
#define SINGLE_BEACON_MODES 5+3+1   // How many beacon modes will we have (without background light on)?
85
 
#define FIXED_STROBE_MODES  5+3+1+3 // How many constant-speed strobe modes?
86
 
#define VARIABLE_STROBE_MODES 5+3+1+3+2 // How many variable-speed strobe modes?
87
 
#define BATT_CHECK_MODE     5+3+1+3+2+1 // battery check mode index
88
 
// Note: don't use more than 32 modes, or it will interfere with the mechanism used for mode memory
89
 
#define TOTAL_MODES         BATT_CHECK_MODE
 
82
// How many non-blinky modes will we have?
 
83
#define SOLID_MODES           5
 
84
// battery check mode index
 
85
#define BATT_CHECK_MODE       1+SOLID_MODES
 
86
// How many beacon modes will we have (without background light on)?
 
87
#define SINGLE_BEACON_MODES   1+BATT_CHECK_MODE
 
88
// How many constant-speed strobe modes?
 
89
#define FIXED_STROBE_MODES    3+SINGLE_BEACON_MODES
 
90
// How many variable-speed strobe modes?
 
91
#define VARIABLE_STROBE_MODES 2+FIXED_STROBE_MODES
 
92
// How many beacon modes will we have (with background light on)?
 
93
#define DUAL_BEACON_MODES     3+VARIABLE_STROBE_MODES
90
94
 
91
95
#define USE_BATTCHECK
92
96
#define BATTCHECK_VpT  // Use the volts+tenths battcheck style
93
97
//#define BATTCHECK_4bars  // Use the volts+tenths battcheck style
94
98
 
95
 
#include "../tk-calibration.h"
 
99
#include "tk-calibration.h"
96
100
 
97
101
/*
98
102
 * =========================================================================
99
103
 */
100
104
 
101
 
#include "../tk-delay.h"
 
105
#include "tk-delay.h"
102
106
 
103
107
#include <avr/pgmspace.h>
104
108
#include <avr/interrupt.h>
105
 
#include <avr/eeprom.h>
106
109
#include <avr/sleep.h>
107
110
 
108
 
#include "../tk-voltage.h"
 
111
#include "tk-voltage.h"
109
112
 
110
113
/*
111
114
 * global variables
123
126
#define memory 0
124
127
 
125
128
// Modes (hardcoded to save space)
126
 
static uint8_t modes[TOTAL_MODES] = { // high enough to handle all
 
129
const uint8_t modes[] = { // high enough to handle all
127
130
    MODE_MOON, MODE_LOW, MODE_MED, MODE_HIGH, MODE_HIGHER, // regular solid modes
128
 
    MODE_MOON, MODE_LOW, MODE_MED, // dual beacon modes (this level and this level + 2)
 
131
    MODE_MED, // battery check mode
129
132
    MODE_HIGHER, // heartbeat beacon
130
 
    79, 41, 15, // constant-speed strobe modes (12.5 Hz, 24 Hz, 60 Hz)
 
133
    82, 41, 15, // constant-speed strobe modes (12 Hz, 24 Hz, 60 Hz)
131
134
    MODE_HIGHER, MODE_HIGHER, // variable-speed strobe modes
132
 
    MODE_MED, // battery check mode
 
135
    MODE_MOON, MODE_LOW, MODE_MED, // dual beacon modes (this level and this level + 2)
133
136
};
134
137
volatile uint8_t mode_idx = 0;
135
138
// 1 or -1. Do we increase or decrease the idx when moving up to a higher mode?
138
141
 
139
142
inline void next_mode() {
140
143
    mode_idx += mode_dir;
141
 
    if (mode_idx > (TOTAL_MODES - 1)) {
 
144
    if (mode_idx > (sizeof(modes) - 1)) {
142
145
        // Wrap around
143
146
        mode_idx = 0;
144
147
    }
229
232
    while(1) {
230
233
        if(mode_idx < SOLID_MODES) { // Just stay on at a given brightness
231
234
            sleep_mode();
232
 
        } else if (mode_idx < DUAL_BEACON_MODES) { // two-level fast strobe pulse at about 1 Hz
233
 
            for(i=0; i<4; i++) {
234
 
                PWM_LVL = modes[mode_idx-SOLID_MODES+2];
235
 
                _delay_ms(5);
236
 
                PWM_LVL = modes[mode_idx];
237
 
                _delay_ms(65);
238
 
            }
239
 
            _delay_ms(720);
 
235
        } else if (mode_idx < BATT_CHECK_MODE) {
 
236
            PWM_LVL = 0;
 
237
            get_voltage();  _delay_ms(200);  // the first reading is junk
 
238
#ifdef BATTCHECK_VpT
 
239
            uint8_t result = battcheck();
 
240
            blink(result >> 5, BLINK_SPEED/8);
 
241
            _delay_ms(BLINK_SPEED);
 
242
            blink(1,5);
 
243
            _delay_ms(BLINK_SPEED*3/2);
 
244
            blink(result & 0b00011111, BLINK_SPEED/8);
 
245
#else
 
246
            blink(battcheck());
 
247
#endif  // BATTCHECK_VpT
 
248
            _delay_ms(2000);  // wait at least 2 seconds between readouts
240
249
        } else if (mode_idx < SINGLE_BEACON_MODES) { // heartbeat flasher
241
250
            PWM_LVL = modes[SOLID_MODES-1];
242
251
            _delay_ms(1);
272
281
                else { strobe_len = 100-j; }
273
282
                _delay_ms(strobe_len+9);
274
283
            }
275
 
        } else if (mode_idx < BATT_CHECK_MODE) {
276
 
            get_voltage();  _delay_ms(50);  // the first reading is junk
277
 
#ifdef BATTCHECK_VpT
278
 
            uint8_t result = battcheck();
279
 
            blink(result >> 5, BLINK_SPEED/8);
280
 
            _delay_ms(BLINK_SPEED);
281
 
            blink(1,5);
282
 
            _delay_ms(BLINK_SPEED*3/2);
283
 
            blink(result & 0b00011111, BLINK_SPEED/8);
284
 
#else
285
 
            blink(battcheck());
286
 
#endif  // BATTCHECK_VpT
287
 
            _delay_ms(2000);  // wait at least 2 seconds between readouts
 
284
        } else if (mode_idx < DUAL_BEACON_MODES) { // two-level fast strobe pulse at about 1 Hz
 
285
            for(i=0; i<4; i++) {
 
286
                PWM_LVL = modes[mode_idx-SOLID_MODES+2];
 
287
                _delay_ms(5);
 
288
                PWM_LVL = modes[mode_idx];
 
289
                _delay_ms(65);
 
290
            }
 
291
            _delay_ms(720);
288
292
        }
289
293
#ifdef VOLTAGE_MON
290
294
        if (ADCSRA & (1 << ADIF)) {  // if a voltage reading is ready