Subversion Repositories FlightCtrl

Rev

Rev 1118 | Rev 1131 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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