Windows Virtual Shields for Arduino
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
VirtualShield Class Reference

#include <VirtualShield.h>

Public Member Functions

 VirtualShield ()
 Initializes a new instance of the VirtualShield class. More...
 
void begin (long bitRate=DEFAULT_BAUDRATE)
 Begins the specified bit rate. More...
 
void setPort (int port)
 Sets the port for bluetooth (this only works for AVR_ATmega32U4 where there are more than one port). More...
 
bool checkSensors (int watchForId=0, long timeout=0, int waitForResultId=-1)
 Receives events as long as they exist, or until an optional timeout occurs. More...
 
int waitFor (int id, long timeout=WAITFOR_TIMEOUT, bool asSuccess=true, int resultId=-1)
 Blocks and awaits an event with an id. More...
 
bool hasError (ShieldEvent *shieldEvent=0)
 Returns true More...
 
bool getEvent (ShieldEvent *shieldEvent)
 Gets zero or one available events for processing. More...
 
int directToSerial (const char *cmd)
 
bool addSensor (Sensor *sensor)
 Adds a sensor to the list of known sensors in order to match and dispatch for incoming events. More...
 
virtual void onJsonReceived (JsonObject &root, ShieldEvent *shieldEvent)
 Event called when a valid json message was received. Dispatches to added sensors that match the incoming Type. More...
 
void write (const char *text)
 Writes the specified text to the communication channel. More...
 
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. More...
 
int writeAll (const char *serviceName)
 Writes the service name only to the communication channel. More...
 
int beginWrite (const char *serviceName)
 Begins a service write operation to the communication channel. Increments a message id, includes and returns the id. More...
 
int write (EPtr eptr) const
 Writes the specified eptr. More...
 
int endWrite ()
 Ends the write operation. More...
 
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 allowAutoBlocking is true). More...
 
void setOnEvent (void(*onEvent)(ShieldEvent *))
 
void setOnConnect (void(*onConnect)(ShieldEvent *))
 
void setOnRefresh (void(*onRefresh)(ShieldEvent *))
 
void setOnSuspend (void(*onSuspend)(ShieldEvent *))
 
void setOnResume (void(*onResume)(ShieldEvent *))
 
void enableAutoBlocking (bool enable)
 Enables or disables block() to block for specific id-based responses. More...
 
int parseToHash (const char *text, unsigned int *hash, int hashCount, char separator= ' ', unsigned int length=-1)
 

Static Public Member Functions

static unsigned int hash (const char *s, unsigned int len=-1, unsigned int seed=0)
 

Public Attributes

void(* onEvent )(ShieldEvent *)=0
 
void(* onConnect )(ShieldEvent *)=0
 
void(* onRefresh )(ShieldEvent *)=0
 
void(* onSuspend )(ShieldEvent *)=0
 
void(* onResume )(ShieldEvent *)=0
 

Protected Member Functions

int sendFlashStringOnSerial (const char *flashStringAdr, int start=-1, bool encode=false) const
 Sends the flash (PROGMEM) string on the communication channel. More...
 
void onJsonStringReceived (char *json, ShieldEvent *shieldEvent)
 Event callback for when a full json string is received. More...
 
void onStringReceived (char *buffer, int length, ShieldEvent *shieldEvent)
 Event callback for when a full string is received. More...
 
void flush ()
 Flushes this instance onto the serial port. More...
 

Protected Attributes

Stream * _VShieldSerial
 

Constructor & Destructor Documentation

VirtualShield::VirtualShield ( )

Initializes a new instance of the VirtualShield class.

Member Function Documentation

bool VirtualShield::addSensor ( Sensor sensor)

Adds a sensor to the list of known sensors in order to match and dispatch for incoming events.

Parameters
sensorThe sensor to add.
Returns
true if it was added, false if the maxRememberedSensors is reached.
void VirtualShield::begin ( long  bitRate = DEFAULT_BAUDRATE)

Begins the specified bit rate.

Parameters
bitRateThe bit rate to use for the virtual shield serial connection.
int VirtualShield::beginWrite ( const char *  serviceName)

Begins a service write operation to the communication channel. Increments a message id, includes and returns the id.

Parameters
serviceNameName of the service.
Returns
The new id of the message or a negative error..
int VirtualShield::block ( int  id,
bool  blocking,
long  timeout = WAITFOR_TIMEOUT,
int  watchForResultId = -1 
)

Blocks while waiting for a specific id-based response (only when blocking is true and allowAutoBlocking is true).

bool VirtualShield::checkSensors ( int  watchForId = 0,
long  timeout = 0,
int  watchForResultId = -1 
)

Receives events as long as they exist, or until an optional timeout occurs.

Parameters
watchForIdAn id to return true if found. Otherwise true is returned for any events processed.
timeoutThe timeout in milliseconds.
Returns
true if the id matched an incoming event, or if no id, any event.
int VirtualShield::directToSerial ( const char *  cmd)
void VirtualShield::enableAutoBlocking ( bool  enable)
inline

Enables or disables block() to block for specific id-based responses.

int VirtualShield::endWrite ( )

Ends the write operation.

Returns
Zero if no error, negative if an error.
void VirtualShield::flush ( )
protected

Flushes this instance onto the serial port.

bool VirtualShield::getEvent ( ShieldEvent shieldEvent)

Gets zero or one available events for processing.

Parameters
shieldEventThe address of ShieldEvent to populate.
Returns
true if an event was populated
bool VirtualShield::hasError ( ShieldEvent shieldEvent = 0)

Returns true

Parameters
idThe id.
timeoutThe timeout.
Returns
The matching id, or zero if not found.
unsigned int VirtualShield::hash ( const char *  s,
unsigned int  len = -1,
unsigned int  seed = 0 
)
static
void VirtualShield::onJsonReceived ( JsonObject &  root,
ShieldEvent shieldEvent 
)
virtual

Event called when a valid json message was received. Dispatches to added sensors that match the incoming Type.

Parameters
rootThe root json object.
shieldEventThe shield event.
void VirtualShield::onJsonStringReceived ( char *  json,
ShieldEvent shieldEvent 
)
protected

Event callback for when a full json string is received.

Parameters
jsonThe json string.
shieldEventThe shield event to populate.
void VirtualShield::onStringReceived ( char *  buffer,
int  length,
ShieldEvent shieldEvent 
)
protected

Event callback for when a full string is received.

Parameters
bufferThe buffer.
lengthThe length.
shieldEventThe shield event.
int VirtualShield::parseToHash ( const char *  text,
unsigned int *  hash,
int  hashCount,
char  separator = ' ',
unsigned int  length = -1 
)
int VirtualShield::sendFlashStringOnSerial ( const char *  flashStringAdr,
int  start = -1,
bool  encode = false 
) const
protected

Sends the flash (PROGMEM) string on the communication channel.

Parameters
flashStringAdrThe flash (PROGMEM) string address.
Returns
Zero if no error, negative if an error.
void VirtualShield::setOnConnect ( void(*)(ShieldEvent *)  onConnect)
inline
void VirtualShield::setOnEvent ( void(*)(ShieldEvent *)  onEvent)
inline
void VirtualShield::setOnRefresh ( void(*)(ShieldEvent *)  onRefresh)
inline
void VirtualShield::setOnResume ( void(*)(ShieldEvent *)  onResume)
inline
void VirtualShield::setOnSuspend ( void(*)(ShieldEvent *)  onSuspend)
inline
void VirtualShield::setPort ( int  port)

Sets the port for bluetooth (this only works for AVR_ATmega32U4 where there are more than one port).

Parameters
portThe port.
int VirtualShield::waitFor ( int  id,
long  timeout = WAITFOR_TIMEOUT,
bool  asSuccess = true,
int  resultId = -1 
)

Blocks and awaits an event with an id.

Parameters
idThe id.
timeoutThe timeout.
Returns
The matching id, or zero if not found.
void VirtualShield::write ( const char *  text)

Writes the specified text to the communication channel.

Parameters
textThe text.
int VirtualShield::write ( EPtr  eptr) const

Writes the specified eptr.

Parameters
eptrThe eptr.
Returns
Zero if no error, negative if an error.
int VirtualShield::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.

Parameters
serviceNameName of the service.
valuesThe values.
countThe count of values.
Returns
The new id of the message or a negative error.
int VirtualShield::writeAll ( const char *  serviceName)

Writes the service name only to the communication channel.

Parameters
serviceNameName of the service.
Returns
int.

Member Data Documentation

Stream* VirtualShield::_VShieldSerial
protected
void(* VirtualShield::onConnect) (ShieldEvent *)=0
void(* VirtualShield::onEvent) (ShieldEvent *)=0
void(* VirtualShield::onRefresh) (ShieldEvent *)=0
void(* VirtualShield::onResume) (ShieldEvent *)=0
void(* VirtualShield::onSuspend) (ShieldEvent *)=0

The documentation for this class was generated from the following files: