Windows Virtual Shields for Arduino
VirtualShield.h
Go to the documentation of this file.
1 /*
2  Copyright(c) Microsoft Open Technologies, Inc. All rights reserved.
3 
4  The MIT License(MIT)
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files(the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions :
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  THE SOFTWARE.
23 */
24 
25 #ifndef VirtualShield_h
26 #define VirtualShield_h
27 
28 #include "Arduino.h"
29 
30 #include <ArduinoJson.h>
31 
32 #include "Sensor.h"
33 #include "ShieldEvent.h"
34 #include "Attr.h"
35 
36 typedef unsigned int UINT;
37 
38 class Sensor;
39 struct SensorEvent;
40 
41 const long DEFAULT_BAUDRATE = 115200;
42 const long WAITFOR_TIMEOUT = 30000;
43 
44 #define REFRESH_HASH 0xC5BF
45 #define CONNECT_HASH 0xA91A
46 #define PING_HASH 0x2CFE
47 #define SUSPEND_HASH 0xC15E
48 #define RESUME_HASH 0x3549
49 
51 {
52 public:
53  void(*onEvent)(ShieldEvent*) = 0;
54  void(*onConnect)(ShieldEvent*) = 0;
55  void(*onRefresh)(ShieldEvent*) = 0;
56  void(*onSuspend)(ShieldEvent*) = 0;
57  void(*onResume)(ShieldEvent*) = 0;
58 
59  VirtualShield();
60 
61  void begin(long bitRate = DEFAULT_BAUDRATE);
62  void setPort(int port);
63 
64  bool checkSensors(int watchForId = 0, long timeout = 0, int waitForResultId = -1);
65  int waitFor(int id, long timeout = WAITFOR_TIMEOUT, bool asSuccess = true, int resultId = -1);
66  bool hasError(ShieldEvent* shieldEvent = 0);
67 
68  bool getEvent(ShieldEvent* shieldEvent);
69 
70  int directToSerial(const char* cmd);
71 
72  bool addSensor(Sensor* sensor);
73 
74  virtual void onJsonReceived(JsonObject& root, ShieldEvent* shieldEvent);
75 
76  void write(const char* text);
77  int writeAll(const char* serviceName, EPtr values[], int count, Attr extraAttributes[] = 0, int extraAttributeCount = 0, const char sensorType = '\0');
78 
79  int writeAll(const char* serviceName);
80 
81  int beginWrite(const char* serviceName) ;
82  int write(EPtr eptr) const;
83 
84  int endWrite();
85 
86  int block(int id, bool blocking, long timeout = WAITFOR_TIMEOUT, int waitForResultId = -1);
87 
89  {
90  this->onEvent = onEvent;
91  }
92 
94  {
95  this->onConnect = onConnect;
96  }
97 
99  {
100  this->onRefresh = onRefresh;
101  }
102 
104  {
105  this->onSuspend = onSuspend;
106  }
107 
109  {
110  this->onResume = onResume;
111  }
112 
116  void enableAutoBlocking(bool enable) {
117  this->allowAutoBlocking = enable;
118  }
119 
120  int parseToHash(const char* text, unsigned int *hash, int hashCount, char separator = ' ', unsigned int length = -1);
121  static unsigned int hash(const char* s, unsigned int len = -1, unsigned int seed = 0);
122 
123 protected:
124  int sendFlashStringOnSerial(const char* flashStringAdr, int start = -1, bool encode = false) const;
125 
126  void onJsonStringReceived(char* json, ShieldEvent* shieldEvent);
127  void onStringReceived(char* buffer, int length, ShieldEvent* shieldEvent);
128 
129  void flush();
130 
131  Stream* _VShieldSerial;
132 private:
133  int nextId = 1;
134  ShieldEvent recentEvent;
135  bool allowAutoBlocking = true;
136 
137  void sendPingBack(ShieldEvent* shieldEvent);
138  void sendStart();
139  int writeValue(EPtr eptr, int start = 0) const;
140 };
141 
142 #endif
Definition: Sensor.h:51
void setPort(int port)
Sets the port for bluetooth (this only works for AVR_ATmega32U4 where there are more than one port)...
Definition: VirtualShield.cpp:118
void(* onConnect)(ShieldEvent *)=0
Definition: VirtualShield.h:54
Definition: VirtualShield.h:50
const long DEFAULT_BAUDRATE
Definition: VirtualShield.h:41
void onStringReceived(char *buffer, int length, ShieldEvent *shieldEvent)
Event callback for when a full string is received.
Definition: VirtualShield.cpp:360
Stream * _VShieldSerial
Definition: VirtualShield.h:131
bool addSensor(Sensor *sensor)
Adds a sensor to the list of known sensors in order to match and dispatch for incoming events...
Definition: VirtualShield.cpp:104
bool getEvent(ShieldEvent *shieldEvent)
Gets zero or one available events for processing.
Definition: VirtualShield.cpp:172
void setOnSuspend(void(*onSuspend)(ShieldEvent *))
Definition: VirtualShield.h:103
const long WAITFOR_TIMEOUT
Definition: VirtualShield.h:42
virtual void onJsonReceived(JsonObject &root, ShieldEvent *shieldEvent)
Event called when a valid json message was received. Dispatches to added sensors that match the incom...
Definition: VirtualShield.cpp:251
bool hasError(ShieldEvent *shieldEvent=0)
Returns true
Definition: VirtualShield.cpp:438
bool checkSensors(int watchForId=0, long timeout=0, int waitForResultId=-1)
Receives events as long as they exist, or until an optional timeout occurs.
Definition: VirtualShield.cpp:374
int endWrite()
Ends the write operation.
Definition: VirtualShield.cpp:695
void setOnEvent(void(*onEvent)(ShieldEvent *))
Definition: VirtualShield.h:88
VirtualShield()
Initializes a new instance of the VirtualShield class.
Definition: VirtualShield.cpp:94
void setOnResume(void(*onResume)(ShieldEvent *))
Definition: VirtualShield.h:108
void(* onSuspend)(ShieldEvent *)=0
Definition: VirtualShield.h:56
int waitFor(int id, long timeout=WAITFOR_TIMEOUT, bool asSuccess=true, int resultId=-1)
Blocks and awaits an event with an id.
Definition: VirtualShield.cpp:415
int writeAll(const char *serviceName, EPtr values[], int count, Attr extraAttributes[]=0, int extraAttributeCount=0, const char sensorType= '\0')
Writes all EPtr values to the communication channel.
Definition: VirtualShield.cpp:474
void setOnConnect(void(*onConnect)(ShieldEvent *))
Definition: VirtualShield.h:93
static unsigned int hash(const char *s, unsigned int len=-1, unsigned int seed=0)
Definition: VirtualShield.cpp:680
int beginWrite(const char *serviceName)
Begins a service write operation to the communication channel. Increments a message id...
Definition: VirtualShield.cpp:447
Definition: SensorModels.h:101
Definition: Sensor.h:89
void(* onRefresh)(ShieldEvent *)=0
Definition: VirtualShield.h:55
void begin(long bitRate=DEFAULT_BAUDRATE)
Begins the specified bit rate.
Definition: VirtualShield.cpp:132
int sendFlashStringOnSerial(const char *flashStringAdr, int start=-1, bool encode=false) const
Sends the flash (PROGMEM) string on the communication channel.
Definition: VirtualShield.cpp:713
void(* onEvent)(ShieldEvent *)=0
Definition: VirtualShield.h:53
void enableAutoBlocking(bool enable)
Enables or disables block() to block for specific id-based responses.
Definition: VirtualShield.h:116
Definition: Attr.h:30
unsigned int UINT
Definition: VirtualShield.h:36
Definition: ShieldEvent.h:35
int directToSerial(const char *cmd)
Definition: VirtualShield.cpp:702
void flush()
Flushes this instance onto the serial port.
Definition: VirtualShield.cpp:161
int parseToHash(const char *text, unsigned int *hash, int hashCount, char separator= ' ', unsigned int length=-1)
Definition: VirtualShield.cpp:652
void(* onResume)(ShieldEvent *)=0
Definition: VirtualShield.h:57
void setOnRefresh(void(*onRefresh)(ShieldEvent *))
Definition: VirtualShield.h:98
void write(const char *text)
Writes the specified text to the communication channel.
Definition: VirtualShield.cpp:392
int block(int id, bool blocking, long timeout=WAITFOR_TIMEOUT, int waitForResultId=-1)
Blocks while waiting for a specific id-based response (only when blocking is true and allowAutoBlocki...
Definition: VirtualShield.cpp:153
void onJsonStringReceived(char *json, ShieldEvent *shieldEvent)
Event callback for when a full json string is received.
Definition: VirtualShield.cpp:342