Ram Gandikota
/
IOTMetronome
FRDM K64F Metronome
main.cpp@0:dbad57390bd1, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:37:05 2017 +0000
- Revision:
- 0:dbad57390bd1
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:dbad57390bd1 | 1 | #include "mbed.h" |
ram54288 | 0:dbad57390bd1 | 2 | #include "utils.hpp" |
ram54288 | 0:dbad57390bd1 | 3 | #include "stdio.h" |
ram54288 | 0:dbad57390bd1 | 4 | |
ram54288 | 0:dbad57390bd1 | 5 | #include "EthernetInterface.h" |
ram54288 | 0:dbad57390bd1 | 6 | #include "frdm_client.hpp" |
ram54288 | 0:dbad57390bd1 | 7 | |
ram54288 | 0:dbad57390bd1 | 8 | #include "metronome.hpp" |
ram54288 | 0:dbad57390bd1 | 9 | |
ram54288 | 0:dbad57390bd1 | 10 | |
ram54288 | 0:dbad57390bd1 | 11 | |
ram54288 | 0:dbad57390bd1 | 12 | #define IOT_ENABLED |
ram54288 | 0:dbad57390bd1 | 13 | |
ram54288 | 0:dbad57390bd1 | 14 | namespace active_low |
ram54288 | 0:dbad57390bd1 | 15 | { |
ram54288 | 0:dbad57390bd1 | 16 | const bool on = false; |
ram54288 | 0:dbad57390bd1 | 17 | const bool off = true; |
ram54288 | 0:dbad57390bd1 | 18 | } |
ram54288 | 0:dbad57390bd1 | 19 | |
ram54288 | 0:dbad57390bd1 | 20 | RawSerial pc(USBTX, USBRX); |
ram54288 | 0:dbad57390bd1 | 21 | |
ram54288 | 0:dbad57390bd1 | 22 | DigitalOut g_led_red(LED1); |
ram54288 | 0:dbad57390bd1 | 23 | DigitalOut g_led_green(LED2); |
ram54288 | 0:dbad57390bd1 | 24 | DigitalOut g_led_blue(LED3); |
ram54288 | 0:dbad57390bd1 | 25 | |
ram54288 | 0:dbad57390bd1 | 26 | InterruptIn g_button_mode(SW3); |
ram54288 | 0:dbad57390bd1 | 27 | InterruptIn g_button_tap(SW2); |
ram54288 | 0:dbad57390bd1 | 28 | |
ram54288 | 0:dbad57390bd1 | 29 | size_t prev_bpm =0,max_bpm=0, min_bpm=INT_MAX; |
ram54288 | 0:dbad57390bd1 | 30 | bool min_changed = 0, max_changed =0, bpm_changed =0; |
ram54288 | 0:dbad57390bd1 | 31 | uint8_t* buffIn = NULL; |
ram54288 | 0:dbad57390bd1 | 32 | uint32_t sizeIn; |
ram54288 | 0:dbad57390bd1 | 33 | |
ram54288 | 0:dbad57390bd1 | 34 | Semaphore updates(0); |
ram54288 | 0:dbad57390bd1 | 35 | |
ram54288 | 0:dbad57390bd1 | 36 | Ticker status_ticker; |
ram54288 | 0:dbad57390bd1 | 37 | void blinky(){ |
ram54288 | 0:dbad57390bd1 | 38 | |
ram54288 | 0:dbad57390bd1 | 39 | g_led_green = !g_led_green; |
ram54288 | 0:dbad57390bd1 | 40 | |
ram54288 | 0:dbad57390bd1 | 41 | } |
ram54288 | 0:dbad57390bd1 | 42 | metronome metronome_object; |
ram54288 | 0:dbad57390bd1 | 43 | |
ram54288 | 0:dbad57390bd1 | 44 | |
ram54288 | 0:dbad57390bd1 | 45 | void metronome::start_timing(){ |
ram54288 | 0:dbad57390bd1 | 46 | m_timing = true; |
ram54288 | 0:dbad57390bd1 | 47 | m_beat_count = 0; |
ram54288 | 0:dbad57390bd1 | 48 | m_timer.start(); |
ram54288 | 0:dbad57390bd1 | 49 | |
ram54288 | 0:dbad57390bd1 | 50 | } |
ram54288 | 0:dbad57390bd1 | 51 | |
ram54288 | 0:dbad57390bd1 | 52 | void metronome::stop_timing(){ |
ram54288 | 0:dbad57390bd1 | 53 | |
ram54288 | 0:dbad57390bd1 | 54 | m_timing = false; |
ram54288 | 0:dbad57390bd1 | 55 | m_timer.stop(); |
ram54288 | 0:dbad57390bd1 | 56 | size_t curr_bpm=0; |
ram54288 | 0:dbad57390bd1 | 57 | curr_bpm = metronome_object.get_bpm(); |
ram54288 | 0:dbad57390bd1 | 58 | pc.printf("BPM:- %d",curr_bpm); |
ram54288 | 0:dbad57390bd1 | 59 | bpm_changed = 1; |
ram54288 | 0:dbad57390bd1 | 60 | if(curr_bpm < min_bpm){ |
ram54288 | 0:dbad57390bd1 | 61 | min_bpm = curr_bpm; |
ram54288 | 0:dbad57390bd1 | 62 | min_changed =1; |
ram54288 | 0:dbad57390bd1 | 63 | } |
ram54288 | 0:dbad57390bd1 | 64 | if(curr_bpm > max_bpm){ |
ram54288 | 0:dbad57390bd1 | 65 | max_bpm = curr_bpm; |
ram54288 | 0:dbad57390bd1 | 66 | max_changed = 1; |
ram54288 | 0:dbad57390bd1 | 67 | } |
ram54288 | 0:dbad57390bd1 | 68 | if (curr_bpm == 0){ |
ram54288 | 0:dbad57390bd1 | 69 | g_led_green = active_low::off; |
ram54288 | 0:dbad57390bd1 | 70 | }else{ |
ram54288 | 0:dbad57390bd1 | 71 | //pc.printf("Blinky Call"); |
ram54288 | 0:dbad57390bd1 | 72 | size_t tempo = 60/curr_bpm; |
ram54288 | 0:dbad57390bd1 | 73 | status_ticker.attach(blinky,tempo); |
ram54288 | 0:dbad57390bd1 | 74 | } |
ram54288 | 0:dbad57390bd1 | 75 | } |
ram54288 | 0:dbad57390bd1 | 76 | void metronome::tap(){ |
ram54288 | 0:dbad57390bd1 | 77 | |
ram54288 | 0:dbad57390bd1 | 78 | m_beats[m_beat_count++] = m_timer.read_us(); |
ram54288 | 0:dbad57390bd1 | 79 | pc.printf("%d\n",m_timer.read_us()); |
ram54288 | 0:dbad57390bd1 | 80 | |
ram54288 | 0:dbad57390bd1 | 81 | |
ram54288 | 0:dbad57390bd1 | 82 | } |
ram54288 | 0:dbad57390bd1 | 83 | size_t metronome::get_bpm() const { |
ram54288 | 0:dbad57390bd1 | 84 | int i=0; |
ram54288 | 0:dbad57390bd1 | 85 | size_t bpm=0; |
ram54288 | 0:dbad57390bd1 | 86 | if(m_beat_count < 4){ |
ram54288 | 0:dbad57390bd1 | 87 | return prev_bpm; |
ram54288 | 0:dbad57390bd1 | 88 | }else{ |
ram54288 | 0:dbad57390bd1 | 89 | for(i=0;i<m_beat_count-1;i++){ |
ram54288 | 0:dbad57390bd1 | 90 | bpm = bpm + m_beats[i+1]-m_beats[i]; |
ram54288 | 0:dbad57390bd1 | 91 | } |
ram54288 | 0:dbad57390bd1 | 92 | bpm = (size_t) bpm/(m_beat_count-1); |
ram54288 | 0:dbad57390bd1 | 93 | prev_bpm = (bpm * 0.00006); |
ram54288 | 0:dbad57390bd1 | 94 | pc.printf("BPM Calc:- %d",prev_bpm); |
ram54288 | 0:dbad57390bd1 | 95 | return prev_bpm; |
ram54288 | 0:dbad57390bd1 | 96 | } |
ram54288 | 0:dbad57390bd1 | 97 | } |
ram54288 | 0:dbad57390bd1 | 98 | |
ram54288 | 0:dbad57390bd1 | 99 | void on_mode() |
ram54288 | 0:dbad57390bd1 | 100 | { |
ram54288 | 0:dbad57390bd1 | 101 | |
ram54288 | 0:dbad57390bd1 | 102 | if(metronome_object.is_timing()){ |
ram54288 | 0:dbad57390bd1 | 103 | //Go to play mode |
ram54288 | 0:dbad57390bd1 | 104 | //printf("playmode"); |
ram54288 | 0:dbad57390bd1 | 105 | g_led_red = active_low::off; |
ram54288 | 0:dbad57390bd1 | 106 | g_led_green = active_low::on; |
ram54288 | 0:dbad57390bd1 | 107 | metronome_object.stop_timing(); |
ram54288 | 0:dbad57390bd1 | 108 | |
ram54288 | 0:dbad57390bd1 | 109 | }else{ |
ram54288 | 0:dbad57390bd1 | 110 | //Go to learn mode |
ram54288 | 0:dbad57390bd1 | 111 | // printf("learnmode"); |
ram54288 | 0:dbad57390bd1 | 112 | g_led_green = active_low::off; |
ram54288 | 0:dbad57390bd1 | 113 | g_led_red = active_low::on; |
ram54288 | 0:dbad57390bd1 | 114 | status_ticker.detach(); |
ram54288 | 0:dbad57390bd1 | 115 | metronome_object.start_timing(); |
ram54288 | 0:dbad57390bd1 | 116 | |
ram54288 | 0:dbad57390bd1 | 117 | } |
ram54288 | 0:dbad57390bd1 | 118 | } |
ram54288 | 0:dbad57390bd1 | 119 | |
ram54288 | 0:dbad57390bd1 | 120 | void on_tap() |
ram54288 | 0:dbad57390bd1 | 121 | { |
ram54288 | 0:dbad57390bd1 | 122 | // Receive a tempo tap |
ram54288 | 0:dbad57390bd1 | 123 | if(metronome_object.is_timing()){ |
ram54288 | 0:dbad57390bd1 | 124 | metronome_object.tap(); |
ram54288 | 0:dbad57390bd1 | 125 | } |
ram54288 | 0:dbad57390bd1 | 126 | } |
ram54288 | 0:dbad57390bd1 | 127 | |
ram54288 | 0:dbad57390bd1 | 128 | |
ram54288 | 0:dbad57390bd1 | 129 | /*Frequency Class and related resources*/ |
ram54288 | 0:dbad57390bd1 | 130 | class FreqResource { |
ram54288 | 0:dbad57390bd1 | 131 | public: |
ram54288 | 0:dbad57390bd1 | 132 | FreqResource() { |
ram54288 | 0:dbad57390bd1 | 133 | |
ram54288 | 0:dbad57390bd1 | 134 | // M2MObject* freq_object; |
ram54288 | 0:dbad57390bd1 | 135 | freq_object = M2MInterfaceFactory::create_object("3318"); |
ram54288 | 0:dbad57390bd1 | 136 | M2MObjectInstance* freq_inst = freq_object->create_object_instance(); |
ram54288 | 0:dbad57390bd1 | 137 | |
ram54288 | 0:dbad57390bd1 | 138 | |
ram54288 | 0:dbad57390bd1 | 139 | // Why is it blinking only 4 time? - Ram |
ram54288 | 0:dbad57390bd1 | 140 | // there's not really an execute LWM2M ID that matches... hmm... |
ram54288 | 0:dbad57390bd1 | 141 | M2MResource* setValue_res = freq_inst->create_dynamic_resource("5900", "Set Value", |
ram54288 | 0:dbad57390bd1 | 142 | M2MResourceInstance::FLOAT, true); |
ram54288 | 0:dbad57390bd1 | 143 | // we allow executing a function here... |
ram54288 | 0:dbad57390bd1 | 144 | setValue_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
ram54288 | 0:dbad57390bd1 | 145 | //setValue_res->set_value_updated_function(value_updated_callback2(this, &FreqResource::handle_put_bpm)); |
ram54288 | 0:dbad57390bd1 | 146 | setValue_res->set_execute_function(execute_callback(this, &FreqResource::handle_put_bpm)); |
ram54288 | 0:dbad57390bd1 | 147 | // Completion of execute function can take a time, that's why delayed response is used |
ram54288 | 0:dbad57390bd1 | 148 | setValue_res->set_delayed_response(true); |
ram54288 | 0:dbad57390bd1 | 149 | |
ram54288 | 0:dbad57390bd1 | 150 | //Max BPM |
ram54288 | 0:dbad57390bd1 | 151 | M2MResource* maxBPM_res = freq_inst->create_dynamic_resource("5602", "Max BPM", |
ram54288 | 0:dbad57390bd1 | 152 | M2MResourceInstance::FLOAT, true); |
ram54288 | 0:dbad57390bd1 | 153 | |
ram54288 | 0:dbad57390bd1 | 154 | // we allow executing a function here... |
ram54288 | 0:dbad57390bd1 | 155 | maxBPM_res->set_operation(M2MBase::GET_ALLOWED); |
ram54288 | 0:dbad57390bd1 | 156 | |
ram54288 | 0:dbad57390bd1 | 157 | // Completion of execute function can take a time, that's why delayed response is used |
ram54288 | 0:dbad57390bd1 | 158 | // maxBPM_res->set_delayed_response(true); |
ram54288 | 0:dbad57390bd1 | 159 | |
ram54288 | 0:dbad57390bd1 | 160 | //Min BPM |
ram54288 | 0:dbad57390bd1 | 161 | M2MResource* minBPM_res = freq_inst->create_dynamic_resource("5601", "Min BPM", |
ram54288 | 0:dbad57390bd1 | 162 | M2MResourceInstance::FLOAT, true); |
ram54288 | 0:dbad57390bd1 | 163 | // we allow executing a function here... |
ram54288 | 0:dbad57390bd1 | 164 | minBPM_res->set_operation(M2MBase::GET_ALLOWED); |
ram54288 | 0:dbad57390bd1 | 165 | |
ram54288 | 0:dbad57390bd1 | 166 | // Completion of execute function can take a time, that's why delayed response is used |
ram54288 | 0:dbad57390bd1 | 167 | //minBPM_res->set_delayed_response(true); |
ram54288 | 0:dbad57390bd1 | 168 | |
ram54288 | 0:dbad57390bd1 | 169 | //Reset Min/Max |
ram54288 | 0:dbad57390bd1 | 170 | M2MResource* resetBPM_res = freq_inst->create_dynamic_resource("5605", "Reset Min/Max BPM", |
ram54288 | 0:dbad57390bd1 | 171 | M2MResourceInstance::STRING, true); |
ram54288 | 0:dbad57390bd1 | 172 | // we allow executing a function here... |
ram54288 | 0:dbad57390bd1 | 173 | resetBPM_res->set_operation(M2MBase::POST_ALLOWED); |
ram54288 | 0:dbad57390bd1 | 174 | resetBPM_res->set_execute_function(execute_callback(this, &FreqResource::handle_reset_minMax_bpm)); |
ram54288 | 0:dbad57390bd1 | 175 | // Completion of execute function can take a time, that's why delayed response is used |
ram54288 | 0:dbad57390bd1 | 176 | resetBPM_res->set_delayed_response(true); |
ram54288 | 0:dbad57390bd1 | 177 | |
ram54288 | 0:dbad57390bd1 | 178 | //Units |
ram54288 | 0:dbad57390bd1 | 179 | M2MResource* unitsBPM_res = freq_inst->create_dynamic_resource("5701", "Units", |
ram54288 | 0:dbad57390bd1 | 180 | M2MResourceInstance::STRING, true); |
ram54288 | 0:dbad57390bd1 | 181 | // we allow executing a function here... |
ram54288 | 0:dbad57390bd1 | 182 | unitsBPM_res->set_operation(M2MBase::GET_ALLOWED); |
ram54288 | 0:dbad57390bd1 | 183 | // Completion of execute function can take a time, that's why delayed response is used |
ram54288 | 0:dbad57390bd1 | 184 | //unitsBPM_res->set_delayed_response(true); |
ram54288 | 0:dbad57390bd1 | 185 | unitsBPM_res->set_value((uint8_t*)"BPM",3); |
ram54288 | 0:dbad57390bd1 | 186 | } |
ram54288 | 0:dbad57390bd1 | 187 | |
ram54288 | 0:dbad57390bd1 | 188 | |
ram54288 | 0:dbad57390bd1 | 189 | ~FreqResource() { |
ram54288 | 0:dbad57390bd1 | 190 | |
ram54288 | 0:dbad57390bd1 | 191 | } |
ram54288 | 0:dbad57390bd1 | 192 | |
ram54288 | 0:dbad57390bd1 | 193 | void handle_put_bpm(void *argument){ |
ram54288 | 0:dbad57390bd1 | 194 | |
ram54288 | 0:dbad57390bd1 | 195 | status_ticker.detach(); |
ram54288 | 0:dbad57390bd1 | 196 | M2MObjectInstance* inst = freq_object->object_instance(); |
ram54288 | 0:dbad57390bd1 | 197 | M2MResource* setValue_res = inst->resource("5900"); |
ram54288 | 0:dbad57390bd1 | 198 | |
ram54288 | 0:dbad57390bd1 | 199 | // values in mbed Client are all buffers, and we need a vector of int's |
ram54288 | 0:dbad57390bd1 | 200 | setValue_res->get_value(buffIn, sizeIn); |
ram54288 | 0:dbad57390bd1 | 201 | |
ram54288 | 0:dbad57390bd1 | 202 | int temp=0; |
ram54288 | 0:dbad57390bd1 | 203 | for(int i=0;i<sizeIn;i++){ |
ram54288 | 0:dbad57390bd1 | 204 | temp=temp*10+(int)buffIn[i]-48; |
ram54288 | 0:dbad57390bd1 | 205 | } |
ram54288 | 0:dbad57390bd1 | 206 | prev_bpm=temp; |
ram54288 | 0:dbad57390bd1 | 207 | |
ram54288 | 0:dbad57390bd1 | 208 | if(prev_bpm < min_bpm) |
ram54288 | 0:dbad57390bd1 | 209 | { |
ram54288 | 0:dbad57390bd1 | 210 | min_bpm = prev_bpm; |
ram54288 | 0:dbad57390bd1 | 211 | min_changed =1; |
ram54288 | 0:dbad57390bd1 | 212 | } |
ram54288 | 0:dbad57390bd1 | 213 | if(prev_bpm > max_bpm) |
ram54288 | 0:dbad57390bd1 | 214 | { |
ram54288 | 0:dbad57390bd1 | 215 | max_bpm = prev_bpm; |
ram54288 | 0:dbad57390bd1 | 216 | max_changed = 1; |
ram54288 | 0:dbad57390bd1 | 217 | } |
ram54288 | 0:dbad57390bd1 | 218 | size_t tempo = 60/prev_bpm; |
ram54288 | 0:dbad57390bd1 | 219 | status_ticker.attach(blinky,tempo); |
ram54288 | 0:dbad57390bd1 | 220 | |
ram54288 | 0:dbad57390bd1 | 221 | bpm_changed = 1; |
ram54288 | 0:dbad57390bd1 | 222 | } |
ram54288 | 0:dbad57390bd1 | 223 | |
ram54288 | 0:dbad57390bd1 | 224 | void handle_set_bpm(){ |
ram54288 | 0:dbad57390bd1 | 225 | |
ram54288 | 0:dbad57390bd1 | 226 | M2MObjectInstance* inst = freq_object->object_instance(); |
ram54288 | 0:dbad57390bd1 | 227 | M2MResource* setValue_res = inst->resource("5900"); |
ram54288 | 0:dbad57390bd1 | 228 | |
ram54288 | 0:dbad57390bd1 | 229 | char buffer[20]; |
ram54288 | 0:dbad57390bd1 | 230 | int size = sprintf(buffer,"%d",prev_bpm); |
ram54288 | 0:dbad57390bd1 | 231 | setValue_res->set_value((uint8_t*)buffer, size); |
ram54288 | 0:dbad57390bd1 | 232 | |
ram54288 | 0:dbad57390bd1 | 233 | } |
ram54288 | 0:dbad57390bd1 | 234 | |
ram54288 | 0:dbad57390bd1 | 235 | void handle_set_min_bpm(){ |
ram54288 | 0:dbad57390bd1 | 236 | //Send a notification |
ram54288 | 0:dbad57390bd1 | 237 | M2MObjectInstance* inst = freq_object->object_instance(); |
ram54288 | 0:dbad57390bd1 | 238 | M2MResource* minBPM_res = inst->resource("5601"); |
ram54288 | 0:dbad57390bd1 | 239 | pc.printf("%d",min_bpm); |
ram54288 | 0:dbad57390bd1 | 240 | char buffer[20]; |
ram54288 | 0:dbad57390bd1 | 241 | int size = sprintf(buffer,"%d",min_bpm); |
ram54288 | 0:dbad57390bd1 | 242 | minBPM_res->set_value((uint8_t*)buffer, size); |
ram54288 | 0:dbad57390bd1 | 243 | |
ram54288 | 0:dbad57390bd1 | 244 | |
ram54288 | 0:dbad57390bd1 | 245 | } |
ram54288 | 0:dbad57390bd1 | 246 | void handle_set_max_bpm(){ |
ram54288 | 0:dbad57390bd1 | 247 | //Send a notification |
ram54288 | 0:dbad57390bd1 | 248 | M2MObjectInstance* inst = freq_object->object_instance(); |
ram54288 | 0:dbad57390bd1 | 249 | M2MResource* maxBPM_res = inst->resource("5602"); |
ram54288 | 0:dbad57390bd1 | 250 | |
ram54288 | 0:dbad57390bd1 | 251 | char buffer[20]; |
ram54288 | 0:dbad57390bd1 | 252 | int size = sprintf(buffer,"%d",max_bpm); |
ram54288 | 0:dbad57390bd1 | 253 | maxBPM_res->set_value((uint8_t*)buffer, size); |
ram54288 | 0:dbad57390bd1 | 254 | |
ram54288 | 0:dbad57390bd1 | 255 | |
ram54288 | 0:dbad57390bd1 | 256 | } |
ram54288 | 0:dbad57390bd1 | 257 | |
ram54288 | 0:dbad57390bd1 | 258 | void handle_reset_minMax_bpm(void *argument){ |
ram54288 | 0:dbad57390bd1 | 259 | //Handle request to reset the min_max values to zero and 1000000 each |
ram54288 | 0:dbad57390bd1 | 260 | |
ram54288 | 0:dbad57390bd1 | 261 | M2MObjectInstance* inst = freq_object->object_instance(); |
ram54288 | 0:dbad57390bd1 | 262 | M2MResource* minBPM_res = inst->resource("5601"); |
ram54288 | 0:dbad57390bd1 | 263 | M2MResource* maxBPM_res = inst->resource("5602"); |
ram54288 | 0:dbad57390bd1 | 264 | |
ram54288 | 0:dbad57390bd1 | 265 | minBPM_res->set_value(0); |
ram54288 | 0:dbad57390bd1 | 266 | maxBPM_res->set_value(0); |
ram54288 | 0:dbad57390bd1 | 267 | } |
ram54288 | 0:dbad57390bd1 | 268 | |
ram54288 | 0:dbad57390bd1 | 269 | M2MObject* get_object() { |
ram54288 | 0:dbad57390bd1 | 270 | return freq_object; |
ram54288 | 0:dbad57390bd1 | 271 | } |
ram54288 | 0:dbad57390bd1 | 272 | |
ram54288 | 0:dbad57390bd1 | 273 | private: |
ram54288 | 0:dbad57390bd1 | 274 | M2MObject* freq_object; |
ram54288 | 0:dbad57390bd1 | 275 | }; |
ram54288 | 0:dbad57390bd1 | 276 | |
ram54288 | 0:dbad57390bd1 | 277 | int main() |
ram54288 | 0:dbad57390bd1 | 278 | { |
ram54288 | 0:dbad57390bd1 | 279 | // Seed the RNG for networking purposes |
ram54288 | 0:dbad57390bd1 | 280 | unsigned seed = utils::entropy_seed(); |
ram54288 | 0:dbad57390bd1 | 281 | srand(seed); |
ram54288 | 0:dbad57390bd1 | 282 | |
ram54288 | 0:dbad57390bd1 | 283 | // LEDs are active LOW - true/1 means off, false/0 means on |
ram54288 | 0:dbad57390bd1 | 284 | // Use the constants for easier reading |
ram54288 | 0:dbad57390bd1 | 285 | g_led_red = active_low::off; |
ram54288 | 0:dbad57390bd1 | 286 | g_led_green = active_low::off; |
ram54288 | 0:dbad57390bd1 | 287 | g_led_blue = active_low::off; |
ram54288 | 0:dbad57390bd1 | 288 | |
ram54288 | 0:dbad57390bd1 | 289 | |
ram54288 | 0:dbad57390bd1 | 290 | // Button falling edge is on push (rising is on release) |
ram54288 | 0:dbad57390bd1 | 291 | g_button_mode.fall(&on_mode); |
ram54288 | 0:dbad57390bd1 | 292 | g_button_tap.fall(&on_tap); |
ram54288 | 0:dbad57390bd1 | 293 | |
ram54288 | 0:dbad57390bd1 | 294 | #ifdef IOT_ENABLED |
ram54288 | 0:dbad57390bd1 | 295 | // Turn on the blue LED until connected to the network |
ram54288 | 0:dbad57390bd1 | 296 | // g_led_blue = active_low::off; |
ram54288 | 0:dbad57390bd1 | 297 | |
ram54288 | 0:dbad57390bd1 | 298 | |
ram54288 | 0:dbad57390bd1 | 299 | // Need to be connected with Ethernet cable for success |
ram54288 | 0:dbad57390bd1 | 300 | EthernetInterface ethernet; |
ram54288 | 0:dbad57390bd1 | 301 | if (ethernet.connect() != 0) |
ram54288 | 0:dbad57390bd1 | 302 | return 1; |
ram54288 | 0:dbad57390bd1 | 303 | |
ram54288 | 0:dbad57390bd1 | 304 | // Pair with the device connector |
ram54288 | 0:dbad57390bd1 | 305 | frdm_client client("coap://api.connector.mbed.com:5684", ðernet); |
ram54288 | 0:dbad57390bd1 | 306 | if (client.get_state() == frdm_client::state::error) |
ram54288 | 0:dbad57390bd1 | 307 | return 1; |
ram54288 | 0:dbad57390bd1 | 308 | |
ram54288 | 0:dbad57390bd1 | 309 | // The REST endpoints for this device |
ram54288 | 0:dbad57390bd1 | 310 | // Add your own M2MObjects to this list with push_back before client.connect() |
ram54288 | 0:dbad57390bd1 | 311 | M2MObjectList objects; |
ram54288 | 0:dbad57390bd1 | 312 | |
ram54288 | 0:dbad57390bd1 | 313 | //Frequency device definition and instance definition |
ram54288 | 0:dbad57390bd1 | 314 | FreqResource freq_resource; |
ram54288 | 0:dbad57390bd1 | 315 | |
ram54288 | 0:dbad57390bd1 | 316 | M2MDevice* device = frdm_client::make_device(); |
ram54288 | 0:dbad57390bd1 | 317 | objects.push_back(device); |
ram54288 | 0:dbad57390bd1 | 318 | objects.push_back(freq_resource.get_object()); |
ram54288 | 0:dbad57390bd1 | 319 | |
ram54288 | 0:dbad57390bd1 | 320 | // Publish the RESTful endpoints |
ram54288 | 0:dbad57390bd1 | 321 | client.connect(objects); |
ram54288 | 0:dbad57390bd1 | 322 | |
ram54288 | 0:dbad57390bd1 | 323 | // Connect complete; turn off blue LED forever |
ram54288 | 0:dbad57390bd1 | 324 | //g_led_blue = active_low::on; |
ram54288 | 0:dbad57390bd1 | 325 | |
ram54288 | 0:dbad57390bd1 | 326 | #endif |
ram54288 | 0:dbad57390bd1 | 327 | |
ram54288 | 0:dbad57390bd1 | 328 | while (true) |
ram54288 | 0:dbad57390bd1 | 329 | { |
ram54288 | 0:dbad57390bd1 | 330 | //updates.wait(25000); |
ram54288 | 0:dbad57390bd1 | 331 | #ifdef IOT_ENABLED |
ram54288 | 0:dbad57390bd1 | 332 | if (client.get_state() == frdm_client::state::error) |
ram54288 | 0:dbad57390bd1 | 333 | break; |
ram54288 | 0:dbad57390bd1 | 334 | #endif |
ram54288 | 0:dbad57390bd1 | 335 | |
ram54288 | 0:dbad57390bd1 | 336 | // Insert any code that must be run continuously here |
ram54288 | 0:dbad57390bd1 | 337 | if(bpm_changed){ |
ram54288 | 0:dbad57390bd1 | 338 | bpm_changed = 0; |
ram54288 | 0:dbad57390bd1 | 339 | freq_resource.handle_set_bpm(); |
ram54288 | 0:dbad57390bd1 | 340 | } |
ram54288 | 0:dbad57390bd1 | 341 | |
ram54288 | 0:dbad57390bd1 | 342 | if(min_changed) { |
ram54288 | 0:dbad57390bd1 | 343 | min_changed = 0; |
ram54288 | 0:dbad57390bd1 | 344 | freq_resource.handle_set_min_bpm(); |
ram54288 | 0:dbad57390bd1 | 345 | } |
ram54288 | 0:dbad57390bd1 | 346 | if(max_changed) { |
ram54288 | 0:dbad57390bd1 | 347 | max_changed = 0; |
ram54288 | 0:dbad57390bd1 | 348 | freq_resource.handle_set_max_bpm(); |
ram54288 | 0:dbad57390bd1 | 349 | } |
ram54288 | 0:dbad57390bd1 | 350 | |
ram54288 | 0:dbad57390bd1 | 351 | } |
ram54288 | 0:dbad57390bd1 | 352 | |
ram54288 | 0:dbad57390bd1 | 353 | #ifdef IOT_ENABLED |
ram54288 | 0:dbad57390bd1 | 354 | client.disconnect(); |
ram54288 | 0:dbad57390bd1 | 355 | #endif |
ram54288 | 0:dbad57390bd1 | 356 | return 1; |
ram54288 | 0:dbad57390bd1 | 357 | } |