2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2014-08-23 04:52:43 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
|
|
|
|
2014-11-15 01:51:21 -04:00
|
|
|
#include "ToneAlarmDriver.h"
|
2014-08-23 04:52:43 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace Linux;
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
static uint16_t notes[] = { 0,
|
|
|
|
NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
|
|
|
|
NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
|
|
|
|
NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
|
|
|
|
NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
|
|
|
|
};
|
|
|
|
|
2014-11-12 02:43:52 -04:00
|
|
|
//List of RTTTL tones
|
2014-11-15 01:51:21 -04:00
|
|
|
const char* ToneAlarm::tune[TONE_NUMBER_OF_TUNES] = {
|
2014-11-12 06:01:21 -04:00
|
|
|
"Startup:d=8,o=6,b=480:a,d7,c7,a,d7,c7,a,d7,16d7,16c7,16d7,16c7,16d7,16c7,16d7,16c7",
|
|
|
|
"Error:d=4,o=6,b=400:8a,8a,8a,p,a,a,a,p",
|
|
|
|
"notify_pos:d=4,o=6,b=400:8e,8e,a",
|
|
|
|
"notify_neut:d=4,o=6,b=400:8e,e",
|
|
|
|
"notify_neg:d=4,o=6,b=400:8e,8c,8e,8c,8e,8c",
|
2014-11-12 02:43:52 -04:00
|
|
|
"arming_warn:d=1,o=4,b=75:g",
|
2014-11-12 06:01:21 -04:00
|
|
|
"batt_war_slow:d=4,o=6,b=200:8a",
|
|
|
|
"batt_war_fast:d=4,o=6,b=512:8a,8a,8a,8a,8a,8a,8a,8a,8a,8a,8a,8a,8a,8a,8a,8a,8a",
|
|
|
|
"GPS_war:d=4,o=6,b=512:a,a,a,1f#",
|
|
|
|
"Arm_fail:d=4,o=4,b=512:b,a,p",
|
|
|
|
"para_rel:d=16,o=6,b=512:a,g,a,g,a,g,a,g"};
|
2014-11-12 02:43:52 -04:00
|
|
|
//Tune Repeat true: play rtttl tune in loop, false: play only once
|
2014-11-15 01:51:21 -04:00
|
|
|
bool ToneAlarm::tune_repeat[TONE_NUMBER_OF_TUNES] = {false,true,false,false,false,false,true,true,false,false,false};
|
2014-11-12 02:43:52 -04:00
|
|
|
|
2014-11-15 01:51:21 -04:00
|
|
|
ToneAlarm::ToneAlarm()
|
2014-08-23 04:52:43 -03:00
|
|
|
{
|
2014-11-12 02:43:52 -04:00
|
|
|
period_fd = open("/sys/devices/ocp.3/pwm_test_P8_36.12/period",O_WRONLY);
|
|
|
|
duty_fd = open("/sys/devices/ocp.3/pwm_test_P8_36.12/duty",O_WRONLY);
|
|
|
|
run_fd = open("/sys/devices/ocp.3/pwm_test_P8_36.12/run",O_WRONLY);
|
2014-08-23 04:52:43 -03:00
|
|
|
|
|
|
|
tune_num = -1; //initialy no tune to play
|
2014-11-12 06:01:21 -04:00
|
|
|
tune_pos = 0;
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
2014-11-15 01:51:21 -04:00
|
|
|
|
|
|
|
bool ToneAlarm::init()
|
2014-08-23 04:52:43 -03:00
|
|
|
{
|
|
|
|
tune_num = 0; //play startup tune
|
|
|
|
if((period_fd == -1) || (duty_fd == -1) || (run_fd == -1)){
|
2014-11-15 01:58:58 -04:00
|
|
|
hal.console->printf("ToneAlarm: Error!! please check if PWM overlays are loaded correctly");
|
2014-11-15 01:51:21 -04:00
|
|
|
return false;
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
2014-11-15 01:51:21 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToneAlarm::set_tune(uint8_t tone)
|
|
|
|
{
|
|
|
|
tune_num = tone;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToneAlarm::is_tune_comp()
|
|
|
|
{
|
|
|
|
return tune_comp;
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
|
|
|
|
2014-11-15 01:51:21 -04:00
|
|
|
void ToneAlarm::stop()
|
2014-08-23 04:52:43 -03:00
|
|
|
{
|
2014-11-15 01:51:21 -04:00
|
|
|
dprintf(run_fd,"0");
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
|
|
|
|
2014-11-15 01:51:21 -04:00
|
|
|
bool ToneAlarm::play()
|
2014-08-23 04:52:43 -03:00
|
|
|
{
|
2014-11-12 06:01:21 -04:00
|
|
|
uint16_t cur_time = hal.scheduler->millis();
|
2014-08-23 04:52:43 -03:00
|
|
|
if(tune_num != prev_tune_num){
|
|
|
|
tune_changed = true;
|
2014-11-12 06:01:21 -04:00
|
|
|
return true;
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
2014-11-12 06:01:21 -04:00
|
|
|
if(cur_note != 0){
|
2014-11-09 15:51:53 -04:00
|
|
|
dprintf(run_fd,"0");
|
2014-11-12 06:01:21 -04:00
|
|
|
dprintf(period_fd,"%u",1000000000/cur_note);
|
|
|
|
dprintf(duty_fd,"%u",500000000/cur_note);
|
2014-11-09 15:51:53 -04:00
|
|
|
dprintf(run_fd,"1");
|
2014-11-12 06:01:21 -04:00
|
|
|
cur_note =0;
|
|
|
|
prev_time = cur_time;
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
2014-11-12 06:01:21 -04:00
|
|
|
if((cur_time - prev_time) > duration){
|
|
|
|
stop();
|
2014-11-15 01:51:21 -04:00
|
|
|
if(tune[tune_num][tune_pos] == '\0'){
|
2014-11-12 06:01:21 -04:00
|
|
|
if(!tune_repeat[tune_num]){
|
|
|
|
tune_num = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tune_pos = 0;
|
2014-11-15 01:51:21 -04:00
|
|
|
tune_comp = true;
|
2014-11-12 06:01:21 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
|
|
|
|
2014-11-15 01:51:21 -04:00
|
|
|
bool ToneAlarm::set_note(){
|
2014-11-12 06:01:21 -04:00
|
|
|
// first, get note duration, if available
|
|
|
|
uint16_t scale,note,num =0;
|
|
|
|
duration = 0;
|
|
|
|
|
|
|
|
while(isdigit(tune[tune_num][tune_pos])){ //this is a safe while loop as it can't go further than
|
|
|
|
//the length of the rtttl tone string
|
|
|
|
num = (num * 10) + (tune[tune_num][tune_pos++] - '0');
|
|
|
|
}
|
|
|
|
if(num){
|
|
|
|
duration = wholenote / num;
|
|
|
|
} else{
|
|
|
|
duration = wholenote / 4; // we will need to check if we are a dotted note after
|
|
|
|
}
|
|
|
|
// now get the note
|
|
|
|
note = 0;
|
|
|
|
|
|
|
|
switch(tune[tune_num][tune_pos]){
|
|
|
|
case 'c':
|
|
|
|
note = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
note = 3;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
note = 5;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
note = 6;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
note = 8;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
note = 10;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
note = 12;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
default:
|
|
|
|
note = 0;
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
tune_pos++;
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
// now, get optional '#' sharp
|
|
|
|
if(tune[tune_num][tune_pos] == '#'){
|
|
|
|
note++;
|
|
|
|
tune_pos++;
|
|
|
|
}
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
// now, get optional '.' dotted note
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
if(tune[tune_num][tune_pos] == '.'){
|
|
|
|
duration += duration/2;
|
|
|
|
tune_pos++;
|
|
|
|
}
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
// now, get scale
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
if(isdigit(tune[tune_num][tune_pos])){
|
|
|
|
scale = tune[tune_num][tune_pos] - '0';
|
|
|
|
tune_pos++;
|
|
|
|
} else{
|
|
|
|
scale = default_oct;
|
|
|
|
}
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
scale += OCTAVE_OFFSET;
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
if(tune[tune_num][tune_pos] == ','){
|
|
|
|
tune_pos++; // skip comma for next note (or we may be at the end)
|
|
|
|
}
|
|
|
|
// now play the note
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
if(note){
|
|
|
|
if(tune_changed == true){
|
|
|
|
tune_pos =0;
|
|
|
|
tune_changed = false;
|
|
|
|
}
|
|
|
|
cur_note = notes[(scale - 4) * 12 + note];
|
|
|
|
return true;
|
|
|
|
} else{
|
|
|
|
cur_note = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-15 01:51:21 -04:00
|
|
|
bool ToneAlarm::init_tune(){
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
uint16_t num;
|
|
|
|
default_dur = 4;
|
|
|
|
default_oct = 6;
|
|
|
|
bpm = 63;
|
|
|
|
prev_tune_num = tune_num;
|
|
|
|
if(tune_num <0 || tune_num > TONE_NUMBER_OF_TUNES){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-15 01:51:21 -04:00
|
|
|
tune_comp = false;
|
2014-11-12 06:01:21 -04:00
|
|
|
while(tune[tune_num][tune_pos] != ':'){
|
2014-11-15 01:51:21 -04:00
|
|
|
if(tune[tune_num][tune_pos] == '\0'){
|
2014-11-12 06:01:21 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tune_pos++;
|
|
|
|
}
|
|
|
|
tune_pos++;
|
|
|
|
|
|
|
|
if(tune[tune_num][tune_pos] == 'd'){
|
|
|
|
tune_pos+=2;
|
|
|
|
num = 0;
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
while(isdigit(tune[tune_num][tune_pos])){
|
|
|
|
num = (num * 10) + (tune[tune_num][tune_pos++] - '0');
|
|
|
|
}
|
|
|
|
if(num > 0){
|
|
|
|
default_dur = num;
|
|
|
|
}
|
|
|
|
tune_pos++; // skip comma
|
|
|
|
}
|
2014-08-23 04:52:43 -03:00
|
|
|
|
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
// get default octave
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
if(tune[tune_num][tune_pos] == 'o')
|
|
|
|
{
|
|
|
|
tune_pos+=2; // skip "o="
|
|
|
|
num = tune[tune_num][tune_pos++] - '0';
|
|
|
|
if(num >= 3 && num <=7){
|
|
|
|
default_oct = num;
|
|
|
|
}
|
|
|
|
tune_pos++; // skip comma
|
|
|
|
}
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
// get BPM
|
2014-08-23 04:52:43 -03:00
|
|
|
|
2014-11-12 06:01:21 -04:00
|
|
|
if(tune[tune_num][tune_pos] == 'b'){
|
|
|
|
tune_pos+=2; // skip "b="
|
|
|
|
num = 0;
|
|
|
|
while(isdigit(tune[tune_num][tune_pos])){
|
|
|
|
num = (num * 10) + (tune[tune_num][tune_pos++] - '0');
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
2014-11-12 06:01:21 -04:00
|
|
|
bpm = num;
|
|
|
|
tune_pos++; // skip colon
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
2014-11-12 06:01:21 -04:00
|
|
|
|
|
|
|
// BPM usually expresses the number of quarter notes per minute
|
|
|
|
wholenote = (60 * 1000L / bpm) * 4; // this is the time for whole note (in milliseconds)
|
|
|
|
return true;
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
|
|
|
|
2014-11-15 01:51:21 -04:00
|
|
|
|
2014-11-09 15:51:53 -04:00
|
|
|
#endif
|