Subversion Repositories FlightCtrl

Rev

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