Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
2108 - 1
#include <inttypes.h>
2
#include "output.h"
3
#include "eeprom.h"
4
#include "timer0.h"
5
uint8_t flashCnt[2], flashMask[2];
6
 
7
DebugOut_t debugOut;
8
 
9
void output_init(void) {
10
  // set PC2 & PC3 as output (control of J16 & J17)
11
  DDRB |= (1 << DDB1) | (1 << DDB2);
12
  outputSet(0,0);
13
  outputSet(1,0);
14
  flashCnt[0] = flashCnt[1] = 0;
15
  flashMask[0] = flashMask[1] = 128;
16
}
17
 
18
void outputSet(uint8_t num, uint8_t state) {
19
  if (staticParams.outputFlags & (OUTPUTFLAGS_INVERT_0 << num)) {
20
    if (state) OUTPUT_LOW(num) else OUTPUT_HIGH(num);
21
  } else {
22
    if (state) OUTPUT_HIGH(num) else OUTPUT_LOW(num);
23
  }
24
  if (staticParams.outputFlags & OUTPUTFLAGS_USE_ONBOARD_LEDS) {
25
    if (num) {
26
      if (state) GRN_ON else GRN_OFF;
27
    } else {
28
      if (state) RED_ON else RED_OFF;
29
    }
30
  }
31
}
32
 
33
void flashingLight(uint8_t port, uint8_t timing, uint8_t bitmask, uint8_t manual) {
34
  if (timing > 250 && manual > 230) {
35
    // "timing" is set to "manual (a variable)" and the value is very high --> Set to the value in bitmask bit 7.
36
    outputSet(port, 1);
37
  } else if (timing > 250 && manual < 10) {
38
    // "timing" is set to "manual" (a variable) and the value is very low --> Set to the negated value in bitmask bit 7.
39
    outputSet(port, 0);
40
  } else if (!flashCnt[port]--) {
41
    // rotating mask over bitmask...
42
    flashCnt[port] = timing - 1;
43
    if (flashMask[port] == 1)
44
      flashMask[port] = 128;
45
    else
46
      flashMask[port] >>= 1;
47
    outputSet(port, flashMask[port] & bitmask);
48
  }
49
}
50
 
51
void output_update(void) {
52
  static int8_t delay = 0;
53
  if (!delay--) { // 10 ms intervals
54
    delay = 4;
55
  }
56
  if (staticParams.outputFlags & OUTPUTFLAGS_TEST_ON) {
57
    outputSet(0, 1);
58
    outputSet(1, 1);
59
  } else if (staticParams.outputFlags & OUTPUTFLAGS_TEST_OFF) {
60
    outputSet(0, 0);
61
    outputSet(1, 0);
62
  } else {
63
    if (staticParams.outputFlags & OUTPUTFLAGS_FLASH_0_AT_BEEP && beepModulation != BEEP_MODULATION_NONE) {
64
      flashingLight(0, 25, 0x55, 25);
65
    } else if (staticParams.outputDebugMask) {
66
      outputSet(0, debugOut.digital[0] & staticParams.outputDebugMask);
67
    } else if (!delay) {
68
      flashingLight(0, staticParams.outputFlash[0].timing, staticParams.outputFlash[0].bitmask, dynamicParams.output0Timing);
69
    }
70
    if (staticParams.outputFlags & OUTPUTFLAGS_FLASH_1_AT_BEEP && beepModulation != BEEP_MODULATION_NONE) {
71
      flashingLight(1, 25, 0x55, 25);
72
    } else if (staticParams.outputDebugMask) {
73
      outputSet(1, debugOut.digital[1] & staticParams.outputDebugMask);
74
    } else if (!delay) {
75
      flashingLight(1, staticParams.outputFlash[1].timing, staticParams.outputFlash[1].bitmask, dynamicParams.output1Timing);
76
    }
77
  }
78
}
79
 
80
void beep(uint16_t millis) {
81
  beepTime = millis;
82
}
83
 
84
/*
85
 * Make [numbeeps] beeps.
86
 */
87
void beepNumber(uint8_t numbeeps) {
88
  while(numbeeps--) {
89
    if(isFlying) return; //auf keinen Fall bei laufenden Motoren!
90
    beep(100); // 0.1 second
91
    delay_ms(250); // blocks 250 ms as pause to next beep,
92
    // this will block the flight control loop,
93
    // therefore do not use this function if motors are running
94
  }
95
}
96
 
97
/*
98
 * Beep the R/C alarm signal
99
 */
100
void beepRCAlarm(void) {
101
  if(beepModulation == BEEP_MODULATION_NONE) { // If not already beeping an alarm signal (?)
102
    beepTime = 15000; // 1.5 seconds
103
    beepModulation = BEEP_MODULATION_RCALARM;
104
  }
105
}
106
 
107
/*
108
 * Beep the battery low alarm signal
109
 */
110
void beepBatteryAlarm(void) {
111
  beepModulation = BEEP_MODULATION_BATTERYALARM;
112
  if(!beepTime) {
113
    beepTime = 6000; // 0.6 seconds
114
  }
115
}
116
 
117
/*
118
 * Beep the EEPROM checksum alarm
119
 */
120
void beepEEPROMAlarm(void) {
121
  beepModulation = BEEP_MODULATION_EEPROMALARM;
122
  if(!beepTime) {
123
    beepTime = 6000; // 0.6 seconds
124
  }
125
}