Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2136 - 1
/*****************************************************************************
2
 *   Copyright (C) 2008 Thomas Kaiser, thomas@ft-fanpage.de                  *
3
 *   Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net                  *
4
 *   based on the key handling by Peter Dannegger                            *
5
 *     see www.mikrocontroller.net                                           *
6
 *   Copyright (C) 2011 Christian "Cebra" Brandtner, brandtner@brandtner.net *
7
 *   Copyright (C) 2011 Harald Bongartz                                      *
8
 *                                                                           *
9
 *   This program is free software; you can redistribute it and/or modify    *
10
 *   it under the terms of the GNU General Public License as published by    *
11
 *   the Free Software Foundation; either version 2 of the License.          *
12
 *                                                                           *
13
 *   This program is distributed in the hope that it will be useful,         *
14
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
16
 *   GNU General Public License for more details.                            *
17
 *                                                                           *
18
 *   You should have received a copy of the GNU General Public License       *
19
 *   along with this program; if not, write to the                           *
20
 *   Free Software Foundation, Inc.,                                         *
21
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.               *
22
 *                                                                           *
23
 *                                                                           *
24
 *   Credits to:                                                             *
25
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN  *
26
 *                          http://www.mikrokopter.de                        *
27
 *   Gregor "killagreg" Stobrawa for his version of the MK code              *
28
 *   Thomas Kaiser "thkais" for the original project. See                    *
29
 *                          http://www.ft-fanpage.de/mikrokopter/            *
30
 *                          http://forum.mikrokopter.de/topic-4061-1.html    *
31
 *   Claas Anders "CaScAdE" Rathje for providing the font and his C-OSD code *
32
 *                          http://www.mylifesucks.de/oss/c-osd/             *
33
 *   Harald Bongartz "HaraldB" for providing his Ideas and Code for usibility*
34
 *****************************************************************************/
35
 
36
//############################################################################
37
//# HISTORY  timer.c
38
//#
39
//#
40
//# 08.08.2015 CB
41
//# - add: timer_nmea_timeout
42
//#
43
//# 29.06.2014 OG
44
//# - chg: ISR( TIMER0_COMPA_vect ) - LipoCheck() wieder hinzugefuegt
45
//# - chg: LipoCheck() - leichte optische Anpassung und lcd-Ausgabe an ISR angepasst
46
//# - add: #include "../lipo/lipo.h"
47
//#
48
//# 24.06.2014 OG
49
//# - add: timer_gauge
50
//# - add: #include "../pkt/pkt.h"
51
//#
52
//# 13.06.2014 OG
53
//# - del: IdleTimer entfernt da nicht verwendet
54
//# - chg: ISR(TIMER0_COMPA_vect) - "PKT aus nach" (via timer_pkt_off)
55
//# - chg: ISR(TIMER0_COMPA_vect) - "LCD aus nach" umgestellt von Sekunden
56
//#        auf Minuten (via timer_lcd_off)
57
//# - chg: Code-Formattierung
58
//#
59
//# 31.05.2014 OG
60
//# - chg: 'timer_pktctrl' umbenannt zu 'timer_pktupdatecheck' (exklusiv fuer pkt.c)
61
//#
62
//# 25.04.2014 OG
63
//# - add: timer_get_displaydata
64
//#
65
//# 24.04.2014 OG
66
//# - add: timer1 (generischer Timer)
67
//# - add: timer3 (generischer Timer)
68
//# - add: timer_mk_timeout
69
//# - del: timer_get_erdata
70
//#
71
//# 29.03.2014 OG
72
//# - add: clear_key_all() - loescht ALLE Tasten egal ob short, long, repeat...
73
//#
74
//# 13.06.2013 cebra
75
//# - chg: Abfrage nach Hardwaresound bei Displaybeleuchtung entfällt
76
//#
77
//# 19.05.2013 OG
78
//# - add: timer_pktctrl - exclusive Verwendung von pkt.c
79
//#
80
//# 24.03.2013 OG
81
//# - add: UTCTime - inkrementiert wird UTCTime.seconds auf der Basis
82
//#        von timer_pkt_uptime (modulo 100) aktuell initialisert von
83
//#        der MK-NC Time durch OSD_MK_UTCTime()
84
//# - add: timer_get_tidata
85
//#
86
//# 21.03.2013 OG
87
//# - add: timer_pkt_uptime (auch in timer.h)
88
//# - add: timer_osd_refresh (auch in timer.h)  -> verwendet in osd.c
89
//# - add: timer_get_bldata (auch in timer.h)   -> verwendet in osd.c
90
//# - add: timer_get_erdata (auch in timer.h)   -> verwendet in osd.c
91
//#
92
//# 09.03.2013 OG
93
//# - add: timer2 (auch in timer.h)
94
//############################################################################
95
 
96
 
97
#include "../cpu.h"
98
#include <avr/io.h>
99
#include <avr/interrupt.h>
100
#include <avr/pgmspace.h>
101
#include <string.h>
102
#include <util/delay.h>
103
#include <inttypes.h>
104
 
105
 
106
#include "../main.h"
107
#include "../timer/timer.h"
108
#include "../eeprom/eeprom.h"
109
#include "../lcd/lcd.h"
110
#include "../uart/uart1.h"
111
#include "../bluetooth/bluetooth.h"
112
#include "../setup/setup.h"
113
#include"../tracking/tracking.h"
114
#include "../sound/pwmsine8bit.h"
115
#include "../lipo/lipo.h"
116
#include "../pkt/pkt.h"
117
 
118
//
119
//#if defined HWVERSION1_2W || defined HWVERSION1_2
120
//#include "HAL_HW1_2.h"
121
//#endif
122
//
123
//#if defined HWVERSION1_3W || defined HWVERSION1_3
124
//#include "HAL_HW1_3.h"
125
//#endif
126
 
127
//#ifdef HWVERSION3_9
128
#include "../HAL_HW3_9.h"
129
//#endif
130
 
131
//----------------------
132
// generische Timer
133
//----------------------
134
volatile uint16_t timer;
135
volatile uint16_t timer1;
136
volatile uint16_t timer2;
137
volatile uint16_t timer3;
138
 
139
//----------------------
140
// spezielle Timer
141
//----------------------
142
volatile uint16_t timer_pktupdatecheck;     // verwendet von pkt.c (!EXKLUSIV!)
143
volatile uint16_t timer_osd_refresh;        // verwendet in osd.c
144
volatile uint16_t timer_get_bldata;         // verwendet in osd.c
145
volatile uint16_t timer_get_tidata;         // verwendet in osd.c
146
volatile uint16_t timer_get_displaydata;    // verwendet in osd.c
147
volatile uint16_t timer_mk_timeout;         // verwendet u.a. in osd.c
148
volatile uint16_t timer_gauge;              // verwendet in pkt.c fuer den rotierenden Wartekreisel
149
volatile uint16_t timer_nmea_timeout;       // verwendet in nmea.c
150
volatile uint16_t abo_timer;
151
 
152
uint32_t timer_lcd_off = 0;                 // LCD aus nach... (der Zaehler muss 32Bit sein)
153
uint32_t timer_pkt_off = 0;                 // PKT aus nach... (der Zaehler muss 32Bit sein)
154
 
155
volatile uint16_t sound_timer;
156
volatile uint16_t soundpause_timer;
157
volatile static unsigned int tim_main;
158
 
159
volatile PKTdatetime_t  UTCTime;
160
volatile uint32_t timer_pkt_uptime = 0;     // OG: ja, 32 bit muss so sein!
161
 
162
uint8_t key_state = 0;                      // debounced and inverted key state:
163
                                            // bit = 1: key pressed
164
uint8_t key_press = 0;                      // key press detect
165
uint8_t key_long  = 0;                      // key long press
166
uint8_t key_rpt   = 0;                      // key repeat
167
uint8_t key_lrpt  = 0;                      // key long press and repeat
168
uint8_t key_rpts  = 0;                      // key long press and speed repeat
169
uint8_t repeat_speed = 0;
170
 
171
 
172
volatile uint8_t Display_on;                // LCD Flag Display on/off
173
 
174
volatile uint16_t WarnCount  = 0;           // Zähler der LIPO Warnzeit
175
volatile uint16_t WarnToggle = 0;           // Togglezähler zum blinken
176
volatile uint16_t WarnTime   = 10;          // Laenge der LIPO Warnzeit 10 Sek.
177
volatile uint16_t PoffTime   = 15;          // Laenge der Wartezeit vor abschalten 15 Sek.
178
//volatile uint16_t PoffTime   = 30;          // Laenge der Wartezeit vor abschalten 30 Sek.
179
 
180
 
181
 
182
unsigned int BeepTime   = 0;
183
unsigned int BeepMuster = 0xffff;
184
unsigned int BeepPrio   = 0;
185
 
186
volatile unsigned int CountMilliseconds = 0;
187
 
188
 
189
 
190
//--------------------------------------------------------------
191
// System (100Hz)
192
//--------------------------------------------------------------
193
void Timer0_Init( void )
194
{
195
    timer = 0;
196
 
197
    TCCR0A = (1 << WGM01);
198
    TCCR0B = (1 << CS02) | (1 << CS00);
199
    OCR0A  = (F_CPU / (100L * 1024L)) ;
200
 
201
    TIMSK0 |= (1 << OCIE0A);        // enable interrupt for OCR
202
}
203
 
204
 
205
 
206
//--------------------------------------------------------------
207
//--------------------------------------------------------------
208
//void Timer1_Init (void) // Timer 1-A
209
//{
210
//  // löschen
211
//  TCCR1A = 0;
212
//  TCCR1B = 0;
213
//  TIMSK1 = 0;
214
//
215
//  // setzen
216
//  TCCR1A |=   (1 << COM1A1) | (1 << WGM11);
217
//  TCCR1B |=   (1 << CS11)   | (1 << CS10)   | (1 << WGM13)  | (1 << WGM12);
218
//
219
//  ICR1 = (F_CPU / 64) * 20 / 1000;
220
//
221
//  OCR1A = 470;  // ca. Servomitte
222
//}
223
 
224
 
225
 
226
 
227
//--------------------------------------------------------------
228
//--------------------------------------------------------------
229
ISR( TIMER2_COMPA_vect )
230
{
231
    PORTD |= (1 << PD7);
232
}
233
 
234
 
235
//--------------------------------------------------------------
236
//--------------------------------------------------------------
237
ISR( TIMER2_COMPB_vect )
238
{
239
    PORTD &= ~(1 << PD7);
240
}
241
 
242
 
243
 
244
//--------------------------------------------------------------
245
//--------------------------------------------------------------
246
void Timer2_Init( void )
247
{
248
    if( Config.HWSound == 1 )
249
    {                                                                   // Sound PWM
250
        TCCR2A = 0x00; //stop
251
        ASSR   = 0x00; //set async mode
252
        TCNT2  = 0x00; //setup
253
        OCR2A  = 0xff;
254
        //Fast PWM 0xFF BOTTOM MAX
255
        //Set OC2A on Compare Match
256
        //clkT2S/8 (From prescaler)
257
        TCCR2A |= (1 << WGM20) | (1 << WGM21) |(1 << COM2A1) | (1 << COM2A0);
258
        TCCR2B |= (1 << CS20);
259
    }
260
    else
261
    {                                                                   // Displayhelligkeit
262
        DDRD   |= (1 << DDD7);                                          // PD7 output
263
        TCCR2A |= (1 << WGM21)  | (1 << WGM20)  | (1 << COM2A1);        // non invers
264
        TCCR2B |= (1 << CS20);                                          // Prescaler 1/1
265
        TIMSK2 |= (1 << OCIE2A) | (1 << OCIE2B);
266
        OCR2A = 255;
267
    }
268
}
269
 
270
 
271
 
272
//--------------------------------------------------------------
273
// Sound, Timer CTC
274
//--------------------------------------------------------------
275
void Timer3_Init( void )
276
{
277
    TCCR3A = 0x00;              // stop
278
    TCNT3H = 0xF8;              // set count
279
    TCNT3L = 0x00;              // set count
280
    OCR3AH = 0x07;              // set compare
281
    OCR3AL = 0xFF;              // set compare
282
    TCCR3A |= (1 << WGM31);
283
    TCCR3B |= (1 << CS30);
284
    TIMSK3 |= (1 << OCIE3A);    // timer interrupt sources  2=t0 compare
285
}
286
 
287
 
288
 
289
//--------------------------------------------------------------
290
//--------------------------------------------------------------
291
#ifdef USE_SOUND
292
ISR(TIMER3_COMPA_vect)  // Sound
293
{
294
    //void timer0_comp_isr(void){
295
    //was 8 KHz 125usec  sampling rate
296
    //now 12 KHz 83usec  sampling rate
297
    unsigned char oldfpart;
298
    signed char fullsamp;
299
    signed int tmp;
300
 
301
    TCNT3 = 0;
302
 
303
    if( generate )
304
    {
305
        DDRD |= (1<<DDD7);              // Port aus Ausgang
306
        oldfpart=waveptfpart;           // remember fractional part
307
        waveptfpart+=freqincfpart;      // add frac part of freq inc to wave pointer
308
        if( waveptfpart < oldfpart )    // did it walk off the end?
309
        {
310
            waveptipart++;              // yes, bump integer part
311
        }
312
        waveptipart+=freqincipart;      // add int part of freq increment to wave pointer
313
        fullsamp=sindat[waveptipart];   // get 8 bit sin sample from table (signed)
314
        tmp=fullsamp*iattenfac;         // cvt 7 bit x 8 = 15 bit
315
        OCR2A=(tmp >> 8)+128;           // cvt 15 bit signed to 8 bit unsigned
316
    }
317
    else
318
    {
319
        DDRD &= ~(1 << DDD7);           // Port auf Eingang, sperrt das Rauschen
320
    }
321
}
322
#endif
323
 
324
 
325
 
326
//--------------------------------------------------------------
327
// Timer-Interrupt (100 Hz)
328
//--------------------------------------------------------------
329
ISR( TIMER0_COMPA_vect )
330
{
331
    static uint8_t ct0 = 0;
332
    static uint8_t ct1 = 0;
333
    static uint8_t k_time_l = 0;
334
    static uint8_t k_time_r = 0;
335
    static uint8_t k_time_lr = 0;
336
    static uint8_t k_time_rs = 0;
337
    uint8_t i;
338
 
339
    static unsigned char cnt_1ms = 1,cnt = 0;
340
    unsigned char beeper_ein = 0;
341
 
342
 
343
    i = key_state ^ ~KEY_PIN;                                       // key changed ?
344
    ct0 = ~(ct0 & i);                                               // reset or count ct0
345
    ct1 = ct0 ^ (ct1 & i);                                          // reset or count ct1
346
    i &= (ct0 & ct1);                                               // count until roll over ?
347
    key_state ^= i;                                                 // then toggle debounced state
348
    key_press |= (key_state & i);                                   // 0->1: key press detect
349
 
350
 
351
    if( !cnt-- )
352
    {
353
        cnt = 9;
354
        CountMilliseconds++;
355
        cnt_1ms++;
356
    }
357
 
358
 
359
    //--------------------------------
360
    // Key pressed -> Timer Reset
361
    //--------------------------------
362
    if( i!=0 )                                                      // eine Taste wurde gedrueckt! -> Timer Rest und weiteres...
363
    {
364
        if( Display_on == 0 )                                       // ggf. Displaylicht einschalten
365
          set_D_LIGHT();
366
 
367
        Display_on    = 1;                                          // Flag Display on
368
        timer_lcd_off = 0;                                          // Timer Reset
369
        timer_pkt_off = 0;                                          // Timer Reset
370
    }
371
 
372
 
373
    //--------------------------------
374
    // LCD off Timeout
375
    // LCD ausschalten nach n Minuten
376
    //--------------------------------
377
    if( (Config.DisplayTimeout > 0) && (Display_on == 1) )
378
    {
379
        timer_lcd_off++;
380
        if( (timer_lcd_off/(100*60)) == Config.DisplayTimeout )     // ISR laeuft mit 100Hz; umgerechnet auf Minuten
381
        {
382
            clr_D_LIGHT();                                          // Displaylicht ausschalten
383
            Display_on = 0;                                         // Flag Display off
384
        }
385
    }
386
 
387
 
388
    //--------------------------------
389
    // PKT off Timeout
390
    // PKT ausschalten nach n Minuten
391
    //--------------------------------
392
    if( Config.PKTOffTimeout > 0 )
393
    {
394
        timer_pkt_off++;
395
        if( (timer_pkt_off/(100*60)) == Config.PKTOffTimeout )      // ISR laeuft mit 100Hz; umgerechnet auf Minuten
396
        {
397
            WriteParameter();                                       // am Ende alle Parameter sichern
398
            set_beep( 50, 0xffff, BeepNormal );                     // ein Mini-Beep zum Abschied (laenger geht nicht, wahrscheinlich wegen der ISR)
399
            clr_V_On();                                             // Spannung abschalten
400
        }
401
    }
402
 
403
 
404
    //--------------------------------
405
    // PKT bei Unterspannung abschalten
406
    //--------------------------------
407
    LipoCheck();
408
 
409
 
410
    //--------------------------------
411
    // Beeper
412
    //--------------------------------
413
    if( Config.HWBeeper==1 )
414
    {
415
        if( BeepTime )
416
        {
417
            if( BeepTime > 10 )         BeepTime -= 10;
418
            else                        BeepTime = 0;
419
 
420
            if( BeepTime & BeepMuster ) beeper_ein = 1;
421
            else                        beeper_ein = 0;
422
        }
423
        else
424
        {
425
            beeper_ein = 0;
426
            BeepMuster = 0xffff;
427
            BeepPrio   = BeepNormal;
428
        }
429
 
430
        if( beeper_ein == 1 )           set_BEEP();
431
        else                            clr_BEEP();
432
    }
433
 
434
 
435
    //--------------------------------
436
    // Sound
437
    //--------------------------------
438
    #ifdef USE_SOUND
439
    if (sound_timer > 0)                                            // Ton spielen
440
    {
441
        sound_timer--;
442
    }
443
    else
444
    {
445
      //TIMSK2 &= ~_BV(TOIE2);                                      // Interrupt sperren, verhindert Störgeräusche
446
      //TCCR2A = 0x00; //stop
447
        generate = 0;                                               // Sound aus
448
        tone_handler();
449
        if (soundpause_timer > 0)
450
        {
451
          soundpause_timer --;        // Ton pause
452
        }
453
    }
454
    #endif
455
 
456
 
457
    //--------------------------------
458
    // Tasten
459
    //--------------------------------
460
    if ((key_state & LONG_MASK) == 0)                               // check long key function
461
        k_time_l = REPEAT_START;                                    // start delay
462
 
463
    if (--k_time_l == 0)                                            // long countdown
464
        key_long |= (key_state & LONG_MASK);
465
 
466
    //------
467
    if ((key_state & REPEAT_MASK) == 0)                             // check repeat function
468
        k_time_r = 1;                                               // kein delay
469
 
470
    if (--k_time_r == 0)
471
    {
472
        k_time_r = REPEAT_NEXT;                                     // repeat delay
473
        key_rpt |= (key_state & REPEAT_MASK);
474
    }
475
 
476
    //------
477
    if ((key_state & LONG_REPEAT_MASK) == 0)                        // check repeat function
478
        k_time_lr = REPEAT_START;                                   // start delay
479
 
480
    if (--k_time_lr == 0)
481
    {
482
        k_time_lr = REPEAT_NEXT;                                    // repeat delay
483
        key_lrpt |= (key_state & LONG_REPEAT_MASK);
484
    }
485
 
486
    //------
487
    if ((key_state & LONG_REPEAT_SP_MASK) == 0)                     // check repeatX function
488
        k_time_rs = REPEAT_START;                                   // start delay
489
 
490
 
491
    if( --k_time_rs == 0 )                                          // repeat countdown
492
    {
493
        if( repeat_speed == 1 )
494
        {
495
            k_time_rs = REPEAT_SPEED_1;
496
            key_rpts |= (key_state & LONG_REPEAT_SP_MASK);
497
        }
498
        else if( repeat_speed == 2 )
499
        {
500
            k_time_rs = REPEAT_SPEED_2;
501
            key_rpts |= (key_state & LONG_REPEAT_SP_MASK);
502
        }
503
        else if( repeat_speed == 3 )
504
        {
505
            k_time_rs = REPEAT_SPEED_3;
506
            key_rpts |= (key_state & LONG_REPEAT_SP_MASK);
507
        }
508
    }
509
 
510
 
511
    //--------------------------------
512
    // generische Timer
513
    //--------------------------------
514
    if( timer > 0 )                 timer --;
515
    if( timer1 > 0 )                timer1 --;
516
    if( timer2 > 0 )                timer2 --;
517
    if( timer3 > 0 )                timer3 --;
518
 
519
 
520
    //--------------------------------
521
    // spezielle Timer
522
    //--------------------------------
523
    if( timer_osd_refresh > 0 )     timer_osd_refresh--;            // Timer fuer OSD-Screenrefresh (verwendet von osd.c)
524
    if( timer_get_bldata > 0 )      timer_get_bldata--;             // Timer um BL-Daten zu holen (verwendet von osd.c)
525
    if( timer_get_tidata > 0 )      timer_get_tidata--;             // Timer um Datum/Zeit vom MK zu holen (verwendet von osd.c)
526
    if( timer_get_displaydata > 0 ) timer_get_displaydata--;        // Timer um Display vom MK zu holen (verwendet von osd.c)
527
    if( timer_mk_timeout > 0 )      timer_mk_timeout--;             // verwendet u.a. von osd.c
528
    if( abo_timer > 0 )             abo_timer --;                   // Timer zum anfordern neuer Abo-Datenpakete wie OSD oder BL-Daten (verwendet u.a. von osd.c)
529
    if( timer_pktupdatecheck > 0 )  timer_pktupdatecheck--;         // Timer fuer pkt.c (PKT-Update-Check) - * FUER NICHTS ANDERES! *
530
    if( timer_nmea_timeout > 0 )    timer_nmea_timeout--;           // verwendet u.a. von osd.c
531
 
532
    //--------------------------------
533
    //--------------------------------
534
    if( Gauge_active )                                              // Gauge_active -> pkt.c
535
    {
536
        if( timer_gauge > 0 )       timer_gauge--;
537
        if( timer_gauge==0 )        PKT_Gauge_Next();
538
    }
539
 
540
 
541
    //--------------------------------
542
    // PKT Uptime Timer
543
    //--------------------------------
544
    timer_pkt_uptime++;
545
 
546
    if( timer_pkt_uptime % 100 == 0 )                               // theoretisch muesste noch die Tagesgrenze (0 Uhr) implementiert werden...
547
        UTCTime.seconds++;
548
 
549
} // end: ISR(TIMER0_COMPA_vect)
550
 
551
 
552
 
553
 
554
 
555
//--------------------------------------------------------------
556
// Lowbatpin des Spannungswandlers pruefen
557
// LBO des LT1308 wechselt zum Ende der Batterielaufzeit haeufig seinen Zustand in der Uebergangsphase zum LowBat
558
// Die Akkuspannung schwankt auch abhaengig vom momentanen Stromverbrauch
559
//--------------------------------------------------------------
560
void LipoCheck( void )
561
{
562
    uint8_t lcd_xpos_save;
563
    uint8_t lcd_ypos_save;
564
 
565
 
566
    if( WarnToggle == 1 )                               // Beim ersten Auftreten Warnung ausgeben, Rythmus 5/10 Sekunden
567
    {
568
        set_beep( 1000, 0x0020, BeepNormal);
569
 
570
        lcd_xpos_save = lcd_xpos;                       // innerhalb einer ISR -> LCD Screenpos muss gesichert werden!
571
        lcd_ypos_save = lcd_ypos;
572
 
573
        //lcdx_cls_row( y, mode, yoffs )
574
        lcdx_cls_row( 0, MINVERS ,0 );                              // Zeile 0 komplett invers
575
        lcd_printp_at( 0, 0, PSTR(" ** PKT LiPo! ** "), MINVERS);   // und Warn-Text ausgeben
576
 
577
        lcd_xpos = lcd_xpos_save;                                   // lcd Screenpos wieder herstellen
578
        lcd_ypos = lcd_ypos_save;
579
    }
580
 
581
 
582
    if( WarnToggle == WarnTime * 100 )
583
        WarnToggle = 0;                                 // erstmal bis hier warnen
584
 
585
 
586
    if( WarnToggle > 0 )
587
        WarnToggle++;                                   // weiter hochzaehlen
588
 
589
 
590
    if( PINC & (1 << LowBat) )                          // Kurzzeitige Unterspannung bearbeiten und Warnung ausgeben
591
    {
592
        WarnCount = 0;
593
      //if (WarnCount > 0)
594
      //    WarnCount--;                                // Bei LIPO OK erstmal runterzaehlen, LT1308 ueberlegt sich noch genauer ob nun ok oder nicht
595
    }
596
 
597
 
598
    if( !(PINC & (1 << LowBat)) )                       // LT1308 hat Unterspannung erkannt
599
    {
600
        WarnCount++;                                    // solange LBO low ist Zähler hochzählen
601
        if( (WarnCount == 10) && (WarnToggle == 0) )    // mit "10" etwas unempfindlicher gegen kurze Impulse machen
602
            WarnToggle = 1;                             // Warnhinweis starten
603
    }
604
 
605
 
606
    if( WarnCount == (PoffTime * 100) )
607
    {
608
        set_beep( 50, 0xffff, BeepNormal );             // ein Mini-Beep zum Abschied (laenger geht nicht, wahrscheinlich wegen der ISR)
609
        WriteParameter();                               // am Ende alle Parameter sichern
610
        clr_V_On();                                     // Spannung abschalten
611
    }
612
}
613
 
614
 
615
 
616
//--------------------------------------------------------------
617
//--------------------------------------------------------------
618
//void LipoCheck_OLD( void )
619
//{
620
//    if( WarnToggle == 1 )                               // Beim ersten Auftreten Warnung ausgeben, Rythmus 5/10 Sekunden
621
//    {
622
//        set_beep ( 1000, 0x0020, BeepNormal);
623
//        lcd_printp_at (0, 0, PSTR("  LIPO  !!Warnung!!  "), 2);
624
//    }
625
//
626
//    if( WarnToggle == WarnTime * 100 )
627
//        WarnToggle = 0;                                 // erstmal bis hier warnen
628
//
629
//    if( WarnToggle > 0 )
630
//        WarnToggle++;                                   // weiter hochzaehlen
631
//
632
//    if( PINC & (1 << LowBat) )                          // Kurzzeitige Unterspannung bearbeiten und Warnung ausgeben
633
//    {
634
//        WarnCount = 0;
635
//      //if (WarnCount > 0)
636
//      //    WarnCount--;                                // Bei LIPO OK erstmal runterzaehlen, LT1308 ueberlegt sich noch genauer ob nun ok oder nicht
637
//    }
638
//
639
//    if (!(PINC & (1 << LowBat)) )                       // LT1308 hat Unterspannung erkannt
640
//    {
641
//        WarnCount++;                                    // solange LBO low ist Zähler hochzählen
642
//        if( (WarnCount == 10) && (WarnToggle == 0) )    // mit "10" etwas unempfindlicher gegen kurze Impulse machen
643
//            WarnToggle = 1;                             // Warnhinweis starten
644
//    }
645
//
646
//    if( WarnCount == (PoffTime * 100) )
647
//    {
648
//        clr_V_On();                                     // Spannung abschalten
649
//  }
650
//}
651
 
652
 
653
 
654
 
655
//--------------------------------------------------------------
656
//--------------------------------------------------------------
657
unsigned int SetDelay( unsigned int t )
658
{
659
    return( CountMilliseconds + t + 1 );
660
}
661
 
662
 
663
 
664
//--------------------------------------------------------------
665
//--------------------------------------------------------------
666
char CheckDelay( unsigned int t )
667
{
668
    return( ((t - CountMilliseconds) & 0x8000) >> 9 );
669
}
670
 
671
 
672
 
673
//--------------------------------------------------------------
674
//--------------------------------------------------------------
675
void Delay_ms( unsigned int w )
676
{
677
    unsigned int akt;
678
    akt = SetDelay(w);
679
    while( !CheckDelay(akt) );
680
}
681
 
682
 
683
 
684
//--------------------------------------------------------------
685
//--------------------------------------------------------------
686
uint8_t get_key_press( uint8_t key_mask )
687
{
688
    uint8_t sreg = SREG;
689
 
690
    cli();                      // disable all interrupts
691
 
692
    key_mask &= key_press;      // read key(s)
693
    key_press ^= key_mask;      // clear key(s)
694
 
695
    SREG = sreg;                // restore status register
696
 
697
    return key_mask;
698
}
699
 
700
 
701
 
702
//--------------------------------------------------------------
703
//--------------------------------------------------------------
704
uint8_t get_key_short( uint8_t key_mask )
705
{
706
    uint8_t ret;
707
    uint8_t sreg = SREG;
708
 
709
    cli();                          // disable all interrupts
710
 
711
    ret = get_key_press (~key_state & key_mask);
712
 
713
    SREG = sreg;                    // restore status register
714
 
715
    return ret;
716
}
717
 
718
 
719
 
720
//--------------------------------------------------------------
721
//--------------------------------------------------------------
722
uint8_t get_key_long( uint8_t key_mask )
723
{
724
    uint8_t sreg = SREG;
725
 
726
    cli();                          // disable all interrupts
727
 
728
    key_mask &= key_long;           // read key(s)
729
    key_long ^= key_mask;           // clear key(s)
730
 
731
    SREG = sreg;                    // restore status register
732
 
733
  return get_key_press (get_key_rpt (key_mask));
734
}
735
 
736
 
737
 
738
//--------------------------------------------------------------
739
//--------------------------------------------------------------
740
uint8_t get_key_rpt( uint8_t key_mask )
741
{
742
    uint8_t sreg = SREG;
743
 
744
    cli();                          // disable all interrupts
745
 
746
    key_mask &= key_rpt;            // read key(s)
747
    key_rpt ^= key_mask;            // clear key(s)
748
 
749
    SREG = sreg;                    // restore status register
750
 
751
    return key_mask;
752
}
753
 
754
 
755
 
756
//--------------------------------------------------------------
757
//--------------------------------------------------------------
758
uint8_t get_key_long_rpt( uint8_t key_mask )
759
{
760
    uint8_t sreg = SREG;
761
 
762
    cli();                          // disable all interrupts
763
 
764
    key_mask &= key_lrpt;           // read key(s)
765
    key_lrpt ^= key_mask;           // clear key(s)
766
 
767
    SREG = sreg;                    // restore status register
768
 
769
    return get_key_rpt (~key_press^key_mask);
770
}
771
 
772
 
773
 
774
//--------------------------------------------------------------
775
//--------------------------------------------------------------
776
uint8_t get_key_long_rpt_sp( uint8_t key_mask, uint8_t key_speed )
777
{
778
    uint8_t sreg = SREG;
779
 
780
    cli();                          // disable all interrupts
781
 
782
    key_mask &= key_rpts;           // read key(s)
783
    key_rpts ^= key_mask;           // clear key(s)
784
 
785
    repeat_speed = key_speed;
786
 
787
    SREG = sreg;                    // restore status register
788
 
789
    return key_mask;
790
}
791
 
792
 
793
 
794
//--------------------------------------------------------------
795
//--------------------------------------------------------------
796
void clear_key_all( void )
797
{
798
    key_rpt   = 0;                  // clear key(s)
799
    key_rpts  = 0;                  // clear key(s)
800
    key_lrpt  = 0;                  // clear key(s)
801
    key_long  = 0;                  // clear key(s)
802
    key_press = 0;                  // clear key(s)
803
}
804
 
805
 
806
 
807
//--------------------------------------------------------------
808
//--------------------------------------------------------------
809
void set_beep( uint16_t Time, uint16_t Muster, uint8_t Prio)
810
{
811
    if( Config.HWBeeper == 1 )
812
    {
813
        if( (Prio == BeepNormal) && (BeepPrio == BeepNormal) )      // BEEPER: nur setzen wenn keine hohe Prio schon aktiv ist
814
        {
815
            BeepTime   = Time;
816
            BeepMuster = Muster;
817
        }
818
 
819
        if( (Prio == BeepSevere) && (!BeepPrio == BeepSevere) )     // BEEPER: hohe Prio setzen
820
        {
821
            BeepPrio   = BeepSevere;
822
            BeepTime   = Time;
823
            BeepMuster = Muster;
824
        }
825
 
826
        if( Prio == BeepOff )
827
        {
828
            BeepPrio   = BeepNormal;                                // BEEPER: Beep hohe Prio aus
829
            BeepTime   = 0;
830
            BeepMuster = 0;
831
        }
832
    } // end: if( Config.HWBeeper==1 )
833
 
834
    #ifdef USE_SOUND
835
    else
836
    {
837
        if( (Prio == BeepNormal) && (BeepPrio == BeepNormal) )      // SOUND: nur setzen wenn keine hohe Prio schon aktiv ist
838
        {
839
            playTone(900,Time/10,0);
840
        }
841
 
842
        if( (Prio == BeepSevere) && (!BeepPrio == BeepSevere) )
843
        {
844
            playTone(1200,Time/10,0);
845
            playTone(1100,Time/10,0);
846
        }
847
 
848
        if( Prio == BeepOff )
849
        {
850
            playTone(0,0,0);
851
        }
852
    }
853
    #endif // end: #ifdef USE_SOUND
854
}
855
 
856