Hackathon Repo
Dependencies: C12832
2_connected_app/main_working.h@0:6b7ffde9f287, 2017-09-28 (annotated)
- Committer:
- sarahmarshy
- Date:
- Thu Sep 28 13:35:07 2017 -0500
- Revision:
- 0:6b7ffde9f287
- Child:
- 2:b13c81c349ec
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sarahmarshy |
0:6b7ffde9f287 | 1 | //---------------------------------------------------------------------------- |
sarahmarshy |
0:6b7ffde9f287 | 2 | // The confidential and proprietary information contained in this file may |
sarahmarshy |
0:6b7ffde9f287 | 3 | // only be used by a person authorised under and to the extent permitted |
sarahmarshy |
0:6b7ffde9f287 | 4 | // by a subsisting licensing agreement from ARM Limited or its affiliates. |
sarahmarshy |
0:6b7ffde9f287 | 5 | // |
sarahmarshy |
0:6b7ffde9f287 | 6 | // (C) COPYRIGHT 2016 ARM Limited or its affiliates. |
sarahmarshy |
0:6b7ffde9f287 | 7 | // ALL RIGHTS RESERVED |
sarahmarshy |
0:6b7ffde9f287 | 8 | // |
sarahmarshy |
0:6b7ffde9f287 | 9 | // This entire notice must be reproduced on all copies of this file |
sarahmarshy |
0:6b7ffde9f287 | 10 | // and copies of this file may only be made by a person if such person is |
sarahmarshy |
0:6b7ffde9f287 | 11 | // permitted to do so under the terms of a subsisting license agreement |
sarahmarshy |
0:6b7ffde9f287 | 12 | // from ARM Limited or its affiliates. |
sarahmarshy |
0:6b7ffde9f287 | 13 | //---------------------------------------------------------------------------- |
sarahmarshy |
0:6b7ffde9f287 | 14 | #include "simplem2mclient.h" |
sarahmarshy |
0:6b7ffde9f287 | 15 | #include "mbed.h" |
sarahmarshy |
0:6b7ffde9f287 | 16 | #include "application_init.h" |
sarahmarshy |
0:6b7ffde9f287 | 17 | #include "C12832.h" |
sarahmarshy |
0:6b7ffde9f287 | 18 | |
sarahmarshy |
0:6b7ffde9f287 | 19 | C12832 lcd(D11, D13, D6, D7, D10); |
sarahmarshy |
0:6b7ffde9f287 | 20 | AnalogIn pot1(A0); |
sarahmarshy |
0:6b7ffde9f287 | 21 | EventQueue queue; |
sarahmarshy |
0:6b7ffde9f287 | 22 | DigitalOut led(D9, 1); |
sarahmarshy |
0:6b7ffde9f287 | 23 | |
sarahmarshy |
0:6b7ffde9f287 | 24 | // DECLARE RESOURCES HERE |
sarahmarshy |
0:6b7ffde9f287 | 25 | static M2MResource* potentiometer_res; // Resource for potentiometer value |
sarahmarshy |
0:6b7ffde9f287 | 26 | static M2MResource* message_res; // Resource to hold a string message |
sarahmarshy |
0:6b7ffde9f287 | 27 | static M2MResource* display_res; // Resource for displaying message on LCD screen |
sarahmarshy |
0:6b7ffde9f287 | 28 | static M2MResource* led_res; // Resource to blink LED |
sarahmarshy |
0:6b7ffde9f287 | 29 | |
sarahmarshy |
0:6b7ffde9f287 | 30 | void lcd_print(const char* message) { |
sarahmarshy |
0:6b7ffde9f287 | 31 | lcd.cls(); |
sarahmarshy |
0:6b7ffde9f287 | 32 | lcd.locate(0,3); |
sarahmarshy |
0:6b7ffde9f287 | 33 | lcd.printf(message); |
sarahmarshy |
0:6b7ffde9f287 | 34 | } |
sarahmarshy |
0:6b7ffde9f287 | 35 | |
sarahmarshy |
0:6b7ffde9f287 | 36 | void blink_led() { |
sarahmarshy |
0:6b7ffde9f287 | 37 | led = !led; |
sarahmarshy |
0:6b7ffde9f287 | 38 | } |
sarahmarshy |
0:6b7ffde9f287 | 39 | |
sarahmarshy |
0:6b7ffde9f287 | 40 | void set_blink_led(int *) { |
sarahmarshy |
0:6b7ffde9f287 | 41 | static int blink_id = NULL; |
sarahmarshy |
0:6b7ffde9f287 | 42 | // GET VALUE OF LED RESOURCE |
sarahmarshy |
0:6b7ffde9f287 | 43 | int blink = led_res->get_value_int(); |
sarahmarshy |
0:6b7ffde9f287 | 44 | if (blink == 1 && blink_id == NULL) { |
sarahmarshy |
0:6b7ffde9f287 | 45 | blink_id = queue.call_every(500, blink_led); |
sarahmarshy |
0:6b7ffde9f287 | 46 | } |
sarahmarshy |
0:6b7ffde9f287 | 47 | else if (blink == 0) { |
sarahmarshy |
0:6b7ffde9f287 | 48 | queue.cancel(blink_id); |
sarahmarshy |
0:6b7ffde9f287 | 49 | blink_id = NULL; |
sarahmarshy |
0:6b7ffde9f287 | 50 | led = 1; |
sarahmarshy |
0:6b7ffde9f287 | 51 | } |
sarahmarshy |
0:6b7ffde9f287 | 52 | } |
sarahmarshy |
0:6b7ffde9f287 | 53 | |
sarahmarshy |
0:6b7ffde9f287 | 54 | // MESSAGE RESOURCE PUT HANDLER |
sarahmarshy |
0:6b7ffde9f287 | 55 | void message_updated_callback(const char *) |
sarahmarshy |
0:6b7ffde9f287 | 56 | { |
sarahmarshy |
0:6b7ffde9f287 | 57 | printf("PUT received for message resource, new value: %s\n", message_res->get_value_string().c_str()); |
sarahmarshy |
0:6b7ffde9f287 | 58 | } |
sarahmarshy |
0:6b7ffde9f287 | 59 | |
sarahmarshy |
0:6b7ffde9f287 | 60 | // DISPLAY RESOURCE POST HANDLER |
sarahmarshy |
0:6b7ffde9f287 | 61 | void display_message_callback(void *) { |
sarahmarshy |
0:6b7ffde9f287 | 62 | // Get the value of the message resource |
sarahmarshy |
0:6b7ffde9f287 | 63 | String pattern_string = message_res->get_value_string(); |
sarahmarshy |
0:6b7ffde9f287 | 64 | // Display it on the LCD screen |
sarahmarshy |
0:6b7ffde9f287 | 65 | lcd_print(pattern_string.c_str()); |
sarahmarshy |
0:6b7ffde9f287 | 66 | } |
sarahmarshy |
0:6b7ffde9f287 | 67 | |
sarahmarshy |
0:6b7ffde9f287 | 68 | void read_potentiometer() { |
sarahmarshy |
0:6b7ffde9f287 | 69 | static float potentiometer_val = 0; |
sarahmarshy |
0:6b7ffde9f287 | 70 | if ((float)pot1 != potentiometer_val) { |
sarahmarshy |
0:6b7ffde9f287 | 71 | potentiometer_val = (float)pot1; |
sarahmarshy |
0:6b7ffde9f287 | 72 | char val[13]; |
sarahmarshy |
0:6b7ffde9f287 | 73 | sprintf(val, "%.2f", potentiometer_val); |
sarahmarshy |
0:6b7ffde9f287 | 74 | // UPDATE RESOURCE VALUE |
sarahmarshy |
0:6b7ffde9f287 | 75 | potentiometer_res->set_value((uint8_t*)val, strlen(val)); |
sarahmarshy |
0:6b7ffde9f287 | 76 | } |
sarahmarshy |
0:6b7ffde9f287 | 77 | } |
sarahmarshy |
0:6b7ffde9f287 | 78 | |
sarahmarshy |
0:6b7ffde9f287 | 79 | int main() |
sarahmarshy |
0:6b7ffde9f287 | 80 | { |
sarahmarshy |
0:6b7ffde9f287 | 81 | // SimpleClient is used for registering and unregistering resources to a server. |
sarahmarshy |
0:6b7ffde9f287 | 82 | SimpleM2MClient mbedClient; |
sarahmarshy |
0:6b7ffde9f287 | 83 | |
sarahmarshy |
0:6b7ffde9f287 | 84 | if (!mbedClient.init()) { |
sarahmarshy |
0:6b7ffde9f287 | 85 | printf("Initialization failed, exiting application!\n"); |
sarahmarshy |
0:6b7ffde9f287 | 86 | return 1; |
sarahmarshy |
0:6b7ffde9f287 | 87 | } |
sarahmarshy |
0:6b7ffde9f287 | 88 | |
sarahmarshy |
0:6b7ffde9f287 | 89 | // ADD RESOURCES HERE |
sarahmarshy |
0:6b7ffde9f287 | 90 | potentiometer_res = mbedClient.add_cloud_resource(3200, 0, 5501, "potentiometer_resource", M2MResourceInstance::FLOAT, M2MBase::GET_ALLOWED, 0, true, NULL); |
sarahmarshy |
0:6b7ffde9f287 | 91 | message_res = mbedClient.add_cloud_resource(3201, 0, 5853, "message_resource", M2MResourceInstance::STRING, M2MBase::GET_PUT_ALLOWED, "Hello world!", false, (void*)message_updated_callback); |
sarahmarshy |
0:6b7ffde9f287 | 92 | display_res = mbedClient.add_cloud_resource(3201, 0, 5850, "display_resource", M2MResourceInstance::STRING,M2MBase::POST_ALLOWED, "", false, (void*)display_message_callback); |
sarahmarshy |
0:6b7ffde9f287 | 93 | led_res = mbedClient.add_cloud_resource(3202, 0, 5853, "led_resource", M2MResourceInstance::INTEGER,M2MBase::GET_PUT_ALLOWED, 0, false, (void*)set_blink_led); |
sarahmarshy |
0:6b7ffde9f287 | 94 | |
sarahmarshy |
0:6b7ffde9f287 | 95 | mbedClient.start_client(); |
sarahmarshy |
0:6b7ffde9f287 | 96 | queue.call_every(100, read_potentiometer); |
sarahmarshy |
0:6b7ffde9f287 | 97 | while(mbedClient.is_register_called()){ |
sarahmarshy |
0:6b7ffde9f287 | 98 | wait_ms(100); |
sarahmarshy |
0:6b7ffde9f287 | 99 | queue.dispatch(0); |
sarahmarshy |
0:6b7ffde9f287 | 100 | } |
sarahmarshy |
0:6b7ffde9f287 | 101 | } |