blynk & neopixelring & w7500

Fork of WIZwiki-7500_Blynk by IOP

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 "mbed.h"
00015 
00016 static Timer  blynk_millis_timer;
00017 static Ticker blynk_waker;
00018 
00019 static
00020 void blynk_wake() {
00021     //pc.puts("(...)");
00022 }
00023 
00024 static
00025 void delay(unsigned long ms)
00026 {
00027     wait_ms(ms);
00028 }
00029 
00030 static
00031 unsigned long millis(void)
00032 {
00033     return blynk_millis_timer.read_ms();
00034 }
00035 
00036 #include <Blynk/BlynkApi.h>
00037 
00038 template<class Proto>
00039 void BlynkApi<Proto>::Init()
00040 {
00041     blynk_waker.attach(&blynk_wake, 2.0);
00042     blynk_millis_timer.start();
00043 }
00044 
00045 template<class Proto>
00046 BLYNK_FORCE_INLINE
00047 millis_time_t BlynkApi<Proto>::getMillis()
00048 {
00049     return blynk_millis_timer.read_ms();
00050 }
00051 
00052 #ifdef BLYNK_NO_INFO
00053 
00054 template<class Proto>
00055 BLYNK_FORCE_INLINE
00056 void BlynkApi<Proto>::sendInfo() {}
00057 
00058 #else
00059 
00060 template<class Proto>
00061 BLYNK_FORCE_INLINE
00062 void BlynkApi<Proto>::sendInfo()
00063 {
00064     static const char profile[] BLYNK_PROGMEM =
00065         BLYNK_PARAM_KV("ver"    , BLYNK_VERSION)
00066         BLYNK_PARAM_KV("h-beat" , BLYNK_TOSTRING(BLYNK_HEARTBEAT))
00067         BLYNK_PARAM_KV("buff-in", BLYNK_TOSTRING(BLYNK_MAX_READBYTES))
00068 #ifdef BLYNK_INFO_DEVICE
00069         BLYNK_PARAM_KV("dev"    , BLYNK_INFO_DEVICE)
00070 #endif
00071 #ifdef BLYNK_INFO_CPU
00072         BLYNK_PARAM_KV("cpu"    , BLYNK_INFO_CPU)
00073 #endif
00074 #ifdef BLYNK_INFO_CONNECTION
00075         BLYNK_PARAM_KV("con"    , BLYNK_INFO_CONNECTION)
00076 #endif
00077         BLYNK_PARAM_KV("build"  , __DATE__ " " __TIME__)
00078     ;
00079     const size_t profile_len = sizeof(profile)-1;
00080 
00081 #ifdef BLYNK_HAS_PROGMEM
00082     char mem[profile_len];
00083     memcpy_P(mem, profile, profile_len);
00084     static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_INTERNAL, 0, mem, profile_len);
00085 #else
00086     static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_INTERNAL, 0, profile, profile_len);
00087 #endif
00088     return;
00089 }
00090 
00091 #endif
00092 
00093 template<class Proto>
00094 BLYNK_FORCE_INLINE
00095 void BlynkApi<Proto>::processCmd(const void* buff, size_t len)
00096 {
00097     BlynkParam param((void*)buff, len);
00098     BlynkParam::iterator it = param.begin();
00099     if (it >= param.end())
00100         return;
00101     const char* cmd = it.asStr();
00102     uint16_t cmd16;
00103     memcpy(&cmd16, cmd, sizeof(cmd16));
00104     if (++it >= param.end())
00105         return;
00106 
00107 #if defined(analogInputToDigitalPin)
00108     // Good! Analog pins can be referenced on this device by name.
00109     const uint8_t pin = (it.asStr()[0] == 'A') ?
00110                          analogInputToDigitalPin(atoi(it.asStr()+1)) :
00111                          it.asInt();
00112 #else
00113     #if defined(BLYNK_DEBUG_ALL)
00114         #pragma message "analogInputToDigitalPin not defined"
00115     #endif
00116     const uint8_t pin = it.asInt();
00117 #endif
00118 
00119     switch(cmd16) {
00120 
00121 #ifndef BLYNK_NO_BUILTIN
00122 
00123     case BLYNK_HW_PM: {
00124         while (it < param.end()) {
00125             ++it;
00126             if (!strcmp(it.asStr(), "in")) {
00127                 //pinMode(pin, INPUT);
00128             } else if (!strcmp(it.asStr(), "out") || !strcmp(it.asStr(), "pwm")) {
00129                 //pinMode(pin, OUTPUT);
00130             } else {
00131 #ifdef BLYNK_DEBUG
00132                 BLYNK_LOG4(BLYNK_F("Invalid pin "), pin, BLYNK_F(" mode "), it.asStr());
00133 #endif
00134             }
00135             ++it;
00136         }
00137     } break;
00138     case BLYNK_HW_DR: {
00139         DigitalIn p((PinName)pin);
00140         char mem[16];
00141         BlynkParam rsp(mem, 0, sizeof(mem));
00142         rsp.add("dw");
00143         rsp.add(pin);
00144         rsp.add(int(p));
00145         static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
00146     } break;
00147     case BLYNK_HW_DW: {
00148         // Should be 1 parameter (value)
00149         if (++it >= param.end())
00150             return;
00151 
00152         //BLYNK_LOG("digitalWrite %d -> %d", pin, it.asInt());
00153         DigitalOut p((PinName)pin);
00154         p = it.asInt() ? 1 : 0;
00155     } break;
00156     case BLYNK_HW_AR: {
00157         AnalogIn p((PinName)pin);
00158         char mem[16];
00159         BlynkParam rsp(mem, 0, sizeof(mem));
00160         rsp.add("aw");
00161         rsp.add(pin);
00162         rsp.add(int(p.read() * 1024));
00163         static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
00164     } break;
00165     case BLYNK_HW_AW: {
00166         // TODO: Not supported yet
00167     } break;
00168 
00169 #endif
00170 
00171     case BLYNK_HW_VR: {
00172         BlynkReq req = { pin };
00173         WidgetReadHandler handler = GetReadHandler(pin);
00174         if (handler && (handler != BlynkWidgetRead)) {
00175             handler(req);
00176         } else {
00177             BlynkWidgetReadDefault(req);
00178         }
00179     } break;
00180     case BLYNK_HW_VW: {
00181         ++it;
00182         char* start = (char*)it.asStr();
00183         BlynkParam param2(start, len - (start - (char*)buff));
00184         BlynkReq req = { pin };
00185         WidgetWriteHandler handler = GetWriteHandler(pin);
00186         if (handler && (handler != BlynkWidgetWrite)) {
00187             handler(req, param2);
00188         } else {
00189             BlynkWidgetWriteDefault(req, param2);
00190         }
00191     } break;
00192     default:
00193         BLYNK_LOG2(BLYNK_F("Invalid HW cmd: "), cmd);
00194         static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_RESPONSE, static_cast<Proto*>(this)->msgIdOutOverride, NULL, BLYNK_ILLEGAL_COMMAND);
00195     }
00196 }
00197 
00198 #endif