Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1612 dongfang 1
#include <inttypes.h>
2
#include "output.h"
3
#include "eeprom.h"
1887 - 4
#include "timer0.h"
1775 - 5
uint8_t flashCnt[2], flashMask[2];
1964 - 6
 
2052 - 7
DebugOut_t debugOut;
1964 - 8
 
1612 dongfang 9
void output_init(void) {
1869 - 10
  // set PC2 & PC3 as output (control of J16 & J17)
11
  DDRC |= (1 << DDC2) | (1 << DDC3);
1964 - 12
  outputSet(0,0);
13
  outputSet(1,0);
1869 - 14
  flashCnt[0] = flashCnt[1] = 0;
15
  flashMask[0] = flashMask[1] = 128;
1612 dongfang 16
}
17
 
1964 - 18
void outputSet(uint8_t num, uint8_t state) {
1986 - 19
  if (staticParams.outputFlags & (OUTPUTFLAGS_INVERT_0 << num)) {
1968 - 20
    if (state) OUTPUT_LOW(num) else OUTPUT_HIGH(num);
21
  } else {
22
    if (state) OUTPUT_HIGH(num) else OUTPUT_LOW(num);
23
  }
1986 - 24
  if (staticParams.outputFlags & OUTPUTFLAGS_USE_ONBOARD_LEDS) {
1968 - 25
    if (num) {
26
      if (state) GRN_ON else GRN_OFF;
27
    } else {
28
      if (state) RED_ON else RED_OFF;
29
    }
30
  }
1964 - 31
}
32
 
33
void flashingLight(uint8_t port, uint8_t timing, uint8_t bitmask, uint8_t manual) {
1869 - 34
  if (timing > 250 && manual > 230) {
1960 - 35
    // "timing" is set to "manual (a variable)" and the value is very high --> Set to the value in bitmask bit 7.
1964 - 36
    outputSet(port, 1);
1869 - 37
  } else if (timing > 250 && manual < 10) {
1960 - 38
    // "timing" is set to "manual" (a variable) and the value is very low --> Set to the negated value in bitmask bit 7.
1964 - 39
    outputSet(port, 0);
1869 - 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;
1964 - 47
    outputSet(port, flashMask[port] & bitmask);
1869 - 48
  }
1775 - 49
}
50
 
2019 - 51
void output_update(void) {
1869 - 52
  static int8_t delay = 0;
53
  if (!delay--) { // 10 ms intervals
54
    delay = 4;
2019 - 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 {
1986 - 63
    if (staticParams.outputFlags & OUTPUTFLAGS_FLASH_0_AT_BEEP && beepModulation != BEEP_MODULATION_NONE) {
1908 - 64
      flashingLight(0, 25, 0x55, 25);
2019 - 65
    } else if (staticParams.outputDebugMask) {
66
      outputSet(0, debugOut.digital[0] & staticParams.outputDebugMask);
67
    } else if (!delay) {
1960 - 68
      flashingLight(0, staticParams.outputFlash[0].timing, staticParams.outputFlash[0].bitmask, dynamicParams.output0Timing);
1908 - 69
    }
1986 - 70
    if (staticParams.outputFlags & OUTPUTFLAGS_FLASH_1_AT_BEEP && beepModulation != BEEP_MODULATION_NONE) {
71
      flashingLight(1, 25, 0x55, 25);
2019 - 72
    } else if (staticParams.outputDebugMask) {
73
      outputSet(1, debugOut.digital[1] & staticParams.outputDebugMask);
74
    } else if (!delay) {
2018 - 75
      flashingLight(1, staticParams.outputFlash[1].timing, staticParams.outputFlash[1].bitmask, dynamicParams.output1Timing);
1986 - 76
    }
1869 - 77
  }
1612 dongfang 78
}
79
 
1887 - 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(MKFlags & MKFLAG_MOTOR_RUN) 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) {
1908 - 101
  if(beepModulation == BEEP_MODULATION_NONE) { // If not already beeping an alarm signal (?)
1887 - 102
    beepTime = 15000; // 1.5 seconds
1908 - 103
    beepModulation = BEEP_MODULATION_RCALARM;
1887 - 104
  }
105
}
106
 
107
/*
108
 * Beep the I2C bus error signal
109
 */
110
void beepI2CAlarm(void) {
1908 - 111
  if((beepModulation == BEEP_MODULATION_NONE) && (MKFlags & MKFLAG_MOTOR_RUN)) {
1887 - 112
    beepTime = 10000; // 1 second
1908 - 113
    beepModulation = BEEP_MODULATION_I2CALARM;
1887 - 114
  }
115
}
116
 
117
/*
118
 * Beep the battery low alarm signal
119
 */
120
void beepBatteryAlarm(void) {
1908 - 121
  beepModulation = BEEP_MODULATION_BATTERYALARM;
1887 - 122
  if(!beepTime) {
123
    beepTime = 6000; // 0.6 seconds
124
  }
125
}
126
 
127
/*
128
 * Beep the EEPROM checksum alarm
129
 */
130
void beepEEPROMAlarm(void) {
1908 - 131
  beepModulation = BEEP_MODULATION_EEPROMALARM;
1887 - 132
  if(!beepTime) {
133
    beepTime = 6000; // 0.6 seconds
134
  }
135
}