~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to Tom_E/narsil/NarsilMulti/Channels.h

  • Committer: Selene Scriven
  • Date: 2017-09-12 23:34:36 UTC
  • mto: (188.1.3 trunk)
  • mto: This revision was merged to the branch mainline in revision 331.
  • Revision ID: bzr@toykeeper.net-20170912233436-d3w6nln0ts1subue
Added Flintrock's Bistro-HD 1.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************************
 
2
 * channels.h - output channel support with ramping tables
 
3
 * ==========
 
4
 *
 
5
 *    ATtiny85 Diagrams
 
6
 *    -----------------
 
7
 *
 
8
 * For TA style triples, 3 channel:
 
9
 *              ---
 
10
 *   Reset  1 -|   |- 8  VCC
 
11
 *  switch  2 -|   |- 7  Voltage ADC or Indicator LED
 
12
 * FET PWM  3 -|   |- 6  bank of 7135s PWM
 
13
 *     GND  4 -|   |- 5  one 7135 PWM
 
14
 *              ---
 
15
 *
 
16
 * For standard FET+1, 2 channels:
 
17
 *              ---
 
18
 *   Reset  1 -|   |- 8  VCC
 
19
 *  switch  2 -|   |- 7  Voltage ADC
 
20
 * Ind.LED  3 -|   |- 6  FET PWM
 
21
 *     GND  4 -|   |- 5  7135 PWM
 
22
 *              ---
 
23
 *
 
24
 * For 1 channel, using a NANJG or FET+1:
 
25
 *              ---
 
26
 *   Reset  1 -|   |- 8  VCC
 
27
 *  switch  2 -|   |- 7  Voltage ADC
 
28
 * Ind.LED  3 -|   |- 6  FET or 7135s PWM
 
29
 *     GND  4 -|   |- 5  Not Used (7135 PWM)
 
30
 *              ---
 
31
 *
 
32
 *
 
33
 ****************************************************************************************/
 
34
#ifndef CHANNELS_H_
 
35
#define CHANNELS_H_
 
36
 
 
37
//---------------------------------------------------------------------------------------
 
38
//---------------------------------------------------------------------------------------
 
39
#if   OUT_CHANNELS == 2                 // FET+1
 
40
//---------------------------------------------------------------------------------------
 
41
//---------------------------------------------------------------------------------------
 
42
 
 
43
#ifdef ONBOARD_LED
 
44
/**************************************************************************************
 
45
* TurnOnBoardLed
 
46
* ==============
 
47
**************************************************************************************/
 
48
void TurnOnBoardLed(byte on)
 
49
{
 
50
        if (onboardLedEnable)
 
51
        {
 
52
                if (on)
 
53
                {
 
54
                        DDRB = (1 << ONE7135_PWM_PIN) | (1 << FET_PWM_PIN) | (1 << ONBOARD_LED_PIN);
 
55
                        PORTB |= (1 << ONBOARD_LED_PIN);
 
56
                }
 
57
                else
 
58
                {
 
59
                        DDRB = (1 << ONE7135_PWM_PIN) | (1 << FET_PWM_PIN);
 
60
                        PORTB &= 0xff ^ (1 << ONBOARD_LED_PIN);
 
61
                }
 
62
        }
 
63
}
 
64
#endif
 
65
 
 
66
/**************************************************************************************
 
67
* DefineModeSet
 
68
* =============
 
69
**************************************************************************************/
 
70
void DefineModeSet()
 
71
{
 
72
        byte offset = 1;
 
73
 
 
74
        modesCnt = pgm_read_byte(modeSetCnts+modeSetIdx);
 
75
 
 
76
        // Set OFF mode states (index 0)
 
77
        by7135Modes[0] = byFETModes[0] = 0;
 
78
 
 
79
        if (moonLightEnable)
 
80
        {
 
81
                offset = 2;
 
82
                by7135Modes[1] = moonlightLevel;        // PWM value to use for moonlight mode
 
83
                byFETModes[1] = 0;
 
84
        }
 
85
 
 
86
        // Populate the RAM based current mode set
 
87
        for (int i = 0; i < modesCnt; i++)
 
88
        {
 
89
                by7135Modes[offset+i] = pgm_read_byte(modeTable7135[modeSetIdx]+i);
 
90
                byFETModes[offset+i] = pgm_read_byte(modeTableFet[modeSetIdx]+i);
 
91
        }
 
92
 
 
93
        modesCnt += offset;             // adjust to total mode count
 
94
}
 
95
 
 
96
/**************************************************************************************
 
97
* SetOutput - sets the PWM output values directly for the two channels
 
98
* =========
 
99
**************************************************************************************/
 
100
inline void SetOutput(byte pwm7135, byte pwmFet)
 
101
{
 
102
        ONE7135_PWM_LVL = pwm7135;
 
103
        FET_PWM_LVL = pwmFet;
 
104
}
 
105
 
 
106
/**************************************************************************************
 
107
* SetLevel - uses the ramping levels to set the PWM output
 
108
* ========              (0 is OFF, 1..RAMP_SIZE is the ramping index level)
 
109
**************************************************************************************/
 
110
void SetLevel(byte level)
 
111
{
 
112
        if (level == 0)
 
113
        {
 
114
                SetOutput(OFF_OUTPUT);
 
115
                
 
116
          #ifdef ONBOARD_LED
 
117
                if (locatorLed)
 
118
                        TurnOnBoardLed(1);
 
119
          #endif
 
120
        }
 
121
        else
 
122
        {
 
123
                level -= 1;     // make it 0 based
 
124
                SetOutput(pgm_read_byte(ramp_7135 + level), pgm_read_byte(ramp_FET  + level));
 
125
                
 
126
          #ifdef ONBOARD_LED
 
127
                if (locatorLed)
 
128
                        TurnOnBoardLed(0);
 
129
          #endif
 
130
        }
 
131
}
 
132
 
 
133
/**************************************************************************************
 
134
* SetMode
 
135
* =======
 
136
**************************************************************************************/
 
137
void inline SetMode(byte mode)
 
138
{
 
139
        currOutLvl1 = by7135Modes[mode];
 
140
        currOutLvl2 = byFETModes[mode];
 
141
        
 
142
        SetOutput(currOutLvl1, currOutLvl2);
 
143
 
 
144
  #ifdef ONBOARD_LED
 
145
        if ((mode == 0) && locatorLed)
 
146
                TurnOnBoardLed(1);
 
147
        else if (last_modeIdx == 0)
 
148
                TurnOnBoardLed(0);
 
149
  #endif
 
150
}
 
151
 
 
152
//---------------------------------------------------------------------------------------
 
153
//---------------------------------------------------------------------------------------
 
154
#elif OUT_CHANNELS == 3         // Triple Channel
 
155
//---------------------------------------------------------------------------------------
 
156
//---------------------------------------------------------------------------------------
 
157
 
 
158
#ifdef ONBOARD_LED
 
159
/**************************************************************************************
 
160
* TurnOnBoardLed
 
161
* ==============
 
162
**************************************************************************************/
 
163
void TurnOnBoardLed(byte on)
 
164
{
 
165
        if (onboardLedEnable)
 
166
        {
 
167
                if (on)
 
168
                {
 
169
                        DDRB = (1 << ONE7135_PWM_PIN) | (1 << BANK_PWM_PIN) | (1 << FET_PWM_PIN) | (1 << ONBOARD_LED_PIN);
 
170
                        
 
171
                        PORTB |= (1 << ONBOARD_LED_PIN);
 
172
                }
 
173
                else
 
174
                {
 
175
                        DDRB = (1 << ONE7135_PWM_PIN) | (1 << BANK_PWM_PIN) | (1 << FET_PWM_PIN);
 
176
                        PORTB &= 0xff ^ (1 << ONBOARD_LED_PIN);
 
177
                }
 
178
        }
 
179
}
 
180
#endif
 
181
 
 
182
/**************************************************************************************
 
183
* DefineModeSet
 
184
* =============
 
185
**************************************************************************************/
 
186
void DefineModeSet()
 
187
{
 
188
        byte offset = 1;
 
189
 
 
190
        modesCnt = pgm_read_byte(modeSetCnts+modeSetIdx);
 
191
 
 
192
        // Set OFF mode states (index 0)
 
193
        by7135Modes[0] = by7135sModes[0] = byFETModes[0] = 0;
 
194
 
 
195
        if (moonLightEnable)
 
196
        {
 
197
                offset = 2;
 
198
                by7135Modes[1] = moonlightLevel;        // PWM value to use for moonlight mode
 
199
                by7135sModes[1] = byFETModes[1] = 0;
 
200
        }
 
201
 
 
202
        // Populate the RAM based current mode set
 
203
        for (int i = 0; i < modesCnt; i++)
 
204
        {
 
205
                by7135Modes[offset+i] = pgm_read_byte(modeTable7135[modeSetIdx]+i);
 
206
                by7135sModes[offset+i] = pgm_read_byte(modeTable7135s[modeSetIdx]+i);
 
207
                byFETModes[offset+i] = pgm_read_byte(modeTableFet[modeSetIdx]+i);
 
208
        }
 
209
 
 
210
        modesCnt += offset;             // adjust to total mode count
 
211
}
 
212
 
 
213
/**************************************************************************************
 
214
* SetOutput - sets the PWM output value directly
 
215
* =========
 
216
**************************************************************************************/
 
217
inline void SetOutput(byte pwm7135, byte pwm7135s, byte pwmFet)
 
 
b'{'
 
218
        ONE7135_PWM_LVL = pwm7135;
 
219
        BANK_PWM_LVL = pwm7135s;
 
220
        FET_PWM_LVL = pwmFet;
 
221
}
 
222
/**************************************************************************************
 
223
* SetLevel - uses the ramping levels to set the PWM output
 
224
* ========              (0 is OFF, 1..RAMP_SIZE is the ramping index level)
 
225
**************************************************************************************/
 
226
void SetLevel(byte level)
 
 
b'{'
 
 
b'\tif (level == 0)'
 
 
b'\t{'
 
 
b'\t\tSetOutput(0,0,0);'
 
227
          #ifdef ONBOARD_LED
 
228
                if (locatorLed)
 
 
b'\t\t\tTurnOnBoardLed(1);'
 
229
          #endif
 
230
        }
 
 
b'\telse'
 
 
b'\t{'
 
 
b'\t\tlevel -= 1;\t// make it 0 based'
 
231
                SetOutput(pgm_read_byte(ramp_7135 + level), pgm_read_byte(ramp_7135s + level), pgm_read_byte(ramp_FET  + level));
 
232
          #ifdef ONBOARD_LED
 
233
                if (locatorLed)
 
 
b'\t\t\tTurnOnBoardLed(0);'
 
234
          #endif
 
235
        }
 
 
b'}'
 
236
/**************************************************************************************
 
237
* SetMode
 
238
* =======
 
239
**************************************************************************************/
 
240
void inline SetMode(byte mode)
 
241
{
 
242
        currOutLvl1 = by7135Modes[mode];
 
243
        currOutLvl2 = by7135sModes[mode];
 
244
        currOutLvl3 = byFETModes[mode];
 
245
        
 
246
        SetOutput(currOutLvl1, currOutLvl2, currOutLvl3);
 
247
  #ifdef ONBOARD_LED
 
248
        if ((mode == 0) && locatorLed)
 
249
                TurnOnBoardLed(1);
 
250
        else if (last_modeIdx == 0)
 
251
                TurnOnBoardLed(0);
 
252
  #endif
 
253
}
 
254
 
 
255
//---------------------------------------------------------------------------------------
 
256
//---------------------------------------------------------------------------------------
 
257
#elif OUT_CHANNELS == 1         // single FET or single bank of 7135's
 
258
//---------------------------------------------------------------------------------------
 
259
//---------------------------------------------------------------------------------------
 
260
 
 
261
#ifdef ONBOARD_LED
 
262
/**************************************************************************************
 
263
* TurnOnBoardLed
 
264
* ==============
 
265
**************************************************************************************/
 
266
void TurnOnBoardLed(byte on)
 
267
{
 
268
        if (onboardLedEnable)
 
269
        {
 
270
                if (on)
 
271
                {
 
272
                        DDRB = (1 << MAIN_PWM_PIN) | (1 << ONBOARD_LED_PIN);
 
273
                        PORTB |= (1 << ONBOARD_LED_PIN);
 
274
                }
 
275
                else
 
276
                {
 
277
                        DDRB = (1 << MAIN_PWM_PIN);
 
278
                        PORTB &= 0xff ^ (1 << ONBOARD_LED_PIN);
 
279
                }
 
280
        }
 
281
}
 
282
#endif
 
283
 
 
284
/**************************************************************************************
 
285
* DefineModeSet
 
286
* =============
 
287
**************************************************************************************/
 
288
void DefineModeSet()
 
289
{
 
290
        byte offset = 1;
 
291
 
 
292
        modesCnt = pgm_read_byte(modeSetCnts+modeSetIdx);
 
293
 
 
294
        // Set OFF mode states (index 0)
 
295
        byMainModes[0] = 0;
 
296
 
 
297
        if (moonLightEnable)
 
298
        {
 
299
                offset = 2;
 
300
                byMainModes[1] = moonlightLevel;        // PWM value to use for moonlight mode
 
301
        }
 
302
 
 
303
        // Populate the RAM based current mode set
 
304
        for (int i = 0; i < modesCnt; i++)
 
305
        {
 
306
                byMainModes[offset+i] = pgm_read_byte(modeTable1Chan[modeSetIdx]+i);
 
307
        }
 
308
 
 
309
        modesCnt += offset;             // adjust to total mode count
 
310
}
 
311
 
 
312
/**************************************************************************************
 
313
* SetOutput - sets the PWM output values directly for the one channel
 
314
* =========
 
315
**************************************************************************************/
 
316
inline void SetOutput(byte pwmChan)
 
317
{
 
318
        MAIN_PWM_LVL = pwmChan;
 
319
}
 
320
 
 
321
/**************************************************************************************
 
322
* SetLevel - uses the ramping levels to set the PWM output
 
323
* ========              (0 is OFF, 1..RAMP_SIZE is the ramping index level)
 
324
**************************************************************************************/
 
325
void SetLevel(byte level)
 
326
{
 
327
        if (level == 0)
 
328
        {
 
329
                SetOutput(OFF_OUTPUT);
 
330
                
 
331
                #ifdef ONBOARD_LED
 
332
                if (locatorLed)
 
333
                TurnOnBoardLed(1);
 
334
                #endif
 
335
        }
 
336
        else
 
337
        {
 
338
                level -= 1;     // make it 0 based
 
339
                SetOutput(pgm_read_byte(ramp_1Chan + level));
 
340
                
 
341
                #ifdef ONBOARD_LED
 
342
                if (locatorLed)
 
343
                TurnOnBoardLed(0);
 
344
                #endif
 
345
        }
 
346
}
 
347
 
 
348
/**************************************************************************************
 
349
* SetMode
 
350
* =======
 
351
**************************************************************************************/
 
352
void inline SetMode(byte mode)
 
353
{
 
354
        currOutLvl = byMainModes[mode];
 
355
        
 
356
        SetOutput(currOutLvl);
 
357
 
 
358
        #ifdef ONBOARD_LED
 
359
        if ((mode == 0) && locatorLed)
 
360
        TurnOnBoardLed(1);
 
361
        else if (last_modeIdx == 0)
 
362
        TurnOnBoardLed(0);
 
363
        #endif
 
364
}
 
365
 
 
366
#endif
 
367
 
 
368
#endif /* CHANNELS_H_ */
 
 
b'\\ No newline at end of file'