-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkItOneBluetoothPublish.cpp
105 lines (97 loc) · 2.67 KB
/
LinkItOneBluetoothPublish.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "LBT.h"
#include "LBTServer.h"
extern "C" {
#include "stdlib.h"
}
#include <LinkItOneBluetoothPublish.h>
extern "C" {
char * dtostrf(double __val,signed char __width,unsigned char __prec,char * __s); // one way
}
LinkItOneBluetoothPublish::LinkItOneBluetoothPublish(void)
{
i=0;
inputString="";
stringComplete=false;
pIndex=0;
}
void LinkItOneBluetoothPublish::beginBT()
{
LBTServer.begin((uint8_t *)"LinkIt_One");
}
void LinkItOneBluetoothPublish::publish(String str,float *ptr)
{
s[pIndex]=str;
p[pIndex]=ptr;
pIndex++;
}
void LinkItOneBluetoothPublish::sync()
{
BTEve();
if (stringComplete) {
for(i=0;i<pIndex;i++)
{
//check for =, if = is there then it is put request
if(inputString.indexOf('=')==-1){ //this is a get request
if(s[i].equalsIgnoreCase(inputString.substring(2,inputString.indexOf('>'))))
{
// LBTServer.print(inputString.substring(2,inputString.indexOf('>'))+":");
uint8_t buf[8];
char *s=(char *)buf;
dtostrf(*p[i],4,2,s);
buf[4]='\r';
buf[5]='\n';
buf[6]=0;
LBTServer.write(buf,6);
//Serial.println("Sent the response");
}
}
else //this is a put request
{
if(s[i].equalsIgnoreCase(inputString.substring(2,inputString.indexOf('='))))
{
//LBTServer.print(inputString.substring(2,inputString.indexOf('='))+":");
*p[i]=inputString.substring(inputString.indexOf('=')+1,inputString.indexOf('>')).toFloat();
uint8_t buf[8];
char *s=(char *)buf;
dtostrf(*p[i],4,2,s);
buf[4]='\r';
buf[5]='\n';
buf[6]=0;
LBTServer.write(buf,6);
//Serial.println("Sent the response");
}
}
}
//LBTServer.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
if connection is not made, will wait for 2 sec for new connections
BTEve occurs whenever a new data comes in the
Bluetooth. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void LinkItOneBluetoothPublish::BTEve() {
if(LBTServer.connected())
{
while (LBTServer.available()&& !stringComplete) {
// get the new byte:
char inChar = (char)LBTServer.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '>') {
stringComplete = true;
}
}
}
else
{
LBTServer.accept(5);
}
}