Subversion Repositories FlightCtrl

Rev

Rev 1140 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1134 thjac 1
#include <inttypes.h>
2
#include "main.h"
3
#include "parameter.h"
4
 
5
unsigned char J16Blinkcount = 0, J16Mask = 1, J16Brightness = 0, J16 = 0;
6
unsigned char J17Blinkcount = 0, J17Mask = 1, J17Brightness = 0, J17 = 0;
7
 
8
unsigned char pwmJ16 = 0, pwmJ17 = 0;
9
static unsigned char pwmtable[32] ={0, 1, 2, 2, 2, 3, 3, 4, 5, 6, 7, 8, 10, 11,
10
    13, 16, 19, 23, 27, 32, 38, 45, 54, 64, 76,
11
    91, 108, 128, 152, 181, 215, 250};
12
 
13
unsigned char lightsEnabled = 0, lightsOn = 0;
14
 
15
extern char MotorenEin;
16
 
17
void setJ16(char enabled) {
18
    /*  if( PARAM_LED_NEGATE )
19
                    enabled = !enabled;
20
            if( enabled && forceEnabled )
21
                    J16_ON;
22
            else
23
                    J16_OFF;*/
24
 
25
    if ((enabled && lightsOn) ^ LED_NEGATE_J16)
26
        J16_ON;
27
    else
28
        J16_OFF;
29
}
30
 
31
void setJ17(char enabled) {
32
    /*  if( PARAM_LED_NEGATE )
33
                    enabled = !enabled;
34
            if( enabled && forceEnabled )
35
                    J17_ON;
36
            else
37
                    J17_OFF;*/
38
 
39
    if ((enabled && lightsOn) ^ LED_NEGATE_J17)
40
        J17_ON;
41
    else
42
        J17_OFF;
43
}
44
 
45
// initializes the LED control outputs J16, J17
46
 
47
void LED_Init(void) {
48
    // set PC2 & PC3 as output (control of J16 & J17)
49
    DDRC |= (1 << DDC2) | (1 << DDC3);
50
 
51
    lightsOn = lightsEnabled = 0;
52
 
53
    setJ16(0);
54
    setJ17(0);
55
 
56
    J16Blinkcount = 0;
57
    J16Mask = 128;
58
    J17Blinkcount = 0;
59
    J17Mask = 128;
60
}
61
 
62
void checkLightsEnabled(void) {
63
 
64
    // Mit dem Gier-Stick rechts lassen sich bei stehenden Motoren die LED's ein- und mit links ausschalten
65
    if (!MotorenEin) {
66
        if (PARAM_LED_STICK_ENABLED) {
67
            if (PPM_in[ EE_Parameter.Kanalbelegung[ K_GAS ] ] > 35 - 125
68
                    && PPM_in[ EE_Parameter.Kanalbelegung[ K_GAS ] ] < 125 - 35) {
69
                if (PPM_in[ EE_Parameter.Kanalbelegung[ K_GIER ] ] < -75)
70
                    lightsEnabled = 1;
71
                if (PPM_in[ EE_Parameter.Kanalbelegung[ K_GIER ] ] > 75)
72
                    lightsEnabled = 0;
73
            }
74
        } else
75
            lightsEnabled = 1;
76
    }
77
 
78
    // Die LED's können mit den Motoren ein- ausgeschaltet werden
79
    if (PARAM_LED_ENGINE_ENABLED)
80
        lightsEnabled = MotorenEin;
81
 
82
    lightsOn = lightsEnabled;
83
}
84
 
85
// called in UpdateMotors() every 2ms
86
 
87
void LED_Update(void) {
88
 
89
    static char delay = 0;
90
 
91
    checkLightsEnabled();
92
 
93
    if (!delay--) {
94
 
95
        delay = 9; // 20ms Intervall
96
 
97
        // Soll die Unterspannungswarnung zu einem schnelleren Blinken führen?
98
        // Grenze für Unterspannungswarnung erreicht?
99
        if (PARAM_LED_WARNING_SPEEDUP && UBat < EE_Parameter.UnterspannungsWarnung) {
100
            if (PARAM_LED_FORCE_WARNING_ENABLED) // Erzwingt die Aktivierung der Ausgänge
101
                lightsOn = 1;
102
            delay /= PARAM_LED_WARNING_SPEEDUP + 1;
103
        }
104
 
105
        // J16
106
        if (EE_Parameter.J16Timing > 250 && Parameter_J16Timing > 230)
107
            J16 = EE_Parameter.J16Bitmask & 128;
108
        else if (EE_Parameter.J16Timing > 250 && Parameter_J16Timing < 10)
109
            J16 = !(EE_Parameter.J16Bitmask & 128);
110
        else if (!J16Blinkcount--) {
111
            J16Blinkcount = Parameter_J16Timing - 1;
112
            J16Mask = (J16Mask == 1 ? 0x80 : J16Mask >> 1);
113
 
114
            J16 = EE_Parameter.J16Bitmask & J16Mask;
115
 
116
            if (EE_Parameter.J16Bitmask & J16Mask) {
117
                pwmJ16 = pwmtable[(Parameter_J16Brightness + 6) / 4];
118
                J16Brightness = Parameter_J16Brightness / 23;
119
            } else
120
                pwmJ16 = J16Brightness = 0;
121
        }
122
 
123
        // J17
124
        if (EE_Parameter.J17Timing > 250 && Parameter_J17Timing > 230)
125
            J17 = EE_Parameter.J17Bitmask & 128;
126
        else if (EE_Parameter.J17Timing > 250 && Parameter_J17Timing < 10)
127
            J17 = !(EE_Parameter.J17Bitmask & 128);
128
        else if (!J17Blinkcount--) {
129
            J17Blinkcount = Parameter_J17Timing - 1;
130
            J17Mask = (J17Mask == 1 ? 0x80 : J17Mask >> 1);
131
 
132
            J17 = EE_Parameter.J17Bitmask & J17Mask;
133
 
134
            if (EE_Parameter.J17Bitmask & J17Mask) {
135
                pwmJ17 = pwmtable[(Parameter_J17Brightness + 6) / 4];
136
                J17Brightness = Parameter_J17Brightness / 23;
137
            } else
138
                pwmJ17 = J17Brightness = 0;
139
        }
140
    }
141
 
142
    // delay: 0...9 - BRIGHTNESS/23: 0-Aus...10-Max - Bei Unterspannung volle Leuchtkraft
143
    setJ16(J16 && (delay < J16Brightness || UBat < EE_Parameter.UnterspannungsWarnung));
144
    setJ17(J17 && (delay < J17Brightness || UBat < EE_Parameter.UnterspannungsWarnung));
145
}