Subversion Repositories FlightCtrl

Rev

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

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 04.2007 Holger Buss
// + Nur für den privaten Gebrauch
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
// + bzgl. der Nutzungsbedingungen aufzunehmen.
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
// + Verkauf von Luftbildaufnahmen, usw.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt werden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
// + Benutzung auf eigene Gefahr
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
// + mit unserer Zustimmung zulässig
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
// + this list of conditions and the following disclaimer.
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
// +     from this software without specific prior written permission.
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
// +     for non-commercial use (directly or indirectly)
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
// +     with our written permission
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
// +     clearly linked as origin
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// +  POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#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);
  outputSet(0,0);
  outputSet(1,0);
  flashCnt[0] = flashCnt[1] = 0;
  flashMask[0] = flashMask[1] = 128;
}

void outputSet(uint8_t num, uint8_t state) {
  if (staticParams.outputFlags & (OUTPUTFLAGS_INVERT_0 << num)) {
    if (state) OUTPUT_LOW(num) else OUTPUT_HIGH(num);
  } else {
    if (state) OUTPUT_HIGH(num) else OUTPUT_LOW(num);
  }
  if (staticParams.outputFlags & OUTPUTFLAGS_USE_ONBOARD_LEDS) {
    if (num) {
      if (state) GRN_ON else GRN_OFF;
    } else {
      if (state) RED_ON else RED_OFF;
    }
  }
}

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

void flashingLights(void) {
  static int8_t delay = 0;
  if (!delay--) { // 10 ms intervals
    delay = 4;
    if (staticParams.outputFlags & OUTPUTFLAGS_FLASH_0_AT_BEEP && beepModulation != BEEP_MODULATION_NONE) {
      // alarm
      flashingLight(0, 25, 0x55, 25);
    } else {
      flashingLight(0, staticParams.outputFlash[0].timing, staticParams.outputFlash[0].bitmask, dynamicParams.output0Timing);
    }
    if (staticParams.outputFlags & OUTPUTFLAGS_FLASH_1_AT_BEEP && beepModulation != BEEP_MODULATION_NONE) {
      // alarm
      flashingLight(1, 25, 0x55, 25);
    } else {
      flashingLight(1, staticParams.outputFlash[1].timing, staticParams.outputFlash[1].bitmask, dynamicParams.output1Timing);
    }
  }
}

void output_update(void) {
  if (staticParams.outputFlags & OUTPUTFLAGS_TEST_ON) {
    outputSet(0, 1);
    outputSet(1, 1);
  } else if (staticParams.outputFlags & OUTPUTFLAGS_TEST_OFF) {
    outputSet(0, 0);
    outputSet(1, 0);
  } else if (!staticParams.outputDebugMask) {
    flashingLights();
  } else {
    outputSet(0, debugOut.digital[0] & staticParams.outputDebugMask);
    outputSet(1, debugOut.digital[1] & staticParams.outputDebugMask);
  }
}

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 == BEEP_MODULATION_NONE) { // If not already beeping an alarm signal (?)
    beepTime = 15000; // 1.5 seconds
    beepModulation = BEEP_MODULATION_RCALARM;
  }
}

/*
 * Beep the I2C bus error signal
 */

void beepI2CAlarm(void) {
  if((beepModulation == BEEP_MODULATION_NONE) && (MKFlags & MKFLAG_MOTOR_RUN)) {
    beepTime = 10000; // 1 second
    beepModulation = BEEP_MODULATION_I2CALARM;
  }
}

/*
 * Beep the battery low alarm signal
 */

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

/*
 * Beep the EEPROM checksum alarm
 */

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