Added blynk application related files
parent
2ce32a7f1e
commit
c5710b0453
@ -0,0 +1,851 @@
|
||||
/**************************************************************
|
||||
* < Simple Blynk library for WIZnet products >
|
||||
*
|
||||
* WIZnet official website: http://www.wiznet.co.kr
|
||||
* WIZnet Museum http://www.wiznetmuseum.com
|
||||
* WIZnet Wiki http://wizwiki.net
|
||||
* WIZnet Forum http://wizwiki.net/forum
|
||||
*
|
||||
* Downloads, docs, tutorials: http://www.blynk.cc
|
||||
* Blynk community: http://community.blynk.cc
|
||||
* Social groups: http://www.fb.com/blynkapp
|
||||
* http://twitter.com/blynk_app
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "socket.h"
|
||||
|
||||
#include "blynk.h"
|
||||
#include "blynkDependency.h"
|
||||
#include "../globals.h"
|
||||
|
||||
//#include "common.h" // When the project has no "common.h" file, this line have to commented out.
|
||||
#ifndef BLYNK_DATA_BUF_SIZE
|
||||
#define BLYNK_DATA_BUF_SIZE 2048
|
||||
#endif
|
||||
|
||||
uint8_t blynk_connect(void);
|
||||
void processInput(void);
|
||||
void processCmd(uint8_t * buff, size_t len);
|
||||
uint8_t readHeader(BlynkHeader * hdr);
|
||||
void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t * data2, size_t length2);
|
||||
|
||||
uint16_t getNextMsgId(void);
|
||||
void BlynkAverageSample (uint32_t * avg, const uint32_t input, uint8_t n);
|
||||
//uint32_t millis(void);
|
||||
uint8_t blynk_custom_delay(uint32_t delayms);
|
||||
|
||||
void blynkparam_init(BlynkParam p);
|
||||
uint8_t * blynkparam_get(void);
|
||||
|
||||
// Util functions
|
||||
static uint16_t ATOI(uint8_t * str, uint8_t base);
|
||||
static uint8_t C2D(uint8_t c);
|
||||
static void replacetonull(uint8_t * str, uint8_t c);
|
||||
|
||||
static void printBanner();
|
||||
|
||||
uint8_t * authkey;
|
||||
uint32_t lastActivityIn;
|
||||
uint32_t lastActivityOut;
|
||||
uint32_t lastHeartbeat;
|
||||
#ifdef BLYNK_MSG_LIMIT
|
||||
uint32_t deltaCmd;
|
||||
#endif
|
||||
uint16_t currentMsgId;
|
||||
|
||||
uint8_t blynk_connected = 0;
|
||||
uint8_t blynk_connection_available = 0;
|
||||
uint32_t ptime = 0;
|
||||
|
||||
// Init variables
|
||||
uint8_t blynk_socket, s;
|
||||
uint8_t * server_ip;
|
||||
uint16_t server_port;
|
||||
uint8_t * msgbuf;
|
||||
|
||||
// variables for parameter parsing
|
||||
uint8_t * param_ptr;
|
||||
uint8_t * param_end;
|
||||
|
||||
volatile uint32_t blynk_time_1ms;
|
||||
uint16_t blynkclient_port = BLYNK_DEFAULT_CLIENT_PORT;
|
||||
uint8_t flag_blynkinit_complete = 0;
|
||||
|
||||
void blynk_begin(uint8_t * auth, uint8_t * dest_ip, uint16_t dest_port, uint8_t * buf, uint8_t socket)
|
||||
{
|
||||
s = socket;
|
||||
|
||||
authkey = auth;
|
||||
server_ip = dest_ip;
|
||||
server_port = dest_port;
|
||||
msgbuf = buf;
|
||||
|
||||
flag_blynkinit_complete = 1;
|
||||
}
|
||||
|
||||
uint8_t blynk_connection_try = 0;
|
||||
|
||||
void blynk_run(void)
|
||||
{
|
||||
uint32_t t;
|
||||
|
||||
#ifdef BLYNK_DEBUG
|
||||
uint8_t destip[4];
|
||||
uint16_t destport;
|
||||
#endif
|
||||
|
||||
if(!flag_blynkinit_complete) return;
|
||||
|
||||
switch(getSn_SR(s))
|
||||
{
|
||||
case SOCK_ESTABLISHED:
|
||||
// Interrupt clear
|
||||
if(getSn_IR(s) & Sn_IR_CON)
|
||||
{
|
||||
setSn_IR(s, Sn_IR_CON);
|
||||
|
||||
#ifdef BLYNK_DEBUG
|
||||
getSn_DIPR(s, destip);
|
||||
destport = getSn_DPORT(s);
|
||||
PRINTF("Blynk[%d] : Connected - %d.%d.%d.%d:%d\r\n",s, destip[0], destip[1], destip[2], destip[3], destport);
|
||||
#endif
|
||||
}
|
||||
|
||||
if(!blynk_connected)
|
||||
{
|
||||
if(!(blynk_connection_available = blynk_connect())) return;
|
||||
else
|
||||
{
|
||||
blynk_connected = true;
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("Blynk[%d] : Auth connection complete\r\n", s);
|
||||
#endif
|
||||
printBanner();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(blynk_connection_available > 0) processInput();
|
||||
|
||||
t = millis();
|
||||
|
||||
if (t - lastActivityIn > (1000UL * BLYNK_HEARTBEAT + BLYNK_TIMEOUT_MS*3)) {
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("Heartbeat timeout (last in: %lu)\r\n", lastActivityIn);
|
||||
#else
|
||||
PRINTF("Heartbeat timeout\r\n");
|
||||
#endif
|
||||
blynk_connected = false;
|
||||
blynk_connection_available = false;
|
||||
disconnect(s);
|
||||
}
|
||||
else if (( t - lastActivityIn > 1000UL * BLYNK_HEARTBEAT ||
|
||||
t - lastActivityOut > 1000UL * BLYNK_HEARTBEAT) &&
|
||||
t - lastHeartbeat > BLYNK_TIMEOUT_MS)
|
||||
{
|
||||
// Send ping if we didn't both send and receive something for BLYNK_HEARTBEAT seconds
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("Heartbeat\r\n");
|
||||
#endif
|
||||
sendCmd(BLYNK_CMD_PING, 0, NULL, 0, NULL, 0);
|
||||
lastHeartbeat = t;
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCK_CLOSE_WAIT:
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("Blynk[%d] : ClOSE WAIT\r\n", s); // if a peer requests to close the current connection
|
||||
#endif
|
||||
disconnect(s);
|
||||
break;
|
||||
|
||||
case SOCK_CLOSED:
|
||||
#ifdef BLYNK_DEBUG
|
||||
//PRINTF("> Blynk[%d] : CLOSED\r\n", s);
|
||||
#endif
|
||||
blynk_connected = false;
|
||||
blynk_connection_available = false;
|
||||
|
||||
if(socket(s, Sn_MR_TCP, blynkclient_port++, 0x00) == s) /* Reinitialize the socket */
|
||||
{
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("Blynk[%d] : SOCKET OPEN\r\n", s);
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCK_INIT:
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("Blynk[%d] : Connecting to ", s);
|
||||
PRINTF("%d.%d.%d.%d:%d\r\n", server_ip[0], server_ip[1], server_ip[2], server_ip[3], server_port);
|
||||
#endif
|
||||
connect(s, server_ip, server_port);
|
||||
break;
|
||||
|
||||
default :
|
||||
break;
|
||||
|
||||
} // end of switch
|
||||
}
|
||||
|
||||
|
||||
uint8_t blynk_connect(void)
|
||||
{
|
||||
BlynkHeader hdr;
|
||||
static uint16_t id = 0;
|
||||
uint8_t i;
|
||||
uint8_t hsize = 0;
|
||||
//#ifdef BLYNK_DEBUG
|
||||
uint32_t t = millis();
|
||||
//#endif
|
||||
|
||||
// changed parts
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
static uint8_t cmd_sent = false;
|
||||
|
||||
if(blynk_custom_delay(0)) return false;
|
||||
|
||||
if(!cmd_sent)
|
||||
{
|
||||
id = getNextMsgId();
|
||||
sendCmd(BLYNK_CMD_LOGIN, id, authkey, strlen((char *)authkey), NULL, 0);
|
||||
cmd_sent = true;
|
||||
ptime = millis(); // for check connection timeout
|
||||
}
|
||||
|
||||
if(millis() < (ptime + BLYNK_CONNECTION_TIMEOUT_MS)) // wait data received during 5sec before connection timeout occur
|
||||
{
|
||||
if(!readHeader(&hdr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else // Auth response received
|
||||
{
|
||||
cmd_sent = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
hdr.length = BLYNK_TIMEOUT;
|
||||
cmd_sent = false;
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
if (BLYNK_CMD_RESPONSE != hdr.type ||
|
||||
id != hdr.msg_id ||
|
||||
//(BLYNK_SUCCESS != hdr.length && BLYNK_ALREADY_LOGGED_IN != hdr.length)) Deprecated on BLYNK_VERSION 0.6.0
|
||||
(BLYNK_SUCCESS != hdr.length && BLYNK_ILLEGAL_COMMAND_BODY != hdr.length))
|
||||
{
|
||||
if (BLYNK_TIMEOUT == hdr.length)
|
||||
{
|
||||
PRINTF("Timeout\r\n");
|
||||
}
|
||||
else if (BLYNK_INVALID_TOKEN == hdr.length)
|
||||
{
|
||||
PRINTF("Invalid auth token\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
PRINTF("Connect failed (code: %d)\r\n", hdr.length);
|
||||
|
||||
// Send some invalid headers to server for disconnection
|
||||
hdr.type = 255;
|
||||
hdr.msg_id = 0;
|
||||
hdr.length = 0;
|
||||
|
||||
// problem fixed for header structure size
|
||||
hsize = 0;
|
||||
msgbuf[hsize++] = hdr.type;
|
||||
msgbuf[hsize++] = hdr.msg_id;
|
||||
msgbuf[hsize++] = hdr.msg_id;
|
||||
msgbuf[hsize++] = hdr.length;
|
||||
msgbuf[hsize++] = hdr.length;
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
send(s, msgbuf, hsize);
|
||||
}
|
||||
}
|
||||
disconnect(s);
|
||||
// old delay function removed
|
||||
blynk_custom_delay(5000);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
lastHeartbeat = lastActivityIn = lastActivityOut = millis();
|
||||
#ifdef BLYNK_MSG_LIMIT
|
||||
deltaCmd = 1000;
|
||||
#endif
|
||||
|
||||
PRINTF("Ready!\r\n");
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("Roundtrip: %ldms\r\n", lastActivityIn-t);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void processInput(void)
|
||||
{
|
||||
BlynkHeader hdr;
|
||||
#ifdef BLYNK_DEBUG
|
||||
uint16_t i;
|
||||
#endif
|
||||
|
||||
if (!readHeader(&hdr)) return;
|
||||
|
||||
switch (hdr.type)
|
||||
{
|
||||
case BLYNK_CMD_RESPONSE:
|
||||
{
|
||||
if (BLYNK_NOT_AUTHENTICATED == hdr.length)
|
||||
{
|
||||
disconnect(s);
|
||||
return;
|
||||
}
|
||||
// TODO: return code may indicate App presence
|
||||
} break;
|
||||
|
||||
case BLYNK_CMD_PING:
|
||||
{
|
||||
sendCmd(BLYNK_CMD_RESPONSE, hdr.msg_id, NULL, BLYNK_SUCCESS, NULL, 0);
|
||||
} break;
|
||||
|
||||
case BLYNK_CMD_HARDWARE:
|
||||
case BLYNK_CMD_BRIDGE:
|
||||
{
|
||||
if (hdr.length > BLYNK_MAX_READBYTES)
|
||||
{
|
||||
PRINTF("Packet size (%u) > max allowed (%u)\r\n", hdr.length, BLYNK_MAX_READBYTES);
|
||||
disconnect(s);
|
||||
return;
|
||||
}
|
||||
|
||||
//PRINTF("hdr.length = %d\r\n", hdr.length);
|
||||
if (hdr.length != recv(s, msgbuf, hdr.length))
|
||||
{
|
||||
PRINTF("Can't read body\r\n");
|
||||
return;
|
||||
}
|
||||
msgbuf[hdr.length] = '\0'; // Add 1 to zero-terminate
|
||||
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF(">");
|
||||
for(i = 0; i < hdr.length; i++)
|
||||
{
|
||||
if(msgbuf[i] != '\0') PRINTF("%c", msgbuf[i]);
|
||||
else PRINTF(" ");
|
||||
}
|
||||
PRINTF("\r\n");
|
||||
#endif
|
||||
|
||||
currentMsgId = hdr.msg_id;
|
||||
processCmd(msgbuf, hdr.length);
|
||||
currentMsgId = 0;
|
||||
} break;
|
||||
//!! On BLYNK_VERSION 0.6.0 also present (look BlynkProtocol.h bool BlynkProtocol<Transp>::processInput(void)):
|
||||
/*
|
||||
*
|
||||
case BLYNK_CMD_LOGIN: {..
|
||||
|
||||
case BLYNK_CMD_REDIRECT: {..
|
||||
|
||||
case BLYNK_CMD_INTERNAL: {..
|
||||
|
||||
case BLYNK_CMD_DEBUG_PRINT: {..
|
||||
*/
|
||||
default:
|
||||
PRINTF("Invalid header type: %d\r\n", hdr.type);
|
||||
disconnect(s);
|
||||
return;
|
||||
}
|
||||
|
||||
lastActivityIn = millis();
|
||||
}
|
||||
|
||||
|
||||
void blynkparam_init(BlynkParam p)
|
||||
{
|
||||
param_ptr = p.buff;
|
||||
param_end = param_ptr + p.len;
|
||||
}
|
||||
|
||||
|
||||
uint8_t * blynkparam_get(void)
|
||||
{
|
||||
uint8_t size;
|
||||
uint8_t * ret_ptr = param_ptr;
|
||||
|
||||
if(param_ptr >= param_end) return NULL;
|
||||
|
||||
size = strlen((char *)param_ptr);
|
||||
param_ptr += (size+1);
|
||||
|
||||
return ret_ptr;
|
||||
}
|
||||
|
||||
void processCmd(uint8_t * buff, size_t len)
|
||||
{
|
||||
uint8_t * nexttok;
|
||||
const char * cmd;
|
||||
uint8_t pin;
|
||||
uint8_t rsp_mem[16];
|
||||
uint16_t rsp_len;
|
||||
uint16_t w_param;
|
||||
|
||||
BlynkParam param;
|
||||
param.buff = buff;
|
||||
param.len = len;
|
||||
|
||||
// for virtual read / write functions
|
||||
//BlynkParam param2;
|
||||
//uint8_t * start;
|
||||
|
||||
memset(rsp_mem, 0, sizeof(rsp_mem));
|
||||
|
||||
blynkparam_init(param);
|
||||
|
||||
nexttok = blynkparam_get();
|
||||
if(!nexttok) return;
|
||||
|
||||
cmd = (char *)nexttok;
|
||||
|
||||
if(!strcmp(cmd, "info"))
|
||||
{
|
||||
static uint8_t profile[] =
|
||||
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
|
||||
#ifdef BOARD_FIRMWARE_VERSION
|
||||
BLYNK_PARAM_KV("fw" , BOARD_FIRMWARE_VERSION)
|
||||
#endif
|
||||
BLYNK_PARAM_KV("build" , __DATE__ " " __TIME__)
|
||||
"\0"
|
||||
;
|
||||
const size_t profile_len = sizeof(profile)-1;
|
||||
|
||||
sendCmd(BLYNK_CMD_HARDWARE, 0, profile, profile_len, NULL, 0);
|
||||
}
|
||||
|
||||
nexttok = blynkparam_get();
|
||||
if(!nexttok) return;
|
||||
|
||||
pin = (uint8_t)ATOI((uint8_t *)nexttok, 10);
|
||||
|
||||
if(!strcmp(cmd, "dr")) // digital pin read
|
||||
{
|
||||
//This is bug on LPc13xx original sources, last space symbol is unnecessary
|
||||
//rsp_len = SPRINTF((char *)rsp_mem, "dw %d %d ", pin, digitalRead(pin));
|
||||
rsp_len = SPRINTF((char *)rsp_mem, "dw %d %d", pin, digitalRead(pin));
|
||||
replacetonull(rsp_mem, ' ');
|
||||
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||
}
|
||||
else if(!strcmp(cmd, "ar")) // analog pin read
|
||||
{
|
||||
//This is bug on LPc13xx original sources, last space symbol is unnecessary (as I think..)
|
||||
//rsp_len = SPRINTF((char *)rsp_mem, "aw %d %d ", pin, analogRead(pin));
|
||||
rsp_len = SPRINTF((char *)rsp_mem, "aw %d %d", pin, analogRead(pin));
|
||||
replacetonull(rsp_mem, ' ');
|
||||
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||
}
|
||||
else if(!strcmp(cmd, "vr")) // virtual pin read
|
||||
{
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("vr command: Not fully supported yet\r\n");
|
||||
#endif
|
||||
rsp_len = SPRINTF((char *)rsp_mem, "vw %d %d", pin, virtualRead(pin));
|
||||
replacetonull(rsp_mem, ' ');
|
||||
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||
|
||||
/*
|
||||
WidgetReadHandler handler;
|
||||
handler = GetReadHandler(pin);
|
||||
if(handler)
|
||||
{
|
||||
BlynkReq req = { 0, BLYNK_SUCCESS, (uint8_t)pin };
|
||||
handler(req);
|
||||
}
|
||||
*/
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!strcmp(cmd, "vw")) // virtual pin write
|
||||
{
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("vw command: Not fully supported yet\r\n");
|
||||
#endif
|
||||
nexttok = blynkparam_get();
|
||||
w_param = ATOI((uint8_t *)nexttok, 10);
|
||||
virtualWrite(pin, w_param);
|
||||
|
||||
// update widget state
|
||||
//rsp_len = SPRINTF((char *)rsp_mem, "vw %d %d ", pin, w_param);
|
||||
//replacetonull(rsp_mem, ' ');
|
||||
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||
|
||||
/*
|
||||
WidgetWriteHandler handler;
|
||||
handler = GetWriteHandler(pin);
|
||||
if(handler)
|
||||
{
|
||||
BlynkReq req = { 0, BLYNK_SUCCESS, (uint8_t)pin };
|
||||
|
||||
nexttok = blynkparam_get();
|
||||
start = nexttok;
|
||||
|
||||
param2.buff = start;
|
||||
param2.len = len - (start - buff);
|
||||
handler(req, param2);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
if(!strcmp(cmd, "pm")) // pin mode setting
|
||||
{
|
||||
while(nexttok) // end condition: nexttok == NULL
|
||||
{
|
||||
nexttok = blynkparam_get();
|
||||
|
||||
if (!strcmp((char *)nexttok, "in")) {
|
||||
pinMode(pin, INPUT);
|
||||
} else if (!strcmp((char *)nexttok, "out") || !strcmp((char *)nexttok, "pwm")) {
|
||||
pinMode(pin, OUTPUT);
|
||||
} else if (!strcmp((char *)nexttok, "pu")) {
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
} else {
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("Invalid pinMode %u -> %s\r\n", pin, nexttok);
|
||||
#endif
|
||||
}
|
||||
nexttok = blynkparam_get();
|
||||
if(!nexttok) {break;}
|
||||
|
||||
pin = (uint8_t)ATOI((uint8_t *)nexttok, 10); // pin info update
|
||||
}
|
||||
}
|
||||
|
||||
nexttok = blynkparam_get();
|
||||
if(!nexttok) return;
|
||||
|
||||
// Should be 1 parameter (value)
|
||||
if(!strcmp(cmd, "dw")) // digital pin write
|
||||
{
|
||||
w_param = (uint8_t)ATOI((uint8_t *)nexttok, 10);
|
||||
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, w_param ? HIGH : LOW);
|
||||
|
||||
// update widget state
|
||||
//rsp_len = SPRINTF((char *)rsp_mem, "dw %d %d ", pin, digitalRead(pin));
|
||||
//replacetonull(rsp_mem, ' ');
|
||||
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||
}
|
||||
else if(!strcmp(cmd, "aw")) // analog pin write
|
||||
{
|
||||
w_param = (uint8_t)ATOI((uint8_t *)nexttok, 10);
|
||||
|
||||
analogWrite(pin, w_param);
|
||||
|
||||
// update widget state
|
||||
//rsp_len = SPRINTF((char *)rsp_mem, "aw %d %d ", pin, digitalRead(pin));
|
||||
//replacetonull(rsp_mem, ' ');
|
||||
//sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
PRINTF("Invalid HW cmd: %s\r\n", cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t readHeader(BlynkHeader * hdr)
|
||||
{
|
||||
uint16_t len;
|
||||
uint8_t hsize = 0;
|
||||
|
||||
if((len = getSn_RX_RSR(s)) > 0)
|
||||
{
|
||||
#ifdef BLYNK_DEBUG
|
||||
//PRINTF("recv header len = %d\r\n", len);
|
||||
#endif
|
||||
if(BLINK_HEADER_SIZE != recv(s, msgbuf, BLINK_HEADER_SIZE))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// problem fixed for header structure size
|
||||
hdr->type = msgbuf[hsize++];
|
||||
hdr->msg_id = (uint16_t)msgbuf[hsize++];
|
||||
hdr->msg_id |= ((uint16_t)msgbuf[hsize++]) << 8;
|
||||
hdr->length = (uint16_t)msgbuf[hsize++];
|
||||
hdr->length |= ((uint16_t)msgbuf[hsize++]) << 8;
|
||||
|
||||
hdr->msg_id = ntohs(hdr->msg_id);
|
||||
hdr->length = ntohs(hdr->length);
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF(">msg %d,%u,%u\r\n", hdr->type, hdr->msg_id, hdr->length);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t * data2, size_t length2)
|
||||
{
|
||||
BlynkHeader hdr;
|
||||
size_t wlen = 0;
|
||||
uint32_t ts;
|
||||
|
||||
uint8_t hsize = 0;
|
||||
|
||||
#ifdef BLYNK_DEBUG
|
||||
uint16_t i;
|
||||
#endif
|
||||
|
||||
if(getSn_SR(s) != SOCK_ESTABLISHED)
|
||||
{
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("Cmd not sent\r\n");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
if (0 == id) {
|
||||
id = getNextMsgId();
|
||||
}
|
||||
|
||||
hdr.type = cmd;
|
||||
hdr.msg_id = htons(id);
|
||||
hdr.length = htons(length+length2);
|
||||
|
||||
// problem fixed for header structure size
|
||||
msgbuf[hsize++] = hdr.type;
|
||||
msgbuf[hsize++] = (uint8_t)(0x00ff & hdr.msg_id);
|
||||
msgbuf[hsize++] = (uint8_t)((0xff00 & hdr.msg_id) >> 8);
|
||||
msgbuf[hsize++] = (uint8_t)(0x00ff & hdr.length);
|
||||
msgbuf[hsize++] = (uint8_t)((0xff00 & hdr.length) >> 8);
|
||||
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("<msg %d,%u,%u\r\n", cmd, id, length+length2);
|
||||
#endif
|
||||
|
||||
wlen += (size_t)send(s, msgbuf, hsize);
|
||||
|
||||
if (cmd != BLYNK_CMD_RESPONSE) {
|
||||
if (length) {
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("<");
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
if(data[i] != '\0') PRINTF("%c", data[i]);
|
||||
else PRINTF(" ");
|
||||
}
|
||||
PRINTF("\r\n");
|
||||
#endif
|
||||
wlen += (size_t)send(s, (uint8_t *)data, length);
|
||||
}
|
||||
if (length2) {
|
||||
#ifdef BLYNK_DEBUG
|
||||
PRINTF("<");
|
||||
for(i = 0; i < length2; i++)
|
||||
{
|
||||
if(data2[i] != '\0') PRINTF("%c", data2[i]);
|
||||
else PRINTF(" ");
|
||||
}
|
||||
PRINTF("\r\n");
|
||||
#endif
|
||||
wlen += (size_t)send(s, (uint8_t *)data2, length2);
|
||||
}
|
||||
if (wlen != hsize+length+length2) {
|
||||
PRINTF("Sent %u/%u\r\n", wlen, hsize+length+length2);
|
||||
disconnect(s);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ts = millis();
|
||||
#ifdef BLYNK_MSG_LIMIT
|
||||
BlynkAverageSample(&deltaCmd, ts - lastActivityOut, 10);
|
||||
lastActivityOut = ts;
|
||||
if (deltaCmd < (1000/BLYNK_MSG_LIMIT)) {
|
||||
//::delay(5000);
|
||||
PRINTF("Flood\r\n");
|
||||
disconnect(s);
|
||||
}
|
||||
#else
|
||||
lastActivityOut = ts;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
uint16_t getNextMsgId(void)
|
||||
{
|
||||
static uint16_t last = 0;
|
||||
if (currentMsgId != 0)
|
||||
return currentMsgId;
|
||||
if (++last == 0)
|
||||
last = 1;
|
||||
return last;
|
||||
}
|
||||
|
||||
|
||||
void BlynkAverageSample (uint32_t * avg, const uint32_t input, uint8_t n)
|
||||
{
|
||||
* avg -= * avg/n;
|
||||
* avg += input/n;
|
||||
}
|
||||
|
||||
/*
|
||||
uint32_t millis(void)
|
||||
{
|
||||
return blynk_time_1ms;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
// Time count function; this function have to call by timer (1ms)
|
||||
void blynk_time_handler(void)
|
||||
{
|
||||
blynk_time_1ms++;
|
||||
}
|
||||
*/
|
||||
|
||||
// Custom delay for checking timeout count
|
||||
uint8_t blynk_custom_delay(uint32_t delayms)
|
||||
{
|
||||
uint8_t ret = false;
|
||||
static uint32_t basetime;
|
||||
|
||||
if(delayms > 0)
|
||||
{
|
||||
if(!basetime) basetime = millis() + delayms;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(millis() < basetime)
|
||||
{
|
||||
ret = true; // delaying
|
||||
}
|
||||
else
|
||||
{
|
||||
basetime = 0;
|
||||
ret = false; // no delay
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief CONVERT STRING INTO INTEGER
|
||||
@return a integer number
|
||||
*/
|
||||
static uint16_t ATOI(
|
||||
uint8_t * str, /**< is a pointer to convert */
|
||||
uint8_t base /**< is a base value (must be in the range 2 - 16) */
|
||||
)
|
||||
{
|
||||
unsigned int num = 0;
|
||||
while ((*str !=0) && (*str != 0x20)) // not include the space(0x020)
|
||||
num = num * base + C2D(*str++);
|
||||
return num;
|
||||
}
|
||||
|
||||
static uint8_t C2D(
|
||||
uint8_t c /**< is a character('0'-'F') to convert to HEX */
|
||||
)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
return c - '0';
|
||||
if (c >= 'a' && c <= 'f')
|
||||
return 10 + c -'a';
|
||||
if (c >= 'A' && c <= 'F')
|
||||
return 10 + c -'A';
|
||||
|
||||
return (char)c;
|
||||
}
|
||||
|
||||
static void replacetonull(uint8_t * str, uint8_t c)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; str[x]; x++)
|
||||
if (str[x] == c) str[x] = NULL;
|
||||
}
|
||||
|
||||
uint8_t is_blynk_connection_available(void)
|
||||
{
|
||||
return blynk_connection_available;
|
||||
}
|
||||
|
||||
//Requests Server to re-send current values for all widgets
|
||||
void blynk_syncAll(void)
|
||||
{
|
||||
sendCmd(BLYNK_CMD_HARDWARE_SYNC, 0, NULL, 0, NULL, 0);
|
||||
}
|
||||
|
||||
void blynk_push_pin(uint8_t pin)
|
||||
{
|
||||
uint8_t rsp_mem[16];
|
||||
uint16_t rsp_len;
|
||||
memset(rsp_mem, 0, sizeof(rsp_mem));
|
||||
rsp_len = SPRINTF((char *)rsp_mem, "dw %d %d", pin, digitalRead(pin));
|
||||
replacetonull(rsp_mem, ' ');
|
||||
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sends integer value to a Virtual Pin
|
||||
*/
|
||||
void blynk_push_virtual_pin(uint8_t pin)
|
||||
{
|
||||
uint8_t rsp_mem[16];
|
||||
uint16_t rsp_len;
|
||||
memset(rsp_mem, 0, sizeof(rsp_mem));
|
||||
rsp_len = SPRINTF((char *)rsp_mem, "vw %d %d", pin, virtualRead(pin));
|
||||
replacetonull(rsp_mem, ' ');
|
||||
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len, NULL, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sends buffer (string message for example) to a Virtual Pin
|
||||
*/
|
||||
void blynk_push_virtual_pin_msg(uint8_t pin, uint8_t * data)
|
||||
{
|
||||
uint8_t rsp_mem[16];
|
||||
uint16_t rsp_len;
|
||||
memset(rsp_mem, 0, sizeof(rsp_mem));
|
||||
rsp_len = SPRINTF((char *)rsp_mem, "vw %d", pin);
|
||||
replacetonull(rsp_mem, ' ');
|
||||
sendCmd(BLYNK_CMD_HARDWARE, 0, rsp_mem, rsp_len+1, data, strlen(data));
|
||||
}
|
||||
|
||||
void BLYNK_LOG_TIME() {
|
||||
PRINTF(BLYNK_NEWLINE "[%lu] ", millis());
|
||||
}
|
||||
#define BLYNK_LOG1(p1) { BLYNK_LOG_TIME(); PRINTF(p1); }
|
||||
|
||||
static void printBanner() {
|
||||
#if defined(BLYNK_NO_FANCY_LOGO)
|
||||
BLYNK_LOG1(BLYNK_F("Blynk v" BLYNK_VERSION " on " BLYNK_INFO_DEVICE));
|
||||
#else
|
||||
BLYNK_LOG1(BLYNK_NEWLINE
|
||||
" ___ __ __" BLYNK_NEWLINE
|
||||
" / _ )/ /_ _____ / /__" BLYNK_NEWLINE
|
||||
" / _ / / // / _ \\/ '_/" BLYNK_NEWLINE
|
||||
" /____/_/\\_, /_//_/_/\\_\\" BLYNK_NEWLINE
|
||||
" /___/ v" BLYNK_VERSION " on " BLYNK_INFO_DEVICE BLYNK_NEWLINE
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -0,0 +1,198 @@
|
||||
#ifndef _WIZNET_BLYNK_H_
|
||||
#define _WIZNET_BLYNK_H_
|
||||
|
||||
// Change these settings to match your need
|
||||
#define BLYNK_DEFAULT_DOMAIN "blynk-cloud.com"
|
||||
#define BLYNK_DEFAULT_PORT 80
|
||||
//SSL shouldn't used here
|
||||
//#define BLYNK_DEFAULT_PORT_SSL 8441
|
||||
|
||||
//***************Professional settings: BEGIN
|
||||
// Library version.
|
||||
#define BLYNK_VERSION "0.6.0"
|
||||
|
||||
// Heartbeat period in seconds.
|
||||
#define BLYNK_HEARTBEAT 10
|
||||
|
||||
// Network timeout in milliseconds.
|
||||
#define BLYNK_TIMEOUT_MS 3000UL
|
||||
|
||||
// Limit the amount of outgoing commands per second.
|
||||
//#define BLYNK_MSG_LIMIT 15
|
||||
|
||||
// Limit the incoming command length.
|
||||
#define BLYNK_MAX_READBYTES 255
|
||||
|
||||
// Limit the outgoing command length.
|
||||
#define BLYNK_MAX_SENDBYTES 128
|
||||
|
||||
//***************Professional settings: END
|
||||
|
||||
//Print out BLYNK debug messages
|
||||
#define BLYNK_DEBUG
|
||||
|
||||
//Print out cool BLYNK ASCII LOGO
|
||||
//#define BLYNK_NO_FANCY_LOGO
|
||||
#undef BLYNK_NO_FANCY_LOGO
|
||||
|
||||
#ifndef BLYNK_INFO_DEVICE
|
||||
//#define BLYNK_INFO_DEVICE "Arduino"
|
||||
//#define BLYNK_INFO_DEVICE "WIZWiki"
|
||||
|
||||
#if defined (__AVR_ATmega644P__)
|
||||
#define BLYNK_INFO_DEVICE "m644p_W5500"
|
||||
#elif defined (__AVR_ATmega1284P__)
|
||||
#define BLYNK_INFO_DEVICE "m1284p_W5500"
|
||||
#else
|
||||
#define BLYNK_INFO_DEVICE "Custom platform"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef BLYNK_INFO_CPU
|
||||
//#define BLYNK_INFO_CPU "ATmega1284"
|
||||
//#define BLYNK_INFO_CPU "ATmega2560"
|
||||
//#define BLYNK_INFO_CPU "ATmega328P"
|
||||
#if defined (__AVR_ATmega644P__)
|
||||
#define BLYNK_INFO_CPU "ATmega644P"
|
||||
#elif defined (__AVR_ATmega1284P__)
|
||||
#define BLYNK_INFO_CPU "ATmega1284P"
|
||||
#else
|
||||
#define BLYNK_INFO_CPU "ATmega2560"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef BLYNK_INFO_CONNECTION
|
||||
#define BLYNK_INFO_CONNECTION "W5000"
|
||||
#endif
|
||||
|
||||
#define BLYNK_PARAM_KV(k, v) k "\0" v "\0"
|
||||
|
||||
// General defines
|
||||
#define STRINGIFY(x) #x
|
||||
#define TOSTRING(x) STRINGIFY(x)
|
||||
|
||||
// Custom defines
|
||||
#define BLYNK_DEFAULT_CLIENT_PORT 1025
|
||||
#define BLYNK_CONNECTION_TIMEOUT_MS 5000
|
||||
#define BLINK_HEADER_SIZE 5
|
||||
|
||||
//#ifndef BlynkProtocolDefs_h
|
||||
//#define BlynkProtocolDefs_h
|
||||
|
||||
//BlynkCmd values compatible && synced with BLYNK_VERSION 0.6.0
|
||||
enum BlynkCmd
|
||||
{
|
||||
BLYNK_CMD_RESPONSE = 0,
|
||||
|
||||
//BLYNK_CMD_LOGIN = 2, Deprecated on BLYNK_VERSION 0.6.0
|
||||
BLYNK_CMD_LOGIN = 29,
|
||||
BLYNK_CMD_PING = 6,
|
||||
BLYNK_CMD_TWEET = 12,
|
||||
BLYNK_CMD_EMAIL = 13,
|
||||
//BLYNK_CMD_PUSH_NOTIFICATION = 14, Deprecated on BLYNK_VERSION 0.6.0
|
||||
BLYNK_CMD_NOTIFY = 14,
|
||||
BLYNK_CMD_BRIDGE = 15,
|
||||
BLYNK_CMD_HARDWARE_SYNC = 16,
|
||||
BLYNK_CMD_INTERNAL = 17,
|
||||
BLYNK_CMD_SMS = 18,
|
||||
BLYNK_CMD_PROPERTY = 19,
|
||||
BLYNK_CMD_HARDWARE = 20,
|
||||
|
||||
//BLYNK_CMD_SAVE_PROF = 3, Deprecated on BLYNK_VERSION 0.6.0
|
||||
//BLYNK_CMD_LOAD_PROF = 4, Deprecated on BLYNK_VERSION 0.6.0
|
||||
//BLYNK_CMD_GET_TOKEN = 5, Deprecated on BLYNK_VERSION 0.6.0
|
||||
|
||||
BLYNK_CMD_REDIRECT = 41,
|
||||
BLYNK_CMD_DEBUG_PRINT = 55,
|
||||
BLYNK_CMD_EVENT_LOG = 64
|
||||
|
||||
};
|
||||
|
||||
//BlynkStatus values compatible && synced with BLYNK_VERSION 0.6.0
|
||||
enum BlynkStatus
|
||||
{
|
||||
BLYNK_SUCCESS = 200,
|
||||
//BLYNK_TIMEOUT = 1, Deprecated on BLYNK_VERSION 0.6.0
|
||||
BLYNK_QUOTA_LIMIT_EXCEPTION = 1,
|
||||
//BLYNK_BAD_FORMAT = 2, Deprecated on BLYNK_VERSION 0.6.0
|
||||
BLYNK_ILLEGAL_COMMAND = 2,
|
||||
BLYNK_NOT_REGISTERED = 3,
|
||||
BLYNK_ALREADY_REGISTERED = 4,
|
||||
//BLYNK_NO_LOGIN = 5, Deprecated on BLYNK_VERSION 0.6.0
|
||||
BLYNK_NOT_AUTHENTICATED = 5,
|
||||
BLYNK_NOT_ALLOWED = 6,
|
||||
//BLYNK_NO_CONNECTION = 7, Deprecated on BLYNK_VERSION 0.6.0
|
||||
BLYNK_DEVICE_NOT_IN_NETWORK = 7,
|
||||
//BLYNK_NOT_SUPPORTED = 8, Deprecated on BLYNK_VERSION 0.6.0
|
||||
BLYNK_NO_ACTIVE_DASHBOARD = 8,
|
||||
BLYNK_INVALID_TOKEN = 9,
|
||||
//BLYNK_SERVER_ERROR = 10, Deprecated on BLYNK_VERSION 0.6.0
|
||||
//BLYNK_ALREADY_LOGGED_IN = 11, Deprecated on BLYNK_VERSION 0.6.0
|
||||
BLYNK_ILLEGAL_COMMAND_BODY = 11,
|
||||
BLYNK_GET_GRAPH_DATA_EXCEPTION = 12,
|
||||
BLYNK_NO_DATA_EXCEPTION = 17,
|
||||
BLYNK_DEVICE_WENT_OFFLINE = 18,
|
||||
BLYNK_SERVER_EXCEPTION = 19,
|
||||
|
||||
BLYNK_NTF_INVALID_BODY = 13,
|
||||
BLYNK_NTF_NOT_AUTHORIZED = 14,
|
||||
BLYNK_NTF_ECXEPTION = 15,
|
||||
|
||||
BLYNK_TIMEOUT = 16,
|
||||
|
||||
BLYNK_NOT_SUPPORTED_VERSION = 20,
|
||||
BLYNK_ENERGY_LIMIT = 21
|
||||
|
||||
};
|
||||
|
||||
typedef struct _BlynkHeader
|
||||
{
|
||||
uint8_t type;
|
||||
uint16_t msg_id;
|
||||
uint16_t length;
|
||||
}
|
||||
BlynkHeader;
|
||||
|
||||
typedef struct _BlynkParam
|
||||
{
|
||||
uint8_t * buff;
|
||||
uint16_t len;
|
||||
}
|
||||
BlynkParam;
|
||||
|
||||
#if defined(__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__)
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) )
|
||||
#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \
|
||||
((x)<< 8 & 0x00FF0000UL) | \
|
||||
((x)>> 8 & 0x0000FF00UL) | \
|
||||
((x)>>24 & 0x000000FFUL) )
|
||||
#define ntohs(x) htons(x)
|
||||
#define ntohl(x) htonl(x)
|
||||
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
#define htons(x) (x)
|
||||
#define htonl(x) (x)
|
||||
#define ntohs(x) (x)
|
||||
#define ntohl(x) (x)
|
||||
#else
|
||||
#error byte order problem
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define BLYNK_NEWLINE "\r\n"
|
||||
|
||||
void blynk_begin(uint8_t * auth, uint8_t * dest_ip, uint16_t dest_port, uint8_t * buf, uint8_t socket);
|
||||
void blynk_run(void);
|
||||
|
||||
//void blynk_time_handler(void);
|
||||
uint8_t is_blynk_connection_available(void);
|
||||
/*
|
||||
void sendCmd(uint8_t cmd, uint16_t id, uint8_t * data, size_t length, uint8_t * data2, size_t length2);
|
||||
*/
|
||||
void blynk_syncAll(void);
|
||||
void blynk_push_pin(uint8_t pin);
|
||||
void blynk_push_virtual_pin(uint8_t pin);
|
||||
void blynk_push_virtual_pin_msg(uint8_t pin, uint8_t * data);
|
||||
#endif
|
||||
|
||||
@ -0,0 +1,201 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include "blynkDependency.h"
|
||||
#include "../globals.h"
|
||||
|
||||
#ifdef WIZNET_WIZ550WEB
|
||||
#include "gpioHandler.h"
|
||||
#include "userHandler.h"
|
||||
#elif defined WIZNET_W5500_EVB
|
||||
#include "board.h"
|
||||
#include "adcHandler.h"
|
||||
#endif
|
||||
|
||||
uint8_t digitalRead(uint8_t pin)
|
||||
{
|
||||
uint8_t val = pin;
|
||||
#ifdef WIZNET_WIZ550WEB
|
||||
val = get_IO_Status(pin);
|
||||
#elif defined WIZNET_W5500_EVB
|
||||
val = Chip_GPIO_GetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin]);
|
||||
#else
|
||||
PRINTF("digital pin %d read\r\n", pin);
|
||||
if(pin == 21)
|
||||
{
|
||||
val = sw1_read()?0:!0;
|
||||
PRINTF("SW1 is: %d %s\r\n", val, val?"HIGH":"LOW");
|
||||
}
|
||||
else if(pin == 13)
|
||||
{
|
||||
val = led2_read()?1:0;
|
||||
PRINTF("LED2 is: %d %s\r\n", val, val?"HIGH":"LOW");
|
||||
}
|
||||
else
|
||||
{
|
||||
val = 1;
|
||||
}
|
||||
#endif
|
||||
return val;
|
||||
}
|
||||
|
||||
void digitalWrite(uint8_t pin, uint8_t val)
|
||||
{
|
||||
#ifdef WIZNET_WIZ550WEB
|
||||
IOdata.ios[pin] = val;
|
||||
if(val == HIGH) IO_On(pin);
|
||||
else if(val == LOW) IO_Off(pin);
|
||||
write_IOstorage(&IOdata, sizeof(IOdata));
|
||||
#elif defined WIZNET_W5500_EVB
|
||||
if(val == HIGH) Chip_GPIO_SetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin], true); // High
|
||||
else if(val == LOW) Chip_GPIO_SetPinState(LPC_GPIO, dio_ports[pin], dio_pins[pin], false); // Low
|
||||
#else
|
||||
PRINTF("digital pin %d write val %d\r\n", pin, val);
|
||||
if(pin == 20)
|
||||
{
|
||||
if(val == 0)
|
||||
{
|
||||
led1_low();
|
||||
}
|
||||
else
|
||||
{
|
||||
led1_high();
|
||||
}
|
||||
//Rise flag to PUSH message to Virtual pin 20
|
||||
v20_changed = 1;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t analogRead(uint8_t pin)
|
||||
{
|
||||
uint16_t val = 0;
|
||||
#ifdef WIZNET_WIZ550WEB
|
||||
uint8_t analog_pin = 0;
|
||||
if(pin > 14) analog_pin = pin - 14;
|
||||
//PRINTF("analog_pin = %d\r\n", analog_pin);
|
||||
val = get_ADC_val(analog_pin);
|
||||
#elif defined WIZNET_W5500_EVB
|
||||
uint8_t analog_pin = 0;
|
||||
if(pin > 14) analog_pin = pin - 14;
|
||||
PRINTF("analog_pin = %d\r\n", analog_pin);
|
||||
if(analog_pin == A0) analog_pin = AIN;
|
||||
PRINTF("changed analog_pin = %d\r\n", analog_pin);
|
||||
val = get_ADC_val(analog_pin);
|
||||
#else
|
||||
if(pin == 6)
|
||||
{
|
||||
val = adc_read(6);
|
||||
}
|
||||
PRINTF("analog pin %d = %d\r\n", pin, val);
|
||||
#endif
|
||||
return val;
|
||||
}
|
||||
|
||||
void analogWrite(uint8_t pin, uint8_t val)
|
||||
{
|
||||
#ifdef WIZNET_WIZ550WEB
|
||||
PRINTF("Analog Write: Not supported yet. pin %d, val %d", pin, val);
|
||||
#elif defined WIZNET_W5500_EVB
|
||||
PRINTF("Analog Write: Not supported yet. pin %d, val %d", pin, val);
|
||||
#else
|
||||
PRINTF("analog pin %d write val %d\r\n", pin, val);
|
||||
/*
|
||||
* Handle PWM out PD7-PIN15:
|
||||
* OCR2A = 0/127/255; Duty 0/50/100%
|
||||
*/
|
||||
if(pin == 15)
|
||||
{
|
||||
OCR2A = val;
|
||||
//Rise flag to PUSH message to Virtual pin 15
|
||||
v15_changed = 1;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Pin mode (dir) defines
|
||||
// 0: Input
|
||||
// 1: Output
|
||||
// 2: Input Pull-up
|
||||
void pinMode(uint8_t pin, pinmode_dir dir)
|
||||
{
|
||||
// TODO: Add here example for AVR PULL-UP
|
||||
#ifdef WIZNET_WIZ550WEB
|
||||
if(dir == INPUT) IOdata.io[pin] = Input;
|
||||
else if(dir == INPUT_PULLUP) IOdata.io[pin] = Input;
|
||||
else if(dir == OUTPUT) IOdata.io[pin] = Output; // Output
|
||||
IO_Init(pin);
|
||||
write_IOstorage(&IOdata, sizeof(IOdata));
|
||||
#elif defined WIZNET_W5500_EVB
|
||||
if(dir == INPUT) Chip_GPIO_SetPinDIRInput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Input
|
||||
else if(dir == INPUT_PULLUP) Chip_GPIO_SetPinDIRInput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Input
|
||||
else if(dir == OUTPUT) Chip_GPIO_SetPinDIROutput(LPC_GPIO, dio_ports[pin], dio_pins[pin]); // Output
|
||||
#else
|
||||
PRINTF("pinmode setting: pin %d dir %d\r\n", pin, dir);
|
||||
if((pin == 20)&&(dir ==1))
|
||||
{
|
||||
//m1284p LED1 pin to out
|
||||
led1_conf();
|
||||
}
|
||||
else if((pin == 21)&&(dir == 0))
|
||||
{
|
||||
//m1284p SW1 pin to input
|
||||
sw1_conf();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Virtual Pin Read
|
||||
uint16_t virtualRead(uint8_t pin)
|
||||
{
|
||||
uint16_t val = 0;
|
||||
PRINTF("virtual pin %d read\r\n", pin);
|
||||
//Example virtual pin reading
|
||||
//Override your own handlers here like:
|
||||
if(pin == 13)
|
||||
{
|
||||
//Digital read example from Virtual Pin 13
|
||||
val = led2_read()?1:0;
|
||||
//PRINTF("LED2 is: %d %s\r\n", val, val?"HIGH":"LOW");
|
||||
}
|
||||
else if(pin == 6)
|
||||
{
|
||||
//Analog read example from Virtual Pin 6
|
||||
val = adc_read(6);
|
||||
//PRINTF("analog pin %d = %d\r\n", pin, val);
|
||||
}
|
||||
else if(pin == 15)
|
||||
{
|
||||
//PWM Value PIN15/PD7
|
||||
val = (uint16_t)OCR2A;
|
||||
}
|
||||
else if(pin == 20)
|
||||
{
|
||||
//Digital OUT Value PIN20/PC4
|
||||
val = led1_read()?1:0;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
void virtualWrite(uint8_t pin, uint16_t val)
|
||||
{
|
||||
PRINTF("virtual pin %d write val %d\r\n", pin, val);
|
||||
//Example virtual pin writing
|
||||
//Override your own handlers here like:
|
||||
if(pin == 15)
|
||||
{
|
||||
//Analog write example to Virtual Pin 15
|
||||
OCR2A = (uint8_t)val;
|
||||
}
|
||||
else if(pin == 20)
|
||||
{
|
||||
//Digital write example to Virtual Pin 20
|
||||
if(val == 0)
|
||||
{
|
||||
led1_low();
|
||||
}
|
||||
else
|
||||
{
|
||||
led1_high();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
#ifndef _WIZNET_BLYNK_DEPENDENCY_H_
|
||||
#define _WIZNET_BLYNK_DEPENDENCY_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include "blynk.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
typedef enum { // Pin mode; directions
|
||||
INPUT,
|
||||
OUTPUT,
|
||||
INPUT_PULLUP
|
||||
}pinmode_dir;
|
||||
|
||||
typedef enum {false = 0, true = !false} Boolian;
|
||||
|
||||
#define HIGH 1
|
||||
#define LOW 0
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
uint8_t digitalRead(uint8_t pin);
|
||||
void digitalWrite(uint8_t pin, uint8_t val);
|
||||
|
||||
uint16_t analogRead(uint8_t pin);
|
||||
void analogWrite(uint8_t pin, uint8_t val);
|
||||
|
||||
uint16_t virtualRead(uint8_t pin);
|
||||
void virtualWrite(uint8_t pin, uint16_t val);
|
||||
|
||||
void pinMode(uint8_t pin, pinmode_dir dir);
|
||||
|
||||
#endif
|
||||
Reference in New Issue