From 329c1f51909254dc1da3c535593d10f452e1a595 Mon Sep 17 00:00:00 2001 From: bugobliterator Date: Sat, 15 Nov 2014 16:51:21 +1100 Subject: [PATCH] HAL_Linux:create ToneAlarmDriver as a separate class ToneAlarm is now declared as a separate class instance of which is added as a private member of LinuxUtil Some minor fixes in this patch include changing return type of tonealarm_init() to bool and use dprintf --- .../AP_HAL_Linux/AP_HAL_Linux_Namespace.h | 1 + libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h | 1 + libraries/AP_HAL_Linux/Scheduler.cpp | 2 +- libraries/AP_HAL_Linux/ToneAlarmDriver.cpp | 61 ++++---- libraries/AP_HAL_Linux/Util.cpp | 30 ++++ libraries/AP_HAL_Linux/Util.h | 138 +----------------- 6 files changed, 66 insertions(+), 167 deletions(-) diff --git a/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h b/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h index 2c79c593db..f9a1c8589a 100644 --- a/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h +++ b/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h @@ -30,6 +30,7 @@ namespace Linux { class LinuxSemaphore; class LinuxScheduler; class LinuxUtil; + class ToneAlarm; //limit the scope of ToneAlarm driver to Linux_HAL only } #endif // __AP_HAL_LINUX_NAMESPACE_H__ diff --git a/libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h b/libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h index ea0a0b66d7..c541cced6d 100644 --- a/libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h +++ b/libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h @@ -19,6 +19,7 @@ #include "RCOutput_ZYNQ.h" #include "Semaphores.h" #include "Scheduler.h" +#include "ToneAlarmDriver.h" #include "Util.h" #endif // __AP_HAL_LINUX_PRIVATE_H__ diff --git a/libraries/AP_HAL_Linux/Scheduler.cpp b/libraries/AP_HAL_Linux/Scheduler.cpp index 425d977ea0..70e0a0910b 100644 --- a/libraries/AP_HAL_Linux/Scheduler.cpp +++ b/libraries/AP_HAL_Linux/Scheduler.cpp @@ -325,7 +325,7 @@ void *LinuxScheduler::_tonealarm_thread(void) poll(NULL, 0, 1); } while (true) { - _microsleep(5000); + _microsleep(10000); // process tone command ((LinuxUtil *)hal.util)->_toneAlarm_timer_tick(); diff --git a/libraries/AP_HAL_Linux/ToneAlarmDriver.cpp b/libraries/AP_HAL_Linux/ToneAlarmDriver.cpp index 5e2d620d38..25cc0591ea 100644 --- a/libraries/AP_HAL_Linux/ToneAlarmDriver.cpp +++ b/libraries/AP_HAL_Linux/ToneAlarmDriver.cpp @@ -2,7 +2,7 @@ #if CONFIG_HAL_BOARD == HAL_BOARD_LINUX -#include "Util.h" +#include "ToneAlarmDriver.h" #include #include #include @@ -14,7 +14,6 @@ using namespace Linux; extern const AP_HAL::HAL& hal; -static int state; 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, @@ -23,7 +22,7 @@ NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_ }; //List of RTTTL tones -const char* LinuxUtil::tune[TONE_NUMBER_OF_TUNES] = { +const char* ToneAlarm::tune[TONE_NUMBER_OF_TUNES] = { "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", @@ -36,9 +35,9 @@ const char* LinuxUtil::tune[TONE_NUMBER_OF_TUNES] = { "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"}; //Tune Repeat true: play rtttl tune in loop, false: play only once -bool LinuxUtil::tune_repeat[TONE_NUMBER_OF_TUNES] = {false,true,false,false,false,false,true,true,false,false,false}; +bool ToneAlarm::tune_repeat[TONE_NUMBER_OF_TUNES] = {false,true,false,false,false,false,true,true,false,false,false}; -void LinuxUtil::toneAlarm() +ToneAlarm::ToneAlarm() { 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); @@ -47,22 +46,33 @@ void LinuxUtil::toneAlarm() tune_num = -1; //initialy no tune to play tune_pos = 0; } -int8_t LinuxUtil::toneAlarm_init() + +bool ToneAlarm::init() { tune_num = 0; //play startup tune if((period_fd == -1) || (duty_fd == -1) || (run_fd == -1)){ hal.console->printf("ToneAlarm: Error!! please check if PWM overlays are loaded correctly"); - return -1; + return false; } - return 0; + return true; +} + +void ToneAlarm::set_tune(uint8_t tone) +{ + tune_num = tone; +} + +bool ToneAlarm::is_tune_comp() +{ + return tune_comp; } -void LinuxUtil::stop() +void ToneAlarm::stop() { - write(run_fd,"0",sizeof(char)); + dprintf(run_fd,"0"); } -bool LinuxUtil::play() +bool ToneAlarm::play() { uint16_t cur_time = hal.scheduler->millis(); if(tune_num != prev_tune_num){ @@ -79,13 +89,13 @@ bool LinuxUtil::play() } if((cur_time - prev_time) > duration){ stop(); - if(tune[tune_num][tune_pos] == NULL){ + if(tune[tune_num][tune_pos] == '\0'){ if(!tune_repeat[tune_num]){ tune_num = -1; } tune_pos = 0; - state = 0; + tune_comp = true; return false; } return true; @@ -93,11 +103,7 @@ bool LinuxUtil::play() return false; } -void LinuxUtil::toneAlarm_set_tune(uint8_t tone) -{ - tune_num = tone; -} -bool LinuxUtil::set_note(){ +bool ToneAlarm::set_note(){ // first, get note duration, if available uint16_t scale,note,num =0; duration = 0; @@ -186,7 +192,7 @@ bool LinuxUtil::set_note(){ } -bool LinuxUtil::init_tune(){ +bool ToneAlarm::init_tune(){ uint16_t num; default_dur = 4; @@ -197,8 +203,9 @@ bool LinuxUtil::init_tune(){ return false; } + tune_comp = false; while(tune[tune_num][tune_pos] != ':'){ - if(tune[tune_num][tune_pos] == NULL){ + if(tune[tune_num][tune_pos] == '\0'){ return false; } tune_pos++; @@ -248,17 +255,5 @@ bool LinuxUtil::init_tune(){ return true; } -void LinuxUtil::_toneAlarm_timer_tick(){ - if(state == 0){ - state = state + init_tune(); - }else if(state == 1){ - state = state + set_note(); - } - if(state == 2){ - state = state + play(); - }else if(state == 3){ - state = 1; - } - -} + #endif diff --git a/libraries/AP_HAL_Linux/Util.cpp b/libraries/AP_HAL_Linux/Util.cpp index c0b075f351..e47cd4cab5 100644 --- a/libraries/AP_HAL_Linux/Util.cpp +++ b/libraries/AP_HAL_Linux/Util.cpp @@ -11,6 +11,9 @@ extern const AP_HAL::HAL& hal; #include "Util.h" using namespace Linux; + +static int state; +ToneAlarm LinuxUtil::_toneAlarm; /** return commandline arguments, if available */ @@ -20,4 +23,31 @@ void LinuxUtil::commandline_arguments(uint8_t &argc, char * const *&argv) argv = saved_argv; } +bool LinuxUtil::toneAlarm_init() +{ + return _toneAlarm.init(); +} + +void LinuxUtil::toneAlarm_set_tune(uint8_t tone) +{ + _toneAlarm.set_tune(tone); +} + +void LinuxUtil::_toneAlarm_timer_tick(){ + if(state == 0){ + state = state + _toneAlarm.init_tune(); + }else if(state == 1){ + state = state + _toneAlarm.set_note(); + } + if(state == 2){ + state = state + _toneAlarm.play(); + }else if(state == 3){ + state = 1; + } + + if(_toneAlarm.is_tune_comp()){ + state = 0; + } + +} #endif // CONFIG_HAL_BOARD == HAL_BOARD_LINUX diff --git a/libraries/AP_HAL_Linux/Util.h b/libraries/AP_HAL_Linux/Util.h index 74bd9546f6..66ad1ce6bc 100644 --- a/libraries/AP_HAL_Linux/Util.h +++ b/libraries/AP_HAL_Linux/Util.h @@ -4,119 +4,13 @@ #include #include "AP_HAL_Linux_Namespace.h" - -#define OCTAVE_OFFSET 0 - -#define NOTE_B0 31 -#define NOTE_C1 33 -#define NOTE_CS1 35 -#define NOTE_D1 37 -#define NOTE_DS1 39 -#define NOTE_E1 41 -#define NOTE_F1 44 -#define NOTE_FS1 46 -#define NOTE_G1 49 -#define NOTE_GS1 52 -#define NOTE_A1 55 -#define NOTE_AS1 58 -#define NOTE_B1 62 -#define NOTE_C2 65 -#define NOTE_CS2 69 -#define NOTE_D2 73 -#define NOTE_DS2 78 -#define NOTE_E2 82 -#define NOTE_F2 87 -#define NOTE_FS2 93 -#define NOTE_G2 98 -#define NOTE_GS2 104 -#define NOTE_A2 110 -#define NOTE_AS2 117 -#define NOTE_B2 123 -#define NOTE_C3 131 -#define NOTE_CS3 139 -#define NOTE_D3 147 -#define NOTE_DS3 156 -#define NOTE_E3 165 -#define NOTE_F3 175 -#define NOTE_FS3 185 -#define NOTE_G3 196 -#define NOTE_GS3 208 -#define NOTE_A3 220 -#define NOTE_AS3 233 -#define NOTE_B3 247 -#define NOTE_C4 262 -#define NOTE_CS4 277 -#define NOTE_D4 294 -#define NOTE_DS4 311 -#define NOTE_E4 330 -#define NOTE_F4 349 -#define NOTE_FS4 370 -#define NOTE_G4 392 -#define NOTE_GS4 415 -#define NOTE_A4 440 -#define NOTE_AS4 466 -#define NOTE_B4 494 -#define NOTE_C5 523 -#define NOTE_CS5 554 -#define NOTE_D5 587 -#define NOTE_DS5 622 -#define NOTE_E5 659 -#define NOTE_F5 698 -#define NOTE_FS5 740 -#define NOTE_G5 784 -#define NOTE_GS5 831 -#define NOTE_A5 880 -#define NOTE_AS5 932 -#define NOTE_B5 988 -#define NOTE_C6 1047 -#define NOTE_CS6 1109 -#define NOTE_D6 1175 -#define NOTE_DS6 1245 -#define NOTE_E6 1319 -#define NOTE_F6 1397 -#define NOTE_FS6 1480 -#define NOTE_G6 1568 -#define NOTE_GS6 1661 -#define NOTE_A6 1760 -#define NOTE_AS6 1865 -#define NOTE_B6 1976 -#define NOTE_C7 2093 -#define NOTE_CS7 2217 -#define NOTE_D7 2349 -#define NOTE_DS7 2489 -#define NOTE_E7 2637 -#define NOTE_F7 2794 -#define NOTE_FS7 2960 -#define NOTE_G7 3136 -#define NOTE_GS7 3322 -#define NOTE_A7 3520 -#define NOTE_AS7 3729 -#define NOTE_B7 3951 -#define NOTE_C8 4186 -#define NOTE_CS8 4435 -#define NOTE_D8 4699 -#define NOTE_DS8 4978 - -#define TONE_STARTUP_TUNE 0 -#define TONE_ERROR_TUNE 1 -#define TONE_NOTIFY_POSITIVE_TUNE 2 -#define TONE_NOTIFY_NEUTRAL_TUNE 3 -#define TONE_NOTIFY_NEGATIVE_TUNE 4 -#define TONE_ARMING_WARNING_TUNE 5 -#define TONE_BATTERY_WARNING_SLOW_TUNE 6 -#define TONE_BATTERY_WARNING_FAST_TUNE 7 -#define TONE_GPS_WARNING_TUNE 8 -#define TONE_ARMING_FAILURE_TUNE 9 -#define TONE_PARACHUTE_RELEASE_TUNE 10 - -#define TONE_NUMBER_OF_TUNES 11 +#include "ToneAlarmDriver.h" class Linux::LinuxUtil : public AP_HAL::Util { public: void init(int argc, char * const *argv) { saved_argc = argc; saved_argv = argv; - toneAlarm(); } @@ -127,35 +21,13 @@ public: */ void commandline_arguments(uint8_t &argc, char * const *&argv); - int8_t toneAlarm_init(); + bool toneAlarm_init(); void toneAlarm_set_tune(uint8_t tune); void _toneAlarm_timer_tick(); - - private: - void toneAlarm(void); - void stop(); - bool play(); - void play_tune(); - bool set_note(); - bool init_tune(); - static const char *tune[TONE_NUMBER_OF_TUNES]; - static bool tune_repeat[TONE_NUMBER_OF_TUNES]; - bool tune_changed; - uint8_t default_oct; - uint8_t default_dur; - uint16_t bpm; - uint16_t wholenote; - uint16_t cur_note; - uint16_t duration; - int32_t prev_tune_num; - uint32_t prev_time; - int32_t period_fd; - int32_t duty_fd; - int32_t run_fd; - int8_t tune_num; - uint8_t tune_pos; - + +private: + static Linux::ToneAlarm _toneAlarm; int saved_argc; char* const *saved_argv; };