kcod 1357
/
seoul-iot-hackathon-demo
test fork
Fork of seoul-iot-hackathon-demo by
2_connected_app/main_working.h@10:911c7a95308e, 2017-11-04 (annotated)
- Committer:
- kcod
- Date:
- Sat Nov 04 07:12:21 2017 +0000
- Revision:
- 10:911c7a95308e
- Parent:
- 2:b13c81c349ec
test 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 "C12832.h" |
sarahmarshy |
0:6b7ffde9f287 | 17 | |
sarahmarshy |
0:6b7ffde9f287 | 18 | C12832 lcd(D11, D13, D6, D7, D10); |
sarahmarshy |
0:6b7ffde9f287 | 19 | AnalogIn pot1(A0); |
sarahmarshy |
0:6b7ffde9f287 | 20 | EventQueue queue; |
sarahmarshy |
0:6b7ffde9f287 | 21 | DigitalOut led(D9, 1); |
sarahmarshy |
0:6b7ffde9f287 | 22 | |
sarahmarshy |
0:6b7ffde9f287 | 23 | // DECLARE RESOURCES HERE |
sarahmarshy |
0:6b7ffde9f287 | 24 | static M2MResource* potentiometer_res; // Resource for potentiometer value |
sarahmarshy |
0:6b7ffde9f287 | 25 | static M2MResource* message_res; // Resource to hold a string message |
sarahmarshy |
0:6b7ffde9f287 | 26 | static M2MResource* display_res; // Resource for displaying message on LCD screen |
sarahmarshy |
0:6b7ffde9f287 | 27 | static M2MResource* led_res; // Resource to blink LED |
sarahmarshy |
0:6b7ffde9f287 | 28 | |
sarahmarshy |
0:6b7ffde9f287 | 29 | void lcd_print(const char* message) { |
sarahmarshy |
0:6b7ffde9f287 | 30 | lcd.cls(); |
sarahmarshy |
0:6b7ffde9f287 | 31 | lcd.locate(0,3); |
sarahmarshy |
0:6b7ffde9f287 | 32 | lcd.printf(message); |
sarahmarshy |
0:6b7ffde9f287 | 33 | } |
sarahmarshy |
0:6b7ffde9f287 | 34 | |
sarahmarshy |
0:6b7ffde9f287 | 35 | void blink_led() { |
sarahmarshy |
0:6b7ffde9f287 | 36 | led = !led; |
sarahmarshy |
0:6b7ffde9f287 | 37 | } |
sarahmarshy |
0:6b7ffde9f287 | 38 | |
sarahmarshy |
0:6b7ffde9f287 | 39 | void set_blink_led(int *) { |
sarahmarshy |
0:6b7ffde9f287 | 40 | static int blink_id = NULL; |
sarahmarshy |
0:6b7ffde9f287 | 41 | // GET VALUE OF LED RESOURCE |
sarahmarshy |
0:6b7ffde9f287 | 42 | int blink = led_res->get_value_int(); |
sarahmarshy |
0:6b7ffde9f287 | 43 | if (blink == 1 && blink_id == NULL) { |
sarahmarshy |
0:6b7ffde9f287 | 44 | blink_id = queue.call_every(500, blink_led); |
sarahmarshy |
0:6b7ffde9f287 | 45 | } |
sarahmarshy |
0:6b7ffde9f287 | 46 | else if (blink == 0) { |
sarahmarshy |
0:6b7ffde9f287 | 47 | queue.cancel(blink_id); |
sarahmarshy |
0:6b7ffde9f287 | 48 | blink_id = NULL; |
sarahmarshy |
0:6b7ffde9f287 | 49 | led = 1; |
sarahmarshy |
0:6b7ffde9f287 | 50 | } |
sarahmarshy |
0:6b7ffde9f287 | 51 | } |
sarahmarshy |
0:6b7ffde9f287 | 52 | |
sarahmarshy |
0:6b7ffde9f287 | 53 | // MESSAGE RESOURCE PUT HANDLER |
sarahmarshy |
0:6b7ffde9f287 | 54 | void message_updated_callback(const char *) |
sarahmarshy |
0:6b7ffde9f287 | 55 | { |
sarahmarshy |
0:6b7ffde9f287 | 56 | printf("PUT received for message resource, new value: %s\n", message_res->get_value_string().c_str()); |
sarahmarshy |
0:6b7ffde9f287 | 57 | } |
sarahmarshy |
0:6b7ffde9f287 | 58 | |
sarahmarshy |
0:6b7ffde9f287 | 59 | // DISPLAY RESOURCE POST HANDLER |
sarahmarshy |
0:6b7ffde9f287 | 60 | void display_message_callback(void *) { |
sarahmarshy |
0:6b7ffde9f287 | 61 | // Get the value of the message resource |
sarahmarshy |
0:6b7ffde9f287 | 62 | String pattern_string = message_res->get_value_string(); |
sarahmarshy |
0:6b7ffde9f287 | 63 | // Display it on the LCD screen |
sarahmarshy |
0:6b7ffde9f287 | 64 | lcd_print(pattern_string.c_str()); |
sarahmarshy |
0:6b7ffde9f287 | 65 | } |
sarahmarshy |
0:6b7ffde9f287 | 66 | |
sarahmarshy |
0:6b7ffde9f287 | 67 | void read_potentiometer() { |
sarahmarshy |
0:6b7ffde9f287 | 68 | static float potentiometer_val = 0; |
sarahmarshy |
0:6b7ffde9f287 | 69 | if ((float)pot1 != potentiometer_val) { |
sarahmarshy |
0:6b7ffde9f287 | 70 | potentiometer_val = (float)pot1; |
sarahmarshy |
0:6b7ffde9f287 | 71 | char val[13]; |
sarahmarshy |
0:6b7ffde9f287 | 72 | sprintf(val, "%.2f", potentiometer_val); |
sarahmarshy |
0:6b7ffde9f287 | 73 | // UPDATE RESOURCE VALUE |
sarahmarshy |
0:6b7ffde9f287 | 74 | potentiometer_res->set_value((uint8_t*)val, strlen(val)); |
sarahmarshy |
0:6b7ffde9f287 | 75 | } |
sarahmarshy |
0:6b7ffde9f287 | 76 | } |
sarahmarshy |
0:6b7ffde9f287 | 77 | |
sarahmarshy |
0:6b7ffde9f287 | 78 | int main() |
sarahmarshy |
0:6b7ffde9f287 | 79 | { |
sarahmarshy |
0:6b7ffde9f287 | 80 | // SimpleClient is used for registering and unregistering resources to a server. |
sarahmarshy |
0:6b7ffde9f287 | 81 | SimpleM2MClient mbedClient; |
sarahmarshy |
0:6b7ffde9f287 | 82 | |
sarahmarshy |
0:6b7ffde9f287 | 83 | if (!mbedClient.init()) { |
sarahmarshy |
0:6b7ffde9f287 | 84 | printf("Initialization failed, exiting application!\n"); |
sarahmarshy |
0:6b7ffde9f287 | 85 | return 1; |
sarahmarshy |
0:6b7ffde9f287 | 86 | } |
sarahmarshy |
0:6b7ffde9f287 | 87 | |
sarahmarshy |
0:6b7ffde9f287 | 88 | // ADD RESOURCES HERE |
sarahmarshy |
0:6b7ffde9f287 | 89 | potentiometer_res = mbedClient.add_cloud_resource(3200, 0, 5501, "potentiometer_resource", M2MResourceInstance::FLOAT, M2MBase::GET_ALLOWED, 0, true, NULL); |
sarahmarshy |
0:6b7ffde9f287 | 90 | 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 | 91 | display_res = mbedClient.add_cloud_resource(3201, 0, 5850, "display_resource", M2MResourceInstance::STRING,M2MBase::POST_ALLOWED, "", false, (void*)display_message_callback); |
sarahmarshy |
0:6b7ffde9f287 | 92 | 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 | 93 | |
sarahmarshy |
0:6b7ffde9f287 | 94 | mbedClient.start_client(); |
sarahmarshy |
0:6b7ffde9f287 | 95 | queue.call_every(100, read_potentiometer); |
sarahmarshy |
0:6b7ffde9f287 | 96 | while(mbedClient.is_register_called()){ |
sarahmarshy |
0:6b7ffde9f287 | 97 | wait_ms(100); |
sarahmarshy |
0:6b7ffde9f287 | 98 | queue.dispatch(0); |
sarahmarshy |
0:6b7ffde9f287 | 99 | } |
sarahmarshy |
0:6b7ffde9f287 | 100 | } |