Subversion Repositories FlightCtrl

Rev

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

#include <inttypes.h>
#include "output.h"
#include "eeprom.h"
#include "timer0.h"

// To access the DebugOut struct.
#include "uart0.h"
uint8_t flashCnt[2], flashMask[2];
// initializes the LED control outputs J16, J17
void output_init(void) {
  // set PC2 & PC3 as output (control of J16 & J17)
  DDRC |= (1 << DDC2) | (1 << DDC3);
  OUTPUT_SET(0,0);
  OUTPUT_SET(1,0);
  flashCnt[0] = flashCnt[1] = 0;
  flashMask[0] = flashMask[1] = 128;
}

void flashingLight(uint8_t port, uint8_t timing, uint8_t bitmask,
    uint8_t manual) {
  if (timing > 250 && manual > 230) {
    // "timing" is set to "manual" and the value is very high --> Set to the value in bitmask bit 7.
    OUTPUT_SET(port, bitmask & 128);
  } else if (timing > 250 && manual < 10) {
    // "timing" is set to "manual" and the value is very low --> Set to the negated value in bitmask bit 7.
    OUTPUT_SET(port, !(bitmask & 128));
  } else if (!flashCnt[port]--) {
    // rotating mask over bitmask...
    flashCnt[port] = timing - 1;
    if (flashMask[port] == 1)
      flashMask[port] = 128;
    else
      flashMask[port] >>= 1;
    OUTPUT_SET(port, flashMask[port] & bitmask);
  }
}

void flashingLights(void) {
  static int8_t delay = 0;
  if (!delay--) { // 10 ms intervals
    delay = 4;
    flashingLight(0, staticParams.J16Timing, staticParams.J16Bitmask,
        dynamicParams.J16Timing);
    flashingLight(1, staticParams.J17Timing, staticParams.J17Bitmask,
        dynamicParams.J17Timing);
  }
}

void output_update(void) {
  if (!DIGITAL_DEBUG_MASK) {
    // If there is a warning beep, also flash it.
    if (beepTime) {
      if (beepTime & beepModulation) {
        OUTPUT_SET(0, 1);
        OUTPUT_SET(1, 1);
      } else {
        OUTPUT_SET(0, 0);
        OUTPUT_SET(1, 0);
      }
    }else
      flashingLights();
  }
  else if (DIGITAL_DEBUG_MASK == DEBUG_LEDTEST_ON) {
    OUTPUT_SET(0, 1);
    OUTPUT_SET(1, 1);
  } else if (DIGITAL_DEBUG_MASK == DEBUG_LEDTEST_OFF) {
    OUTPUT_SET(0, 0);
    OUTPUT_SET(1, 0);
  } else {
    OUTPUT_SET(0, DebugOut.Digital[0] & DIGITAL_DEBUG_MASK);
    OUTPUT_SET(1, DebugOut.Digital[1] & DIGITAL_DEBUG_MASK);
  }
}

void beep(uint16_t millis) {
  beepTime = millis;
}

/*
 * Make [numbeeps] beeps.
 */

void beepNumber(uint8_t numbeeps) {
  while(numbeeps--) {
    if(MKFlags & MKFLAG_MOTOR_RUN) return; //auf keinen Fall bei laufenden Motoren!
    beep(100); // 0.1 second
    delay_ms(250); // blocks 250 ms as pause to next beep,
    // this will block the flight control loop,
    // therefore do not use this function if motors are running
  }
}

/*
 * Beep the R/C alarm signal
 */

void beepRCAlarm(void) {
  if(beepModulation == 0xFFFF) { // If not already beeping an alarm signal (?)
    beepTime = 15000; // 1.5 seconds
    beepModulation = 0x0C00;
  }
}

/*
 * Beep the I2C bus error signal
 */

void beepI2CAlarm(void) {
  if((beepModulation == 0xFFFF) && (MKFlags & MKFLAG_MOTOR_RUN)) {
    beepTime = 10000; // 1 second
    beepModulation = 0x0080;
  }
}

/*
 * Beep the battery low alarm signal
 */

void beepBatteryAlarm(void) {
  beepModulation = 0x0300;
  if(!beepTime) {
    beepTime = 6000; // 0.6 seconds
  }
}

/*
 * Beep the EEPROM checksum alarm
 */

void beepEEPROMAlarm(void) {
  beepModulation = 0x0007;
  if(!beepTime) {
    beepTime = 6000; // 0.6 seconds
  }
}