p kj
/
LPC824-BlynkWeatherstation
Microduino
Fork of Io_moon by
Diff: Blynk_v0_3_7/Blynk/BlynkApiMbed2.h
- Revision:
- 0:740c1eb2df13
diff -r 000000000000 -r 740c1eb2df13 Blynk_v0_3_7/Blynk/BlynkApiMbed2.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Blynk_v0_3_7/Blynk/BlynkApiMbed2.h Thu Jun 23 11:16:14 2016 +0000 @@ -0,0 +1,195 @@ +/** + * @file BlynkApiArduino.h + * @author Volodymyr Shymanskyy + * @license This project is released under the MIT License (MIT) + * @copyright Copyright (c) 2015 Volodymyr Shymanskyy + * @date Mar 2015 + * @brief + * + */ + +#ifndef BlynkApiArduino_h +#define BlynkApiArduino_h + +#include "mbed.h" +#include <Blynk/BlynkApi.h> + + +static Timer blynk_millis_timer; +static Ticker blynk_waker; + +static +void blynk_wake() { + //pc.puts("(...)"); +} + +static +void delay(unsigned long ms) +{ + wait_ms(ms); +} + +static +unsigned long millis(void) +{ + return blynk_millis_timer.read_ms(); +} + +template<class Proto> +void BlynkApi<Proto>::Init() +{ + blynk_waker.attach(&blynk_wake, 2.0); + blynk_millis_timer.start(); +} + +template<class Proto> +BLYNK_FORCE_INLINE +millis_time_t BlynkApi<Proto>::getMillis() +{ + return blynk_millis_timer.read_ms(); +} + +#ifdef BLYNK_NO_INFO + +template<class Proto> +BLYNK_FORCE_INLINE +void BlynkApi<Proto>::sendInfo() {} + +#else + +template<class Proto> +BLYNK_FORCE_INLINE +void BlynkApi<Proto>::sendInfo() +{ + static const char profile[] BLYNK_PROGMEM = + BLYNK_PARAM_KV("ver" , BLYNK_VERSION) + BLYNK_PARAM_KV("h-beat" , TOSTRING(BLYNK_HEARTBEAT)) + BLYNK_PARAM_KV("buff-in", TOSTRING(BLYNK_MAX_READBYTES)) +#ifdef BLYNK_INFO_DEVICE + BLYNK_PARAM_KV("dev" , BLYNK_INFO_DEVICE) +#endif +#ifdef BLYNK_INFO_CPU + BLYNK_PARAM_KV("cpu" , BLYNK_INFO_CPU) +#endif +#ifdef BLYNK_INFO_CONNECTION + BLYNK_PARAM_KV("con" , BLYNK_INFO_CONNECTION) +#endif + BLYNK_PARAM_KV("build" , __DATE__ " " __TIME__) + ; + const size_t profile_len = sizeof(profile)-1; + +#ifdef BLYNK_HAS_PROGMEM + char mem[profile_len]; + memcpy_P(mem, profile, profile_len); + static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, mem, profile_len); +#else + static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, profile, profile_len); +#endif + return; +} + +#endif + +template<class Proto> +BLYNK_FORCE_INLINE +void BlynkApi<Proto>::processCmd(const void* buff, size_t len) +{ + BlynkParam param((void*)buff, len); + BlynkParam::iterator it = param.begin(); + if (it >= param.end()) + return; + const char* cmd = it.asStr(); + const uint16_t cmd16 = *(uint16_t*)cmd; + + if (++it >= param.end()) + return; + +#if defined(analogInputToDigitalPin) + // Good! Analog pins can be referenced on this device by name. + const uint8_t pin = (it.asStr()[0] == 'A') ? + analogInputToDigitalPin(atoi(it.asStr()+1)) : + it.asInt(); +#else + const uint8_t pin = it.asInt(); +#endif + + switch(cmd16) { + +#ifndef BLYNK_NO_BUILTIN + + case BLYNK_HW_PM: { + while (it < param.end()) { + ++it; + if (!strcmp(it.asStr(), "in")) { + //pinMode(pin, INPUT); + } else if (!strcmp(it.asStr(), "out") || !strcmp(it.asStr(), "pwm")) { + //pinMode(pin, OUTPUT); + } else { +#ifdef BLYNK_DEBUG + BLYNK_LOG4(BLYNK_F("Invalid pin "), pin, BLYNK_F(" mode "), it.asStr()); +#endif + } + ++it; + } + } break; + case BLYNK_HW_DR: { + DigitalIn p((PinName)pin); + char mem[16]; + BlynkParam rsp(mem, 0, sizeof(mem)); + rsp.add("dw"); + rsp.add(pin); + rsp.add(int(p)); + static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1); + } break; + case BLYNK_HW_DW: { + // Should be 1 parameter (value) + if (++it >= param.end()) + return; + + //BLYNK_LOG("digitalWrite %d -> %d", pin, it.asInt()); + DigitalOut p((PinName)pin); + p = it.asInt() ? 1 : 0; + } break; + case BLYNK_HW_AR: { + AnalogIn p((PinName)pin); + char mem[16]; + BlynkParam rsp(mem, 0, sizeof(mem)); + rsp.add("aw"); + rsp.add(pin); + rsp.add(int(p.read() * 1024)); + static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1); + } break; + case BLYNK_HW_AW: { + // TODO: Not supported yet + } break; + +#endif + + case BLYNK_HW_VR: { + BlynkReq req = { pin }; + WidgetReadHandler handler = GetReadHandler(pin); + if (handler && (handler != BlynkWidgetRead)) { + handler(req); + } else { + BlynkWidgetReadDefault(req); + } + } break; + case BLYNK_HW_VW: { + ++it; + char* start = (char*)it.asStr(); + BlynkParam param2(start, len - (start - (char*)buff)); + BlynkReq req = { pin }; + WidgetWriteHandler handler = GetWriteHandler(pin); + if (handler && (handler != BlynkWidgetWrite)) { + handler(req, param2); + } else { + BlynkWidgetWriteDefault(req, param2); + } + } break; + default: + BLYNK_LOG2(BLYNK_F("Invalid HW cmd: "), cmd); + static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_RESPONSE, static_cast<Proto*>(this)->currentMsgId, NULL, BLYNK_ILLEGAL_COMMAND); + } +} + +#endif