* AM2321的取温度间隔得大于2s,否则,i2c会不工作了 * SimpleTimer有个bug,会导致两次快速的读温度,现在读温度函数里加了保护 * Blynk有个bug,会导致无法把数据传到服务器 * 现在可以正常工作了

Dependencies:   mbed

Blynk_v0_3_7/Blynk/BlynkApiMbed.h

Committer:
lixianyu
Date:
2016-06-23
Revision:
0:740c1eb2df13

File content as of revision 0:740c1eb2df13:

/**
 * @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 BlynkApiMbed_1_h
#define BlynkApiMbed_1_h

#include "mbed.h"
#include <Blynk/BlynkApi.h>
#include "microduino_util.h"

Timer g_BlynkTimer;
extern Serial pc;

template<class Proto>
void BlynkApi<Proto>::Init()
{
    g_BlynkTimer.start();
    //pc.printf("Enter BlynkApi<Proto>::Init()\r\n");
}

template<class Proto>
BLYNK_FORCE_INLINE
millis_time_t BlynkApi<Proto>::getMillis()
{
// TODO: Remove workaround for Intel Curie
// https://forum.arduino.cc/index.php?topic=391836.0
#ifdef ARDUINO_ARCH_ARC32
    noInterrupts();
    uint64_t t = millis();
    interrupts();
    return t;
#else
    return g_BlynkTimer.read_ms();
#endif
}

#ifdef BLYNK_NO_INFO
ffff
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

static gpio_t g_gpio;
static analogin_t _adc;
//dac_t _dac;
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();
#if defined(MPIDE)
    uint16_t cmd16;
    memcpy(&cmd16, cmd, sizeof(cmd16));
#else
    const uint16_t cmd16 = *(uint16_t*)cmd;
#endif
    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
#warning "analogInputToDigitalPin not defined => Named analog pins will not work"
    //const uint8_t pin = it.asInt();
    const uint16_t pin = MUtil::getRealPin(it.asInt());
    const uint8_t pin1 = 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);
                    gpio_init_in(&g_gpio, (PinName)pin);
                } else if (!strcmp(it.asStr(), "out") || !strcmp(it.asStr(), "pwm")) {
                    //pinMode(pin, OUTPUT);
                    gpio_init_out(&g_gpio, (PinName)pin);
#ifdef INPUT_PULLUP
                } else if (!strcmp(it.asStr(), "pu")) {
                    //pinMode(pin, INPUT_PULLUP);
                    gpio_init_in_ex(&g_gpio, pin, PullUp);
#endif
#ifdef INPUT_PULLDOWN
                } else if (!strcmp(it.asStr(), "pd")) {
                    //pinMode(pin, INPUT_PULLDOWN);
                    gpio_init_in_ex(&g_gpio, pin, PullDown);
#endif
                } 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(pin1);
            //rsp.add(int(p));
            rsp.add(gpio_read(&g_gpio));
            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;

#ifdef ESP8266
            // Disable PWM...
            analogWrite(pin, 0);
#endif
#ifndef BLYNK_MINIMIZE_PINMODE_USAGE
            //pinMode(pin, OUTPUT);
            gpio_init_out(&g_gpio, (PinName)pin);
#endif
            //digitalWrite(pin, it.asInt() ? HIGH : LOW);
            gpio_write(&g_gpio, it.asInt() ? 1 : 0);
        }
        break;
        
        case BLYNK_HW_AR: {
            analogin_init(&_adc, (PinName)pin);
            char mem[16];
            BlynkParam rsp(mem, 0, sizeof(mem));
            rsp.add("aw");
            rsp.add(pin1);
            //rsp.add(analogRead(pin));
            rsp.add(int(analogin_read(&_adc)*1024));
            static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
        }
        break;
        
        case BLYNK_HW_AW: {
            #if 0 // TODO: Not supported yet
            analogout_init(&_dac, pin);
            // Should be 1 parameter (value)
            if (++it >= param.end())
                return;

#ifndef BLYNK_MINIMIZE_PINMODE_USAGE
            //pinMode(pin, OUTPUT);
#endif
            //analogWrite(pin, it.asInt());
            analogout_write(&_dac, float(it.asInt()/1024));
            #endif
        }
        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