Blynk library for embedded hardware. Works with Arduino, ESP8266, Raspberry Pi, Intel Edison/Galileo, LinkIt ONE, Particle Core/Photon, Energia, ARM mbed, etc. http://www.blynk.cc/

Dependents:   Blynk_RBL_BLE_Nano Blynk_MicroBit Blynk_Serial Blynk_RBL_BLE_Nano

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlynkApiMbed.h Source File

BlynkApiMbed.h

Go to the documentation of this file.
00001 /**
00002  * @file       BlynkApiMbed.h
00003  * @author     Volodymyr Shymanskyy
00004  * @license    This project is released under the MIT License (MIT)
00005  * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
00006  * @date       Mar 2015
00007  * @brief
00008  *
00009  */
00010 
00011 #ifndef BlynkApiMbed_h
00012 #define BlynkApiMbed_h
00013 
00014 #include <Blynk/BlynkApi.h>
00015 #include <mbed.h>
00016 
00017 #ifdef BLYNK_NO_INFO
00018 
00019 template<class Proto>
00020 BLYNK_FORCE_INLINE
00021 void BlynkApi<Proto>::sendInfo() {}
00022 
00023 #else
00024 
00025 template<class Proto>
00026 BLYNK_FORCE_INLINE
00027 void BlynkApi<Proto>::sendInfo()
00028 {
00029     static const char profile[] BLYNK_PROGMEM =
00030         BLYNK_PARAM_KV("ver"    , BLYNK_VERSION)
00031         BLYNK_PARAM_KV("h-beat" , BLYNK_TOSTRING(BLYNK_HEARTBEAT))
00032         BLYNK_PARAM_KV("buff-in", BLYNK_TOSTRING(BLYNK_MAX_READBYTES))
00033 #ifdef BLYNK_INFO_DEVICE
00034         BLYNK_PARAM_KV("dev"    , BLYNK_INFO_DEVICE)
00035 #endif
00036 #ifdef BLYNK_INFO_CPU
00037         BLYNK_PARAM_KV("cpu"    , BLYNK_INFO_CPU)
00038 #endif
00039 #ifdef BLYNK_INFO_CONNECTION
00040         BLYNK_PARAM_KV("con"    , BLYNK_INFO_CONNECTION)
00041 #endif
00042         BLYNK_PARAM_KV("build"  , __DATE__ " " __TIME__)
00043     ;
00044     const size_t profile_len = sizeof(profile)-1;
00045 
00046 #ifdef BLYNK_HAS_PROGMEM
00047     char mem[profile_len];
00048     memcpy_P(mem, profile, profile_len);
00049     static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_INTERNAL, 0, mem, profile_len);
00050 #else
00051     static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_INTERNAL, 0, profile, profile_len);
00052 #endif
00053     return;
00054 }
00055 
00056 #endif
00057 
00058 template<class Proto>
00059 BLYNK_FORCE_INLINE
00060 void BlynkApi<Proto>::processCmd(const void* buff, size_t len)
00061 {
00062     BlynkParam param((void*)buff, len);
00063     BlynkParam::iterator it = param.begin();
00064     if (it >= param.end())
00065         return;
00066     const char* cmd = it.asStr();
00067     uint16_t cmd16;
00068     memcpy(&cmd16, cmd, sizeof(cmd16));
00069     if (++it >= param.end())
00070         return;
00071 
00072 #if defined(analogInputToDigitalPin)
00073     // Good! Analog pins can be referenced on this device by name.
00074     const uint8_t pin = (it.asStr()[0] == 'A') ?
00075                          analogInputToDigitalPin(atoi(it.asStr()+1)) :
00076                          it.asInt();
00077 #else
00078     #if defined(BLYNK_DEBUG_ALL)
00079         #pragma message "analogInputToDigitalPin not defined"
00080     #endif
00081     const uint8_t pin = it.asInt();
00082 #endif
00083 
00084     switch(cmd16) {
00085 
00086 #ifndef BLYNK_NO_BUILTIN
00087 
00088     case BLYNK_HW_PM: {
00089         while (it < param.end()) {
00090             ++it;
00091             if (!strcmp(it.asStr(), "in")) {
00092                 //pinMode(pin, INPUT);
00093             } else if (!strcmp(it.asStr(), "out") || !strcmp(it.asStr(), "pwm")) {
00094                 //pinMode(pin, OUTPUT);
00095             } else {
00096 #ifdef BLYNK_DEBUG
00097                 BLYNK_LOG4(BLYNK_F("Invalid pin "), pin, BLYNK_F(" mode "), it.asStr());
00098 #endif
00099             }
00100             ++it;
00101         }
00102     } break;
00103     case BLYNK_HW_DR: {
00104         DigitalIn p((PinName)pin);
00105         char mem[16];
00106         BlynkParam rsp(mem, 0, sizeof(mem));
00107         rsp.add("dw");
00108         rsp.add(pin);
00109         rsp.add(int(p));
00110         static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
00111     } break;
00112     case BLYNK_HW_DW: {
00113         // Should be 1 parameter (value)
00114         if (++it >= param.end())
00115             return;
00116 
00117         //BLYNK_LOG("digitalWrite %d -> %d", pin, it.asInt());
00118         DigitalOut p((PinName)pin);
00119         p = it.asInt() ? 1 : 0;
00120     } break;
00121     case BLYNK_HW_AR: {
00122         AnalogIn p((PinName)pin);
00123         char mem[16];
00124         BlynkParam rsp(mem, 0, sizeof(mem));
00125         rsp.add("aw");
00126         rsp.add(pin);
00127         rsp.add(int(p.read() * 1024));
00128         static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
00129     } break;
00130     case BLYNK_HW_AW: {
00131         // TODO: Not supported yet
00132     } break;
00133 
00134 #endif
00135 
00136     case BLYNK_HW_VR: {
00137         BlynkReq req = { pin };
00138         WidgetReadHandler handler = GetReadHandler(pin);
00139         if (handler && (handler != BlynkWidgetRead)) {
00140             handler(req);
00141         } else {
00142             BlynkWidgetReadDefault(req);
00143         }
00144     } break;
00145     case BLYNK_HW_VW: {
00146         ++it;
00147         char* start = (char*)it.asStr();
00148         BlynkParam param2(start, len - (start - (char*)buff));
00149         BlynkReq req = { pin };
00150         WidgetWriteHandler handler = GetWriteHandler(pin);
00151         if (handler && (handler != BlynkWidgetWrite)) {
00152             handler(req, param2);
00153         } else {
00154             BlynkWidgetWriteDefault(req, param2);
00155         }
00156     } break;
00157     default:
00158         BLYNK_LOG2(BLYNK_F("Invalid HW cmd: "), cmd);
00159         static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_RESPONSE, static_cast<Proto*>(this)->msgIdOutOverride, NULL, BLYNK_ILLEGAL_COMMAND);
00160     }
00161 }
00162 
00163 #endif