* AM2321的取温度间隔得大于2s,否则,i2c会不工作了 * SimpleTimer有个bug,会导致两次快速的读温度,现在读温度函数里加了保护 * Blynk有个bug,会导致无法把数据传到服务器 * 现在可以正常工作了
Blynk_v0_3_7/Blynk/BlynkApiMbed.h@1:e34100dd6532, 2016-06-24 (annotated)
- Committer:
- lixianyu
- Date:
- Fri Jun 24 02:06:43 2016 +0000
- Revision:
- 1:e34100dd6532
- Parent:
- 0:740c1eb2df13
?Arduino??????????0~255??????LPC824????????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lixianyu | 0:740c1eb2df13 | 1 | /** |
lixianyu | 0:740c1eb2df13 | 2 | * @file BlynkApiArduino.h |
lixianyu | 0:740c1eb2df13 | 3 | * @author Volodymyr Shymanskyy |
lixianyu | 0:740c1eb2df13 | 4 | * @license This project is released under the MIT License (MIT) |
lixianyu | 0:740c1eb2df13 | 5 | * @copyright Copyright (c) 2015 Volodymyr Shymanskyy |
lixianyu | 0:740c1eb2df13 | 6 | * @date Mar 2015 |
lixianyu | 0:740c1eb2df13 | 7 | * @brief |
lixianyu | 0:740c1eb2df13 | 8 | * |
lixianyu | 0:740c1eb2df13 | 9 | */ |
lixianyu | 0:740c1eb2df13 | 10 | |
lixianyu | 0:740c1eb2df13 | 11 | #ifndef BlynkApiMbed_1_h |
lixianyu | 0:740c1eb2df13 | 12 | #define BlynkApiMbed_1_h |
lixianyu | 0:740c1eb2df13 | 13 | |
lixianyu | 0:740c1eb2df13 | 14 | #include "mbed.h" |
lixianyu | 0:740c1eb2df13 | 15 | #include <Blynk/BlynkApi.h> |
lixianyu | 0:740c1eb2df13 | 16 | #include "microduino_util.h" |
lixianyu | 0:740c1eb2df13 | 17 | |
lixianyu | 0:740c1eb2df13 | 18 | Timer g_BlynkTimer; |
lixianyu | 0:740c1eb2df13 | 19 | extern Serial pc; |
lixianyu | 0:740c1eb2df13 | 20 | |
lixianyu | 0:740c1eb2df13 | 21 | template<class Proto> |
lixianyu | 0:740c1eb2df13 | 22 | void BlynkApi<Proto>::Init() |
lixianyu | 0:740c1eb2df13 | 23 | { |
lixianyu | 0:740c1eb2df13 | 24 | g_BlynkTimer.start(); |
lixianyu | 0:740c1eb2df13 | 25 | //pc.printf("Enter BlynkApi<Proto>::Init()\r\n"); |
lixianyu | 0:740c1eb2df13 | 26 | } |
lixianyu | 0:740c1eb2df13 | 27 | |
lixianyu | 0:740c1eb2df13 | 28 | template<class Proto> |
lixianyu | 0:740c1eb2df13 | 29 | BLYNK_FORCE_INLINE |
lixianyu | 0:740c1eb2df13 | 30 | millis_time_t BlynkApi<Proto>::getMillis() |
lixianyu | 0:740c1eb2df13 | 31 | { |
lixianyu | 0:740c1eb2df13 | 32 | // TODO: Remove workaround for Intel Curie |
lixianyu | 0:740c1eb2df13 | 33 | // https://forum.arduino.cc/index.php?topic=391836.0 |
lixianyu | 0:740c1eb2df13 | 34 | #ifdef ARDUINO_ARCH_ARC32 |
lixianyu | 0:740c1eb2df13 | 35 | noInterrupts(); |
lixianyu | 0:740c1eb2df13 | 36 | uint64_t t = millis(); |
lixianyu | 0:740c1eb2df13 | 37 | interrupts(); |
lixianyu | 0:740c1eb2df13 | 38 | return t; |
lixianyu | 0:740c1eb2df13 | 39 | #else |
lixianyu | 0:740c1eb2df13 | 40 | return g_BlynkTimer.read_ms(); |
lixianyu | 0:740c1eb2df13 | 41 | #endif |
lixianyu | 0:740c1eb2df13 | 42 | } |
lixianyu | 0:740c1eb2df13 | 43 | |
lixianyu | 0:740c1eb2df13 | 44 | #ifdef BLYNK_NO_INFO |
lixianyu | 0:740c1eb2df13 | 45 | ffff |
lixianyu | 0:740c1eb2df13 | 46 | template<class Proto> |
lixianyu | 0:740c1eb2df13 | 47 | BLYNK_FORCE_INLINE |
lixianyu | 0:740c1eb2df13 | 48 | void BlynkApi<Proto>::sendInfo() {} |
lixianyu | 0:740c1eb2df13 | 49 | |
lixianyu | 0:740c1eb2df13 | 50 | #else |
lixianyu | 0:740c1eb2df13 | 51 | |
lixianyu | 0:740c1eb2df13 | 52 | template<class Proto> |
lixianyu | 0:740c1eb2df13 | 53 | BLYNK_FORCE_INLINE |
lixianyu | 0:740c1eb2df13 | 54 | void BlynkApi<Proto>::sendInfo() |
lixianyu | 0:740c1eb2df13 | 55 | { |
lixianyu | 0:740c1eb2df13 | 56 | static const char profile[] BLYNK_PROGMEM = |
lixianyu | 0:740c1eb2df13 | 57 | BLYNK_PARAM_KV("ver" , BLYNK_VERSION) |
lixianyu | 0:740c1eb2df13 | 58 | BLYNK_PARAM_KV("h-beat" , TOSTRING(BLYNK_HEARTBEAT)) |
lixianyu | 0:740c1eb2df13 | 59 | BLYNK_PARAM_KV("buff-in", TOSTRING(BLYNK_MAX_READBYTES)) |
lixianyu | 0:740c1eb2df13 | 60 | #ifdef BLYNK_INFO_DEVICE |
lixianyu | 0:740c1eb2df13 | 61 | BLYNK_PARAM_KV("dev" , BLYNK_INFO_DEVICE) |
lixianyu | 0:740c1eb2df13 | 62 | #endif |
lixianyu | 0:740c1eb2df13 | 63 | #ifdef BLYNK_INFO_CPU |
lixianyu | 0:740c1eb2df13 | 64 | BLYNK_PARAM_KV("cpu" , BLYNK_INFO_CPU) |
lixianyu | 0:740c1eb2df13 | 65 | #endif |
lixianyu | 0:740c1eb2df13 | 66 | #ifdef BLYNK_INFO_CONNECTION |
lixianyu | 0:740c1eb2df13 | 67 | BLYNK_PARAM_KV("con" , BLYNK_INFO_CONNECTION) |
lixianyu | 0:740c1eb2df13 | 68 | #endif |
lixianyu | 0:740c1eb2df13 | 69 | BLYNK_PARAM_KV("build" , __DATE__ " " __TIME__) |
lixianyu | 0:740c1eb2df13 | 70 | ; |
lixianyu | 0:740c1eb2df13 | 71 | const size_t profile_len = sizeof(profile)-1; |
lixianyu | 0:740c1eb2df13 | 72 | |
lixianyu | 0:740c1eb2df13 | 73 | #ifdef BLYNK_HAS_PROGMEM |
lixianyu | 0:740c1eb2df13 | 74 | char mem[profile_len]; |
lixianyu | 0:740c1eb2df13 | 75 | memcpy_P(mem, profile, profile_len); |
lixianyu | 0:740c1eb2df13 | 76 | static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, mem, profile_len); |
lixianyu | 0:740c1eb2df13 | 77 | #else |
lixianyu | 0:740c1eb2df13 | 78 | static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, profile, profile_len); |
lixianyu | 0:740c1eb2df13 | 79 | #endif |
lixianyu | 0:740c1eb2df13 | 80 | return; |
lixianyu | 0:740c1eb2df13 | 81 | } |
lixianyu | 0:740c1eb2df13 | 82 | |
lixianyu | 0:740c1eb2df13 | 83 | #endif |
lixianyu | 0:740c1eb2df13 | 84 | |
lixianyu | 0:740c1eb2df13 | 85 | static gpio_t g_gpio; |
lixianyu | 0:740c1eb2df13 | 86 | static analogin_t _adc; |
lixianyu | 0:740c1eb2df13 | 87 | //dac_t _dac; |
lixianyu | 0:740c1eb2df13 | 88 | template<class Proto> |
lixianyu | 0:740c1eb2df13 | 89 | BLYNK_FORCE_INLINE |
lixianyu | 0:740c1eb2df13 | 90 | void BlynkApi<Proto>::processCmd(const void* buff, size_t len) |
lixianyu | 0:740c1eb2df13 | 91 | { |
lixianyu | 0:740c1eb2df13 | 92 | BlynkParam param((void*)buff, len); |
lixianyu | 0:740c1eb2df13 | 93 | BlynkParam::iterator it = param.begin(); |
lixianyu | 0:740c1eb2df13 | 94 | if (it >= param.end()) |
lixianyu | 0:740c1eb2df13 | 95 | return; |
lixianyu | 0:740c1eb2df13 | 96 | const char* cmd = it.asStr(); |
lixianyu | 0:740c1eb2df13 | 97 | #if defined(MPIDE) |
lixianyu | 0:740c1eb2df13 | 98 | uint16_t cmd16; |
lixianyu | 0:740c1eb2df13 | 99 | memcpy(&cmd16, cmd, sizeof(cmd16)); |
lixianyu | 0:740c1eb2df13 | 100 | #else |
lixianyu | 0:740c1eb2df13 | 101 | const uint16_t cmd16 = *(uint16_t*)cmd; |
lixianyu | 0:740c1eb2df13 | 102 | #endif |
lixianyu | 0:740c1eb2df13 | 103 | if (++it >= param.end()) |
lixianyu | 0:740c1eb2df13 | 104 | return; |
lixianyu | 0:740c1eb2df13 | 105 | |
lixianyu | 0:740c1eb2df13 | 106 | #if defined(analogInputToDigitalPin) |
lixianyu | 0:740c1eb2df13 | 107 | // Good! Analog pins can be referenced on this device by name. |
lixianyu | 0:740c1eb2df13 | 108 | const uint8_t pin = (it.asStr()[0] == 'A') ? |
lixianyu | 0:740c1eb2df13 | 109 | analogInputToDigitalPin(atoi(it.asStr()+1)) : |
lixianyu | 0:740c1eb2df13 | 110 | it.asInt(); |
lixianyu | 0:740c1eb2df13 | 111 | #else |
lixianyu | 0:740c1eb2df13 | 112 | #warning "analogInputToDigitalPin not defined => Named analog pins will not work" |
lixianyu | 0:740c1eb2df13 | 113 | //const uint8_t pin = it.asInt(); |
lixianyu | 0:740c1eb2df13 | 114 | const uint16_t pin = MUtil::getRealPin(it.asInt()); |
lixianyu | 0:740c1eb2df13 | 115 | const uint8_t pin1 = it.asInt(); |
lixianyu | 0:740c1eb2df13 | 116 | #endif |
lixianyu | 0:740c1eb2df13 | 117 | |
lixianyu | 0:740c1eb2df13 | 118 | switch(cmd16) { |
lixianyu | 0:740c1eb2df13 | 119 | #ifndef BLYNK_NO_BUILTIN |
lixianyu | 0:740c1eb2df13 | 120 | case BLYNK_HW_PM: { |
lixianyu | 0:740c1eb2df13 | 121 | while (it < param.end()) { |
lixianyu | 0:740c1eb2df13 | 122 | ++it; |
lixianyu | 0:740c1eb2df13 | 123 | if (!strcmp(it.asStr(), "in")) { |
lixianyu | 0:740c1eb2df13 | 124 | //pinMode(pin, INPUT); |
lixianyu | 0:740c1eb2df13 | 125 | gpio_init_in(&g_gpio, (PinName)pin); |
lixianyu | 0:740c1eb2df13 | 126 | } else if (!strcmp(it.asStr(), "out") || !strcmp(it.asStr(), "pwm")) { |
lixianyu | 0:740c1eb2df13 | 127 | //pinMode(pin, OUTPUT); |
lixianyu | 0:740c1eb2df13 | 128 | gpio_init_out(&g_gpio, (PinName)pin); |
lixianyu | 0:740c1eb2df13 | 129 | #ifdef INPUT_PULLUP |
lixianyu | 0:740c1eb2df13 | 130 | } else if (!strcmp(it.asStr(), "pu")) { |
lixianyu | 0:740c1eb2df13 | 131 | //pinMode(pin, INPUT_PULLUP); |
lixianyu | 0:740c1eb2df13 | 132 | gpio_init_in_ex(&g_gpio, pin, PullUp); |
lixianyu | 0:740c1eb2df13 | 133 | #endif |
lixianyu | 0:740c1eb2df13 | 134 | #ifdef INPUT_PULLDOWN |
lixianyu | 0:740c1eb2df13 | 135 | } else if (!strcmp(it.asStr(), "pd")) { |
lixianyu | 0:740c1eb2df13 | 136 | //pinMode(pin, INPUT_PULLDOWN); |
lixianyu | 0:740c1eb2df13 | 137 | gpio_init_in_ex(&g_gpio, pin, PullDown); |
lixianyu | 0:740c1eb2df13 | 138 | #endif |
lixianyu | 0:740c1eb2df13 | 139 | } else { |
lixianyu | 0:740c1eb2df13 | 140 | #ifdef BLYNK_DEBUG |
lixianyu | 0:740c1eb2df13 | 141 | BLYNK_LOG4(BLYNK_F("Invalid pin "), pin, BLYNK_F(" mode "), it.asStr()); |
lixianyu | 0:740c1eb2df13 | 142 | #endif |
lixianyu | 0:740c1eb2df13 | 143 | } |
lixianyu | 0:740c1eb2df13 | 144 | ++it; |
lixianyu | 0:740c1eb2df13 | 145 | } |
lixianyu | 0:740c1eb2df13 | 146 | } |
lixianyu | 0:740c1eb2df13 | 147 | break; |
lixianyu | 0:740c1eb2df13 | 148 | |
lixianyu | 0:740c1eb2df13 | 149 | case BLYNK_HW_DR: { |
lixianyu | 0:740c1eb2df13 | 150 | //DigitalIn p((PinName)pin); |
lixianyu | 0:740c1eb2df13 | 151 | char mem[16]; |
lixianyu | 0:740c1eb2df13 | 152 | BlynkParam rsp(mem, 0, sizeof(mem)); |
lixianyu | 0:740c1eb2df13 | 153 | rsp.add("dw"); |
lixianyu | 0:740c1eb2df13 | 154 | rsp.add(pin1); |
lixianyu | 0:740c1eb2df13 | 155 | //rsp.add(int(p)); |
lixianyu | 0:740c1eb2df13 | 156 | rsp.add(gpio_read(&g_gpio)); |
lixianyu | 0:740c1eb2df13 | 157 | static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1); |
lixianyu | 0:740c1eb2df13 | 158 | } |
lixianyu | 0:740c1eb2df13 | 159 | break; |
lixianyu | 0:740c1eb2df13 | 160 | |
lixianyu | 0:740c1eb2df13 | 161 | case BLYNK_HW_DW: { |
lixianyu | 0:740c1eb2df13 | 162 | // Should be 1 parameter (value) |
lixianyu | 0:740c1eb2df13 | 163 | if (++it >= param.end()) |
lixianyu | 0:740c1eb2df13 | 164 | return; |
lixianyu | 0:740c1eb2df13 | 165 | |
lixianyu | 0:740c1eb2df13 | 166 | #ifdef ESP8266 |
lixianyu | 0:740c1eb2df13 | 167 | // Disable PWM... |
lixianyu | 0:740c1eb2df13 | 168 | analogWrite(pin, 0); |
lixianyu | 0:740c1eb2df13 | 169 | #endif |
lixianyu | 0:740c1eb2df13 | 170 | #ifndef BLYNK_MINIMIZE_PINMODE_USAGE |
lixianyu | 0:740c1eb2df13 | 171 | //pinMode(pin, OUTPUT); |
lixianyu | 0:740c1eb2df13 | 172 | gpio_init_out(&g_gpio, (PinName)pin); |
lixianyu | 0:740c1eb2df13 | 173 | #endif |
lixianyu | 0:740c1eb2df13 | 174 | //digitalWrite(pin, it.asInt() ? HIGH : LOW); |
lixianyu | 0:740c1eb2df13 | 175 | gpio_write(&g_gpio, it.asInt() ? 1 : 0); |
lixianyu | 0:740c1eb2df13 | 176 | } |
lixianyu | 0:740c1eb2df13 | 177 | break; |
lixianyu | 0:740c1eb2df13 | 178 | |
lixianyu | 0:740c1eb2df13 | 179 | case BLYNK_HW_AR: { |
lixianyu | 0:740c1eb2df13 | 180 | analogin_init(&_adc, (PinName)pin); |
lixianyu | 0:740c1eb2df13 | 181 | char mem[16]; |
lixianyu | 0:740c1eb2df13 | 182 | BlynkParam rsp(mem, 0, sizeof(mem)); |
lixianyu | 0:740c1eb2df13 | 183 | rsp.add("aw"); |
lixianyu | 0:740c1eb2df13 | 184 | rsp.add(pin1); |
lixianyu | 0:740c1eb2df13 | 185 | //rsp.add(analogRead(pin)); |
lixianyu | 0:740c1eb2df13 | 186 | rsp.add(int(analogin_read(&_adc)*1024)); |
lixianyu | 0:740c1eb2df13 | 187 | static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1); |
lixianyu | 0:740c1eb2df13 | 188 | } |
lixianyu | 0:740c1eb2df13 | 189 | break; |
lixianyu | 0:740c1eb2df13 | 190 | |
lixianyu | 0:740c1eb2df13 | 191 | case BLYNK_HW_AW: { |
lixianyu | 0:740c1eb2df13 | 192 | #if 0 // TODO: Not supported yet |
lixianyu | 0:740c1eb2df13 | 193 | analogout_init(&_dac, pin); |
lixianyu | 0:740c1eb2df13 | 194 | // Should be 1 parameter (value) |
lixianyu | 0:740c1eb2df13 | 195 | if (++it >= param.end()) |
lixianyu | 0:740c1eb2df13 | 196 | return; |
lixianyu | 0:740c1eb2df13 | 197 | |
lixianyu | 0:740c1eb2df13 | 198 | #ifndef BLYNK_MINIMIZE_PINMODE_USAGE |
lixianyu | 0:740c1eb2df13 | 199 | //pinMode(pin, OUTPUT); |
lixianyu | 0:740c1eb2df13 | 200 | #endif |
lixianyu | 0:740c1eb2df13 | 201 | //analogWrite(pin, it.asInt()); |
lixianyu | 0:740c1eb2df13 | 202 | analogout_write(&_dac, float(it.asInt()/1024)); |
lixianyu | 0:740c1eb2df13 | 203 | #endif |
lixianyu | 0:740c1eb2df13 | 204 | } |
lixianyu | 0:740c1eb2df13 | 205 | break; |
lixianyu | 0:740c1eb2df13 | 206 | #endif |
lixianyu | 0:740c1eb2df13 | 207 | |
lixianyu | 0:740c1eb2df13 | 208 | case BLYNK_HW_VR: { |
lixianyu | 0:740c1eb2df13 | 209 | BlynkReq req = { pin }; |
lixianyu | 0:740c1eb2df13 | 210 | WidgetReadHandler handler = GetReadHandler(pin); |
lixianyu | 0:740c1eb2df13 | 211 | if (handler && (handler != BlynkWidgetRead)) { |
lixianyu | 0:740c1eb2df13 | 212 | handler(req); |
lixianyu | 0:740c1eb2df13 | 213 | } else { |
lixianyu | 0:740c1eb2df13 | 214 | BlynkWidgetReadDefault(req); |
lixianyu | 0:740c1eb2df13 | 215 | } |
lixianyu | 0:740c1eb2df13 | 216 | } |
lixianyu | 0:740c1eb2df13 | 217 | break; |
lixianyu | 0:740c1eb2df13 | 218 | |
lixianyu | 0:740c1eb2df13 | 219 | case BLYNK_HW_VW: { |
lixianyu | 0:740c1eb2df13 | 220 | ++it; |
lixianyu | 0:740c1eb2df13 | 221 | char* start = (char*)it.asStr(); |
lixianyu | 0:740c1eb2df13 | 222 | BlynkParam param2(start, len - (start - (char*)buff)); |
lixianyu | 0:740c1eb2df13 | 223 | BlynkReq req = { pin }; |
lixianyu | 0:740c1eb2df13 | 224 | WidgetWriteHandler handler = GetWriteHandler(pin); |
lixianyu | 0:740c1eb2df13 | 225 | if (handler && (handler != BlynkWidgetWrite)) { |
lixianyu | 0:740c1eb2df13 | 226 | handler(req, param2); |
lixianyu | 0:740c1eb2df13 | 227 | } else { |
lixianyu | 0:740c1eb2df13 | 228 | BlynkWidgetWriteDefault(req, param2); |
lixianyu | 0:740c1eb2df13 | 229 | } |
lixianyu | 0:740c1eb2df13 | 230 | } |
lixianyu | 0:740c1eb2df13 | 231 | break; |
lixianyu | 0:740c1eb2df13 | 232 | |
lixianyu | 0:740c1eb2df13 | 233 | default: |
lixianyu | 0:740c1eb2df13 | 234 | BLYNK_LOG2(BLYNK_F("Invalid HW cmd: "), cmd); |
lixianyu | 0:740c1eb2df13 | 235 | static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_RESPONSE, static_cast<Proto*>(this)->currentMsgId, NULL, BLYNK_ILLEGAL_COMMAND); |
lixianyu | 0:740c1eb2df13 | 236 | } |
lixianyu | 0:740c1eb2df13 | 237 | } |
lixianyu | 0:740c1eb2df13 | 238 | |
lixianyu | 0:740c1eb2df13 | 239 | #endif |
lixianyu | 0:740c1eb2df13 | 240 |