~toykeeper/flashlight-firmware/trunk

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
/* Simple sample firmware for otc less design (single cell).

   Half press less than about 300 ms: next mode
              between 300 and 900 ms: previous mode
              longer:                 blink out voltage reading 
                                      (0 to 255) in 3 decimals

   Suggested capacity of buffer cap (C2): >= 47 uF.
   Vbatt is connected to PB3 (Pin2).
   Voltage divider still connected to Pin7.
   Timing specified for Attiny25 at 10 Mhz.
   Don't fuse BOD.
   Created and built with Atmel Studio.
   Fuses for Attiny25: lfuse: 0xD2, hfuse: 0xDF.
*/

#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

#define PIN_7135            PB0
#define PIN_FET             PB1
#define PIN_POWER_DETECT    PB3
#define INT_POWER_DETECT    PCINT3
#define VLT_PIN             PB2
#define VLT_CHANNEL         0x01
#define VLT_DIDR            ADC1D
#define ADC_VREF            ((1 << REFS1) | (1 << REFS2))    // 2.56V
#define ADC_PRESCL          0x06

#define PWM_PHASE           0xA1
#define PWM_7135            OCR0A
#define PWM_FET             OCR0B

#define MODE_COUNT          6
#define PWM_LEVELS_7135     30, 80, 255, 255, 255,   0
#define PWM_LEVELS_FET      0,   0,   0,  30,  80, 255

// forward declarations
uint8_t adc_init_and_read();
void blink_once();
void blink_once_long();
void blink_value(uint8_t value);
void delay_250_micro_sec(uint8_t n);
void delay_50_milli_sec(uint8_t n);
void delay_1_sec();
void led_off();
void led_on();

volatile uint8_t s_clicked = 0;
const uint8_t pwm_levels_7135[] = { PWM_LEVELS_7135 };
const uint8_t pwm_levels_fet[] = { PWM_LEVELS_FET };
uint8_t mode_index = 0;

////////////////////////////////////////////////////////////////////////////////

int main(void)
{
    uint8_t blink_voltage = 0;
    uint8_t voltage;

    // set LED ports as output
    DDRB = (1 << PIN_7135) | (1 << PIN_FET);

    // initialize PWM
    TCCR0A = PWM_PHASE;
    TCCR0B = 0x01;

    // enable pin change interrupts
    PCMSK = (1 << INT_POWER_DETECT);
    GIMSK |= (1 << PCIE);
    sei();

    // start with first mode
    PWM_7135 = pwm_levels_7135[mode_index];
    PWM_FET = pwm_levels_fet[mode_index];

    while ( 1 )
    {
        if ( s_clicked )
        {
            if ( s_clicked <= 10 ) // < about 300 ms
            {
                blink_voltage = 0;
                mode_index++;
                if ( mode_index >= MODE_COUNT )
                    mode_index = 0;
            }
            else if ( s_clicked <= 30 ) // < about 900 ms
            {
                blink_voltage = 0;
                mode_index--;
                if  ( mode_index == 255 )
                    mode_index = MODE_COUNT - 1;
            }
            else // > about 900 ms
            {
                mode_index = 0;
                blink_voltage = 1;
            }

            s_clicked = 0;
            PWM_7135 = pwm_levels_7135[mode_index];
            PWM_FET  = pwm_levels_fet[mode_index];
        }

        voltage = adc_init_and_read();

        if ( blink_voltage )
            blink_value(voltage);

        delay_1_sec();
    }
} 

////////////////////////////////////////////////////////////////////////////////

ISR(PCINT0_vect)
{
    // disable output
    PORTB = 0;

    // turn off PWM generation (might not be necessary)
    TCCR0A = 0;
    TCCR0B = 0;

    // ADC off
    ADCSRA &= ~(1 << ADEN);

    // disable pin change interrupt
    GIMSK &= ~(1 << PCIE);
    PCMSK = 0;

    s_clicked = 0;
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);

    do
    {
        s_clicked++;

        wdt_reset();
        WDTCR = (1<<WDIE) | WDTO_30MS;

        sei();
        sleep_mode();
        cli();

    } while ( (PINB & (1 << PIN_POWER_DETECT)) == 0 );

    wdt_disable();

    // debounce
    delay_250_micro_sec(10); 

    // activate pwm generation
    TCCR0A = PWM_PHASE;
    TCCR0B = 0x01;

    // enable pin change interrupts
    PCMSK = (1 << INT_POWER_DETECT);
    GIMSK |= (1 << PCIE);
}

////////////////////////////////////////////////////////////////////////////////

void delay_250_micro_sec(uint8_t n)
{
    while ( n-- )
    {
        int k = 180;
        while ( k-- )
        {
            if ( s_clicked )
                return;
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

void  delay_50_milli_sec(uint8_t n)
{
    do
    {
        delay_250_micro_sec(200);
        if ( s_clicked )
            return;
    } while ( --n );
}

////////////////////////////////////////////////////////////////////////////////

void delay_1_sec()
{
    delay_50_milli_sec(20);
}

////////////////////////////////////////////////////////////////////////////////

void led_on()
{
    PWM_7135 = pwm_levels_7135[mode_index];
    PWM_FET  = pwm_levels_fet[mode_index];
}

////////////////////////////////////////////////////////////////////////////////

void led_off()
{
    PWM_7135 = 0;
    PWM_FET = 0;
}

////////////////////////////////////////////////////////////////////////////////

void blink_once()
{
    led_on();
    delay_50_milli_sec(5);
    led_off();
    delay_50_milli_sec(5);
}

////////////////////////////////////////////////////////////////////////////////

void blink_once_long()
{
    led_on();
    delay_1_sec();
    led_off();
    delay_1_sec();
}

////////////////////////////////////////////////////////////////////////////////

void blink_once_short()
{
    led_on();
    delay_50_milli_sec(1);
    led_off();
    delay_50_milli_sec(5);
}

////////////////////////////////////////////////////////////////////////////////

void blink_value(uint8_t value)
{
    // 100er
    if ( value < 100 )
        blink_once_short();

    while ( value >= 100 )
    {
        value -= 100;
        blink_once();
    }
    delay_1_sec();

    // 10er
    if ( value < 10 )
        blink_once_short();

    while ( value >= 10 )
    {
        value -= 10;
        blink_once();
    }
    delay_1_sec();

    // 1er
    if ( value == 0 )
        blink_once_short();

    while ( value > 0 )
    {
        value--;
        blink_once();
    }
    delay_1_sec();
}

////////////////////////////////////////////////////////////////////////////////

uint8_t adc_read()
{
    uint8_t value;

    while (ADCSRA & (1 << ADSC))
    {
        if ( s_clicked )
            return 0;
    }
    value = ADCH;
    ADCSRA |= (1 << ADSC);
    return value;
}

////////////////////////////////////////////////////////////////////////////////

uint8_t adc_init_and_read()
{
    DIDR0 |= (1 << VLT_DIDR);
    ADMUX  = ADC_VREF | (1 << ADLAR) | VLT_CHANNEL;
    ADCSRA = (1 << ADEN) | (1 << ADSC) | ADC_PRESCL;
    adc_read(); // first run not reliable (see spec)
    return adc_read();
}

////////////////////////////////////////////////////////////////////////////////
// empty interrupt function required otherwise mcu will reset

ISR(WDT_vect)
{
};

////////////////////////////////////////////////////////////////////////////////