Donald Meyers / Mbed OS evan
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "utils.hpp"
00003 
00004 #include "EthernetInterface.h"
00005 #include "frdm_client.hpp"
00006 
00007 #include "metronome.hpp"
00008 
00009 #define IOT_ENABLED
00010 
00011 namespace active_low
00012 {
00013     const bool on = false;
00014     const bool off = true;
00015 }
00016 
00017 DigitalOut g_led_red(LED1);
00018 DigitalOut g_led_green(LED2);
00019 DigitalOut g_led_blue(LED3);
00020 
00021 InterruptIn g_button_mode(SW3);
00022 InterruptIn g_button_tap(SW2);
00023 
00024 metronome *m = new metronome();
00025 
00026 float delay = 0, pulse = 0.1f;
00027 
00028 void on_mode()
00029 {
00030     // Change modes
00031     if (m->is_timing()) {
00032         m->stop_timing();   
00033     } else {
00034         m->start_timing();
00035     }
00036 
00037 }
00038 
00039 void on_tap()
00040 {
00041     if(m->is_timing()){
00042         g_led_red = active_low::on;
00043         wait(pulse);
00044         g_led_red = active_low::off;
00045         m->tap();
00046     }
00047 }
00048 
00049 int main()
00050 {
00051     // Seed the RNG for networking purposes
00052     unsigned seed = utils::entropy_seed();
00053     srand(seed);
00054 
00055     // LEDs are active LOW - true/1 means off, false/0 means on
00056     // Use the constants for easier reading
00057     g_led_red = active_low::off;
00058     g_led_green = active_low::off;
00059     g_led_blue = active_low::off;
00060 
00061     // Button falling edge is on push (rising is on release)
00062     g_button_mode.fall(&on_mode);
00063     g_button_tap.fall(&on_tap);
00064     
00065     
00066 
00067 #ifdef IOT_ENABLED
00068     // Turn on the blue LED until connected to the network
00069     g_led_blue = active_low::on;
00070 
00071     // Need to be connected with Ethernet cable for success
00072     EthernetInterface ethernet;
00073     if (ethernet.connect() != 0)
00074         return 1;
00075 
00076     // Pair with the device connector
00077     frdm_client client("coap://api.connector.mbed.com:5684", &ethernet);
00078     if (client.get_state() == frdm_client::state::error)
00079         return 1;
00080 
00081     // The REST endpoints for this device
00082     // Add your own M2MObjects to this list with push_back before client.connect()
00083     M2MObjectList objects;
00084     
00085 
00086     M2MDevice* device = frdm_client::make_device();
00087     objects.push_back(device);
00088 
00089     
00090     M2MObject* frequency_object = M2MInterfaceFactory::create_object("3318");
00091     M2MObjectInstance* frequency_object_instance = frequency_object->create_object_instance();
00092     
00093     M2MResource* set_point_value = frequency_object_instance->create_dynamic_resource("5900", "Set",
00094         M2MResourceInstance::STRING, true);
00095     set_point_value->set_operation(M2MBase::GET_PUT_ALLOWED);
00096     set_point_value->set_value((uint8_t*)"0", 1);
00097     
00098     M2MResource* min = frequency_object_instance->create_dynamic_resource("5601", "Min",
00099         M2MResourceInstance::STRING, true);
00100     min->set_operation(M2MBase::GET_PUT_ALLOWED);
00101     min->set_value((uint8_t*)"0", 1);
00102     
00103     M2MResource* max = frequency_object_instance->create_dynamic_resource("5602", "Max",
00104         M2MResourceInstance::STRING, true);
00105     max->set_operation(M2MBase::GET_PUT_ALLOWED);
00106     max->set_value((uint8_t*)"0", 1);
00107     
00108     M2MResource* units = frequency_object_instance->create_dynamic_resource("5701", "Units",
00109         M2MResourceInstance::STRING, true);
00110     units->set_operation(M2MBase::GET_PUT_ALLOWED);
00111     units->set_value((uint8_t*)"BPM", 3);
00112 
00113     objects.push_back(frequency_object);
00114     
00115     
00116     // Publish the RESTful endpoints
00117     client.connect(objects);
00118 
00119     // Connect complete; turn off blue LED forever
00120     g_led_blue = active_low::off;
00121 #endif
00122 
00123     while (true)
00124     {
00125 #ifdef IOT_ENABLED
00126         if (client.get_state() == frdm_client::state::error)
00127             break;
00128 #endif
00129         
00130         // Insert any code that must be run continuously here
00131         
00132         char buffer[20];
00133         int size;
00134         if(!m->is_timing()){
00135             size = sprintf(buffer,"%d",m->get_bpm());
00136             set_point_value->set_value((uint8_t*)buffer, size);
00137             delay = m->get_delay() / 1000.0f;
00138             if(delay != 0){
00139                 g_led_green = active_low::on;
00140                 wait(pulse);
00141                 g_led_green = active_low::off;
00142                 wait(delay - pulse);    
00143             }               
00144         }
00145     }
00146 
00147 #ifdef IOT_ENABLED
00148     client.disconnect();
00149 #endif
00150 
00151     return 1;
00152 }