[ create a new paste ] login | about

Link: http://codepad.org/WRjYRo8h    [ raw code | output | fork ]

C, pasted on May 24:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
 * BLF EE A6 firmware (special-edition group buy light)
 * This light uses a FET+1 style driver, with a FET on the main PWM channel
 * for the brightest high modes and a single 7135 chip on the secondary PWM
 * channel so we can get stable, efficient low / medium modes.  It also
 * includes a capacitor for measuring off time.
 *
 * Copyright (C) 2015 Selene Scriven
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * NANJG 105C Diagram
 *           ---
 *         -|   |- VCC
 *     OTC -|   |- Voltage ADC
 *  Star 3 -|   |- PWM (FET)
 *     GND -|   |- PWM (1x7135)
 *           ---
 *
 * FUSES
 *      I use these fuse settings
 *      Low:  0x75  (4.8MHz CPU without 8x divider, 9.4kHz phase-correct PWM or 18.75kHz fast-PWM)
 *      High: 0xff
 *
 *      For more details on these settings, visit http://github.com/JCapSolutions/blf-firmware/wiki/PWM-Frequency
 *
 * STARS
 *      Star 2 = second PWM output channel
 *      Star 3 = mode memory if soldered, no memory by default
 *      Star 4 = Capacitor for off-time
 *
 * VOLTAGE
 *      Resistor values for voltage divider (reference BLF-VLD README for more info)
 *      Reference voltage can be anywhere from 1.0 to 1.2, so this cannot be all that accurate
 *
 *           VCC
 *            |
 *           Vd (~.25 v drop from protection diode)
 *            |
 *          1912 (R1 19,100 ohms)
 *            |
 *            |---- PB2 from MCU
 *            |
 *          4701 (R2 4,700 ohms)
 *            |
 *           GND
 *
 *   To find out what values to use, flash the driver with battcheck.hex
 *   and hook the light up to each voltage you need a value for.  This is
 *   much more reliable than attempting to calculate the values from a
 *   theoretical formula.
 *
 *   Same for off-time capacitor values.  Measure, don't guess.
 */
#define F_CPU 4800000UL

/*
 * =========================================================================
 * Settings to modify per driver
 */

//#define FAST 0x23           // fast PWM channel 1 only
//#define PHASE 0x21          // phase-correct PWM channel 1 only
#define FAST 0xA3           // fast PWM both channels
#define PHASE 0xA1          // phase-correct PWM both channels

#define VOLTAGE_MON         // Comment out to disable LVP
#define OWN_DELAY           // Should we use the built-in delay or our own?
// Adjust the timing per-driver, since the hardware has high variance
// Higher values will run slower, lower values run faster.
#define DELAY_TWEAK         950

//#define OFFTIM3             // Use short/med/long off-time presses
                            // instead of just short/long

// comment out to use extended config mode instead of a solderable star
// (controls whether mode memory is on the star or if it's a setting in config mode)
#define CONFIG_STARS

// Mode group 1
#define NUM_MODES1          4
// PWM levels for the big circuit (FET or Nx7135)
#define MODESNx1            6,56,135,255
// PWM levels for the small circuit (1x7135)
#define MODES1x1            0,0,0,0
// PWM speed for each mode
#define MODES_PWM1          PHASE,FAST,FAST,PHASE
// Mode group 2
#define NUM_MODES2          3
#define MODESNx2            0,0,0
#define MODES1x2            10,70,255,
#define MODES_PWM2          FAST,FAST,PHASE
// Hidden modes are *before* the lowest (moon) mode, and should be specified
// in reverse order.  So, to go backward from moon to turbo to strobe to
// battcheck, use BATTCHECK,STROBE,TURBO .
//#define NUM_HIDDEN          3
//#define HIDDENMODES         BATTCHECK,STROBE,TURBO
//#define HIDDENMODES_PWM     PHASE,PHASE,PHASE

// Uncomment to use a 2-level stutter beacon instead of a tactical strobe
//#define BIKING_STROBE

#define NON_WDT_TURBO            // enable turbo step-down without WDT
// How many timer ticks before before dropping down.
// Each timer tick is 500ms, so "60" would be a 30-second stepdown.
// Max value of 255 unless you change "ticks"
#define TURBO_TIMEOUT       60

// These values were measured using wight's "A17HYBRID-S" driver built by DBCstm.
// Your mileage may vary.
#define ADC_42          174 // the ADC value we expect for 4.20 volts
#define ADC_100         174 // the ADC value for 100% full (4.2V resting)
#define ADC_75          165 // the ADC value for 75% full (4.0V resting)
#define ADC_50          155 // the ADC value for 50% full (3.8V resting)
#define ADC_25          141 // the ADC value for 25% full (3.5V resting)
#define ADC_0           118 // the ADC value for 0% full (3.0V resting)
#define ADC_LOW         109 // When do we start ramping down (2.8V)
#define ADC_CRIT        104 // When do we shut the light off (2.7V)
// These values were copied from s7.c.
// Your mileage may vary.
//#define ADC_42          185 // the ADC value we expect for 4.20 volts
//#define ADC_100         185 // the ADC value for 100% full (4.2V resting)
//#define ADC_75          175 // the ADC value for 75% full (4.0V resting)
//#define ADC_50          164 // the ADC value for 50% full (3.8V resting)
//#define ADC_25          154 // the ADC value for 25% full (3.5V resting)
//#define ADC_0           139 // the ADC value for 0% full (3.0V resting)
//#define ADC_LOW         123 // When do we start ramping down (2.8V)
//#define ADC_CRIT        113 // When do we shut the light off (2.7V)
// Values for testing only:
//#define ADC_LOW         125 // When do we start ramping down (2.8V)
//#define ADC_CRIT        124 // When do we shut the light off (2.7V)

// the BLF EE A6 driver may have different offtime cap values than most other drivers
#ifdef OFFTIM3
#define CAP_SHORT           250  // Value between 1 and 255 corresponding with cap voltage (0 - 1.1v) where we consider it a short press to move to the next mode
#define CAP_MED             190  // Value between 1 and 255 corresponding with cap voltage (0 - 1.1v) where we consider it a short press to move to the next mode
#else
#define CAP_SHORT           190  // Value between 1 and 255 corresponding with cap voltage (0 - 1.1v) where we consider it a short press to move to the next mode
                                 // Not sure the lowest you can go before getting bad readings, but with a value of 70 and a 1uF cap, it seemed to switch sometimes
                                 // even when waiting 10 seconds between presses.
#endif

#define TURBO     255       // Convenience code for turbo mode
//#define STROBE    254       // Convenience code for strobe mode
//#define BATTCHECK 253       // Convenience code for battery check mode

/*
 * =========================================================================
 */

#ifdef OWN_DELAY
#include <util/delay_basic.h>
// Having own _delay_ms() saves some bytes AND adds possibility to use variables as input
void _delay_ms(uint16_t n)
{
    // TODO: make this take tenths of a ms instead of ms,
    // for more precise timing?
    while(n-- > 0) _delay_loop_2(DELAY_TWEAK);
}
void _delay_s()  // because it saves a bit of ROM space to do it this way
{
    _delay_ms(1000);
}
#else
#include <util/delay.h>
#endif

#include <avr/pgmspace.h>
//#include <avr/io.h>
//#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <avr/sleep.h>
//#include <avr/power.h>

#define STAR2_PIN   PB0     // But note that there is no star 2.
#define STAR3_PIN   PB4
#define CAP_PIN     PB3
#define CAP_CHANNEL 0x03    // MUX 03 corresponds with PB3 (Star 4)
#define CAP_DIDR    ADC3D   // Digital input disable bit corresponding with PB3
#define PWM_PIN     PB1
#define ALT_PWM_PIN PB0
#define VOLTAGE_PIN PB2
#define ADC_CHANNEL 0x01    // MUX 01 corresponds with PB2
#define ADC_DIDR    ADC1D   // Digital input disable bit corresponding with PB2
#define ADC_PRSCL   0x06    // clk/64

#define PWM_LVL     OCR0B   // OCR0B is the output compare register for PB1
#define ALT_PWM_LVL OCR0A   // OCR0A is the output compare register for PB0

/*
 * global variables
 */

// Config / state variables
uint8_t eepos = 0;
uint8_t memory = 0;        // mode memory, or not (set via soldered star)
uint8_t modegroup = 0;     // which mode group (set above in #defines)
uint8_t mode_idx = 0;      // current or last-used mode number
uint8_t fast_presses = 0;  // counter for entering config mode

// NOTE: Only '1' is known to work; -1 will probably break and is untested.
// In other words, short press goes to the next (higher) mode,
// medium press goes to the previous (lower) mode.
#define mode_dir 1
// total length of current mode group's array
uint8_t mode_cnt;
// number of regular non-hidden modes in current mode group
uint8_t solid_modes;
// number of hidden modes in the current mode group
// (hardcoded because both groups have the same hidden modes)
//uint8_t hidden_modes = NUM_HIDDEN;  // this is never used


// Modes (gets set when the light starts up based on saved config values)
PROGMEM const uint8_t modesNx1[] = { MODESNx1 };
PROGMEM const uint8_t modesNx2[] = { MODESNx2 };
const uint8_t *modesNx;  // gets pointed at whatever group is current

PROGMEM const uint8_t modes1x1[] = { MODES1x1 };
PROGMEM const uint8_t modes1x2[] = { MODES1x2 };
const uint8_t *modes1x;

PROGMEM const uint8_t modes_pwm1[] = { MODES_PWM1 };
PROGMEM const uint8_t modes_pwm2[] = { MODES_PWM2 };
const uint8_t *modes_pwm;

PROGMEM const uint8_t voltage_blinks[] = {
    ADC_0,    // 1 blink  for 0%-25%
    ADC_25,   // 2 blinks for 25%-50%
    ADC_50,   // 3 blinks for 50%-75%
    ADC_75,   // 4 blinks for 75%-100%
    ADC_100,  // 5 blinks for >100%
};

void save_state() {  // central method for writing (with wear leveling)
    uint8_t oldpos=eepos;
    // a single 16-bit write uses less ROM space than two 8-bit writes
    uint16_t eep;

    eepos=(eepos+2)&63;  // wear leveling, use next cell

#ifdef CONFIG_STARS
    eep = mode_idx | (fast_presses << 12) | (modegroup << 8);
#else
    eep = mode_idx | (fast_presses << 12) | (modegroup << 8) | (memory << 9);
#endif
    eeprom_write_word((uint16_t *)(eepos), eep);      // save current state
    eeprom_write_word((uint16_t *)(oldpos), 0xffff);  // erase old state
}

void restore_state() {
    // two 8-bit reads use less ROM space than a single 16-bit write
    uint8_t eep1;
    uint8_t eep2;
    // find the config data
    for(eepos=0; eepos<64; eepos+=2) {
        eep1 = eeprom_read_byte((const uint8_t *)eepos);
        eep2 = eeprom_read_byte((const uint8_t *)eepos+1);
        if (eep1 != 0xff) break;
    }
    // unpack the config data
    if (eepos < 64) {
        mode_idx = eep1;
        fast_presses = (eep2 >> 4);
        modegroup = eep2 & 1;
#ifndef CONFIG_STARS
        memory = (eep2 >> 1) & 1;
#endif
    }
    //else eepos=0;  // unnecessary, save_state handles wrap-around
}

inline void next_mode() {
    mode_idx += 1;
    if (mode_idx >= solid_modes) {
        // Wrap around, skipping the hidden modes
        // (note: this also applies when going "forward" from any hidden mode)
        mode_idx = 0;
    }
}

#ifdef OFFTIM3
inline void prev_mode() {
    if (mode_idx == solid_modes) {
        // If we hit the end of the hidden modes, go back to moon
        mode_idx = 0;
    } else if (mode_idx > 0) {
        // Regular mode: is between 1 and TOTAL_MODES
        mode_idx -= 1;
    } else {
        // Otherwise, wrap around (this allows entering hidden modes)
        mode_idx = mode_cnt - 1;
    }
}
#endif

#ifdef CONFIG_STARS
inline void check_stars() {
    // Configure options based on stars
    // 0 being low for soldered, 1 for pulled-up for not soldered
#if 0  // not implemented, STAR2_PIN is used for second PWM channel
    // Moon
    // enable moon mode?
    if ((PINB & (1 << STAR2_PIN)) == 0) {
        modes[mode_cnt++] = MODE_MOON;
    }
#endif
#if 0  // Mode order not as important as mem/no-mem
    // Mode order
    if ((PINB & (1 << STAR3_PIN)) == 0) {
        // High to Low
        mode_dir = -1;
    } else {
        mode_dir = 1;
    }
#endif
    // Memory
    if ((PINB & (1 << STAR3_PIN)) == 0) {
        memory = 1;  // solder to enable memory
    } else {
        memory = 0;  // unsolder to disable memory
    }
}
#endif  // ifdef CONFIG_STARS

void count_modes() {
    /*
     * Determine how many solid and hidden modes we have.
     * The modes_pwm array should have several values for regular modes
     * then some values for hidden modes.
     *
     * (this matters because we have more than one set of modes to choose
     *  from, so we need to count at runtime)
     */
    if (modegroup == 0) {
        solid_modes = NUM_MODES1;
        modesNx = modesNx1;
        modes1x = modes1x1;
        modes_pwm = modes_pwm1;
    } else {
        solid_modes = NUM_MODES2;
        modesNx = modesNx2;
        modes1x = modes1x2;
        modes_pwm = modes_pwm2;
    }
    mode_cnt = solid_modes;
}

#ifdef VOLTAGE_MON
inline void ADC_on() {
    DIDR0 |= (1 << ADC_DIDR);                           // disable digital input on ADC pin to reduce power consumption
    ADMUX  = (1 << REFS0) | (1 << ADLAR) | ADC_CHANNEL; // 1.1v reference, left-adjust, ADC1/PB2
    ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;   // enable, start, prescale
}
#else
inline void ADC_off() {
    ADCSRA &= ~(1<<7); //ADC off
}
#endif

void set_output(uint8_t pwm1, uint8_t pwm2) {
    // Need PHASE to properly turn off the light
    if ((pwm1==0) && (pwm2==0)) {
        TCCR0A = PHASE;
    }
    PWM_LVL = pwm1;
    ALT_PWM_LVL = pwm2;
}

void set_mode(mode) {
    TCCR0A = pgm_read_byte(modes_pwm + mode);
    set_output(pgm_read_byte(modesNx + mode), pgm_read_byte(modes1x + mode));
    /*
    // Only set output for solid modes
    uint8_t out = pgm_read_byte(modesNx + mode);
    if ((out < 250) || (out == 255)) {
        set_output(pgm_read_byte(modesNx + mode), pgm_read_byte(modes1x + mode));
    }
    */
}

#ifdef VOLTAGE_MON
uint8_t get_voltage() {
    // Start conversion
    ADCSRA |= (1 << ADSC);
    // Wait for completion
    while (ADCSRA & (1 << ADSC));
    // See if voltage is lower than what we were looking for
    return ADCH;
}
#endif

void blink(uint8_t val)
{
    for (; val>0; val--)
    {
        set_output(0,20);
        _delay_ms(100);
        set_output(0,0);
        _delay_ms(400);
    }
}

int main(void)
{
    uint8_t cap_val;

    // Read the off-time cap *first* to get the most accurate reading
    // Start up ADC for capacitor pin
    DIDR0 |= (1 << CAP_DIDR);                           // disable digital input on ADC pin to reduce power consumption
    ADMUX  = (1 << REFS0) | (1 << ADLAR) | CAP_CHANNEL; // 1.1v reference, left-adjust, ADC3/PB3
    ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;   // enable, start, prescale

    // Wait for completion
    while (ADCSRA & (1 << ADSC));
    // Start again as datasheet says first result is unreliable
    ADCSRA |= (1 << ADSC);
    // Wait for completion
    while (ADCSRA & (1 << ADSC));
    cap_val = ADCH; // save this for later

    // All ports default to input, but turn pull-up resistors on for the stars (not the ADC input!  Made that mistake already)
    // only one star, because one is used for PWM channel 2
    // and the other is used for the off-time capacitor
    PORTB = (1 << STAR3_PIN);

    // Set PWM pin to output
    DDRB |= (1 << PWM_PIN);     // enable main channel
    DDRB |= (1 << ALT_PWM_PIN); // enable second channel

    // Set timer to do PWM for correct output pin and set prescaler timing
    //TCCR0A = 0x23; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
    //TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
    TCCR0A = PHASE;
    // Set timer to do PWM for correct output pin and set prescaler timing
    TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)

    // Read config values and saved state
#ifdef CONFIG_STARS
    check_stars();
#endif
    restore_state();
    // Enable the current mode group
    count_modes();


    if (cap_val > CAP_SHORT) {
        // Indicates they did a short press, go to the next mode
        next_mode(); // Will handle wrap arounds
        if (fast_presses < 15) fast_presses ++;
#ifdef OFFTIM3
    } else if (cap_val > CAP_MED) {
        // User did a medium press, go back one mode
        prev_mode(); // Will handle "negative" modes and wrap-arounds
        fast_presses = 0;
#endif
    } else {
        // Long press, keep the same mode
        // ... or reset to the first mode
        fast_presses = 0;
        if (! memory) {
            // Reset to the first mode
            mode_idx = 0;
        }
    }
    save_state();

    // Turn off ADC
    //ADC_off();

    // Charge up the capacitor by setting CAP_PIN to output
    DDRB  |= (1 << CAP_PIN);    // Output
    PORTB |= (1 << CAP_PIN);    // High

    // Turn features on or off as needed
    #ifdef VOLTAGE_MON
    ADC_on();
    #else
    ADC_off();
    #endif
    //ACSR   |=  (1<<7); //AC off

    // Enable sleep mode set to Idle that will be triggered by the sleep_mode() command.
    // Will allow us to go idle between WDT interrupts
    //set_sleep_mode(SLEEP_MODE_IDLE);  // not used due to blinky modes

    uint8_t output;
#ifdef NON_WDT_TURBO
    uint8_t ticks = 0;
#endif
#ifdef VOLTAGE_MON
    uint8_t lowbatt_cnt = 0;
    uint8_t i = 0;
    uint8_t voltage;
    // Make sure voltage reading is running for later
    ADCSRA |= (1 << ADSC);
#endif
    while(1) {
        output = pgm_read_byte(modesNx + mode_idx);
        if (fast_presses == 0x0f) {  // Config mode
            _delay_s();       // wait for user to stop fast-pressing button
            fast_presses = 0; // exit this mode after one use
            mode_idx = 0;

#ifdef CONFIG_STARS
            // Short/small version of the config mode
            // Toggle the mode group, blink, then exit
            modegroup ^= 1;
            save_state();
            count_modes();  // reconfigure without a power cycle
            blink(1);
#else
            // Longer/larger version of the config mode
            // Toggle the mode group, blink, un-toggle, continue
            modegroup ^= 1;
            save_state();
            blink(2);
            modegroup ^= 1;

            _delay_s();

            // Toggle memory, blink, untoggle, exit
            memory ^= 1;
            save_state();
            blink(2);
            memory ^= 1;

            save_state();
#endif  // ifdef CONFIG_STARS
/*        }
        else if (output == STROBE) {
#ifdef BIKING_STROBE
            // 2-level stutter beacon for biking and such
            for(i=0;i<4;i++) {
                set_output(255,0);
                _delay_ms(5);
                set_output(0,255);
                _delay_ms(65);
            }
            _delay_ms(720);
#else
            // 10Hz tactical strobe
            set_output(255,255);
            _delay_ms(50);
            set_output(0,0);
            _delay_ms(50);
#endif  // ifdef BIKING_STROBE
        }
        else if (output == BATTCHECK) {
            uint8_t blinks = 0;
            // turn off and wait one second before showing the value
            // (also, ensure voltage is measured while not under load)
            set_output(0,0);
            _delay_s();
            voltage = get_voltage();
            voltage = get_voltage(); // the first one is unreliable
            // division takes too much flash space
            //voltage = (voltage-ADC_LOW) / (((ADC_42 - 15) - ADC_LOW) >> 2);
            // a table uses less space than 5 logic clauses
            for (i=0; i<sizeof(voltage_blinks); i++) {
                if (voltage > pgm_read_byte(voltage_blinks + i)) {
                    blinks ++;
                }
            }

            // blink up to five times to show voltage
            // (~0%, ~25%, ~50%, ~75%, ~100%, >100%)
            blink(blinks);
            _delay_s();  // wait at least 1 second between readouts
        }
        else {  // Regular non-hidden solid mode
            set_mode(mode_idx);
  */          // This part of the code will mostly replace the WDT tick code.
#ifdef NON_WDT_TURBO
            // Do some magic here to handle turbo step-down
            if (ticks < 255) ticks++;
            if ((ticks > TURBO_TIMEOUT) 
                    && (output == TURBO)) {
                mode_idx = solid_modes - 2; // step down to second-highest mode
                set_mode(mode_idx);
                save_state();
            }
#endif
            // Otherwise, just sleep.
            _delay_ms(500);

            // If we got this far, the user has stopped fast-pressing.
            // So, don't enter config mode.
            if (fast_presses) {
                fast_presses = 0;
                save_state();
            }
        }
#ifdef VOLTAGE_MON
#if 1
        if (ADCSRA & (1 << ADIF)) {  // if a voltage reading is ready
            voltage = ADCH; // get_voltage();
            // See if voltage is lower than what we were looking for
            //if (voltage < ((mode_idx <= 1) ? ADC_CRIT : ADC_LOW)) {
            if (voltage < ADC_LOW) {
                lowbatt_cnt ++;
            } else {
                lowbatt_cnt = 0;
            }
            // See if it's been low for a while, and maybe step down
            if (lowbatt_cnt >= 8) {
                // DEBUG: blink on step-down:
                //set_output(0,0);  _delay_ms(100);
                i = mode_idx; // save space by not accessing mode_idx more than necessary
                // properly track hidden vs normal modes
                if (i >= solid_modes) {
                    // step down from blinky modes to medium
                    i = 2;
                } else if (i > 0) {
                    // step down from solid modes one at a time
                    i -= 1;
                } else { // Already at the lowest mode
                    i = 0;
                    // Turn off the light
                    set_output(0,0);
                    // Power down as many components as possible
                    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
                    sleep_mode();
                }
                set_mode(i);
                mode_idx = i;
                save_state();
                lowbatt_cnt = 0;
                // Wait at least 2 seconds before lowering the level again
                _delay_ms(250);  // this will interrupt blinky modes
            }

            // Make sure conversion is running for next time through
            ADCSRA |= (1 << ADSC);
        }
#endif
#endif  // ifdef VOLTAGE_MON
        //sleep_mode();  // incompatible with blinky modes
    }

    //return 0; // Standard Return Code
}


Output:
Line 29: error: util/delay_basic.h: No such file or directory
Line 166: error: expected ')' before 'n'
Line 25: error: avr/pgmspace.h: No such file or directory
Line 23: error: avr/eeprom.h: No such file or directory
Line 22: error: avr/sleep.h: No such file or directory
Line 207: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'eepos'
Line 208: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'memory'
Line 209: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'modegroup'
Line 210: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'mode_idx'
Line 211: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'fast_presses'
Line 218: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'mode_cnt'
Line 220: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'solid_modes'
Line 227: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
Line 228: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
Line 229: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 231: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
Line 232: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
Line 233: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 235: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
Line 236: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
Line 237: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 239: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
In function 'save_state':
Line 248: error: 'uint8_t' undeclared (first use in this function)
Line 248: error: (Each undeclared identifier is reported only once
Line 248: error: for each function it appears in.)
Line 248: error: expected ';' before 'oldpos'
Line 250: error: 'uint16_t' undeclared (first use in this function)
Line 250: error: expected ';' before 'eep'
Line 252: error: 'eepos' undeclared (first use in this function)
Line 255: error: 'eep' undeclared (first use in this function)
Line 255: error: 'mode_idx' undeclared (first use in this function)
Line 255: error: 'fast_presses' undeclared (first use in this function)
Line 255: error: 'modegroup' undeclared (first use in this function)
Line 259: error: expected expression before ')' token
Line 260: error: expected expression before ')' token
Line 260: error: 'oldpos' undeclared (first use in this function)
In function 'restore_state':
Line 265: error: 'uint8_t' undeclared (first use in this function)
Line 265: error: expected ';' before 'eep1'
Line 266: error: expected ';' before 'eep2'
Line 268: error: 'eepos' undeclared (first use in this function)
Line 269: error: 'eep1' undeclared (first use in this function)
Line 269: error: expected ')' before 'uint8_t'
Line 270: error: 'eep2' undeclared (first use in this function)
Line 270: error: expected ')' before 'uint8_t'
Line 275: error: 'mode_idx' undeclared (first use in this function)
Line 276: error: 'fast_presses' undeclared (first use in this function)
Line 277: error: 'modegroup' undeclared (first use in this function)
In function 'next_mode':
Line 286: error: 'mode_idx' undeclared (first use in this function)
Line 287: error: 'solid_modes' undeclared (first use in this function)
In function 'check_stars':
Line 330: error: 'PINB' undeclared (first use in this function)
Line 330: error: 'PB4' undeclared (first use in this function)
Line 331: error: 'memory' undeclared (first use in this function)
In function 'count_modes':
Line 347: error: 'modegroup' undeclared (first use in this function)
Line 348: error: 'solid_modes' undeclared (first use in this function)
Line 349: error: 'modesNx' undeclared (first use in this function)
Line 349: error: 'modesNx1' undeclared (first use in this function)
Line 350: error: 'modes1x' undeclared (first use in this function)
Line 350: error: 'modes1x1' undeclared (first use in this function)
Line 351: error: 'modes_pwm' undeclared (first use in this function)
Line 351: error: 'modes_pwm1' undeclared (first use in this function)
Line 354: error: 'modesNx2' undeclared (first use in this function)
Line 355: error: 'modes1x2' undeclared (first use in this function)
Line 356: error: 'modes_pwm2' undeclared (first use in this function)
Line 358: error: 'mode_cnt' undeclared (first use in this function)
In function 'ADC_on':
Line 363: error: 'DIDR0' undeclared (first use in this function)
Line 363: error: 'ADC1D' undeclared (first use in this function)
Line 364: error: 'ADMUX' undeclared (first use in this function)
Line 364: error: 'REFS0' undeclared (first use in this function)
Line 364: error: 'ADLAR' undeclared (first use in this function)
Line 365: error: 'ADCSRA' undeclared (first use in this function)
Line 365: error: 'ADEN' undeclared (first use in this function)
Line 365: error: 'ADSC' undeclared (first use in this function)
t.c: At top level:
Line 373: error: expected ')' before 'pwm1'
In function 'set_mode':
Line 383: error: 'TCCR0A' undeclared (first use in this function)
Line 383: error: 'modes_pwm' undeclared (first use in this function)
Line 384: error: 'modesNx' undeclared (first use in this function)
Line 384: error: 'modes1x' undeclared (first use in this function)
t.c: At top level:
Line 395: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'get_voltage'
Line 405: error: expected ')' before 'val'
In function 'main':
Line 418: error: 'uint8_t' undeclared (first use in this function)
Line 418: error: expected ';' before 'cap_val'
Line 422: error: 'DIDR0' undeclared (first use in this function)
Line 422: error: 'ADC3D' undeclared (first use in this function)
Line 423: error: 'ADMUX' undeclared (first use in this function)
Line 423: error: 'REFS0' undeclared (first use in this function)
Line 423: error: 'ADLAR' undeclared (first use in this function)
Line 424: error: 'ADCSRA' undeclared (first use in this function)
Line 424: error: 'ADEN' undeclared (first use in this function)
Line 424: error: 'ADSC' undeclared (first use in this function)
Line 432: error: 'cap_val' undeclared (first use in this function)
Line 432: error: 'ADCH' undeclared (first use in this function)
Line 437: error: 'PORTB' undeclared (first use in this function)
Line 437: error: 'PB4' undeclared (first use in this function)
Line 440: error: 'DDRB' undeclared (first use in this function)
Line 440: error: 'PB1' undeclared (first use in this function)
Line 441: error: 'PB0' undeclared (first use in this function)
Line 446: error: 'TCCR0A' undeclared (first use in this function)
Line 448: error: 'TCCR0B' undeclared (first use in this function)
Line 462: error: 'fast_presses' undeclared (first use in this function)
Line 473: error: 'memory' undeclared (first use in this function)
Line 475: error: 'mode_idx' undeclared (first use in this function)
Line 484: error: 'PB3' undeclared (first use in this function)
Line 499: error: expected ';' before 'output'
Line 501: error: expected ';' before 'ticks'
Line 504: error: expected ';' before 'lowbatt_cnt'
Line 505: error: expected ';' before 'i'
Line 506: error: expected ';' before 'voltage'
Line 511: error: 'output' undeclared (first use in this function)
Line 511: error: 'modesNx' undeclared (first use in this function)
Line 520: error: 'modegroup' undeclared (first use in this function)
Line 588: error: 'ticks' undeclared (first use in this function)
Line 591: error: 'solid_modes' undeclared (first use in this function)
Line 608: error: 'ADIF' undeclared (first use in this function)
Line 609: error: 'voltage' undeclared (first use in this function)
Line 613: error: 'lowbatt_cnt' undeclared (first use in this function)
Line 621: error: 'i' undeclared (first use in this function)
Line 634: error: 'SLEEP_MODE_PWR_DOWN' undeclared (first use in this function)


Create a new paste based on this one


Comments: