PiStorms  4.015
PiStorms Library Reference by mindsensors.com
 All Classes Namespaces Files Functions Variables
scratch.Scratch Class Reference
Inheritance diagram for scratch.Scratch:

Public Member Functions

def __init__
 
def __repr__
 
def connect
 
def disconnect
 
def sensorupdate
 
def broadcast
 
def receive
 

Static Public Attributes

int prefix_len = 4
 
tuple broadcast_prefix_len = prefix_len+len('broadcast ')
 
tuple sensorupdate_prefix_len = prefix_len+len('sensor-update ')
 
tuple msg_types = set(['broadcast', 'sensor-update'])
 
tuple n = len(msg)
 
tuple a = array.array('c')
 
string escaped = ''
 
string unescaped = ''
 
int i = 0
 
tuple length = self._extract_len(msg[:self.prefix_len])
 
list msg_type = msg[self.prefix_len:]
 
list update = msg[self.sensorupdate_prefix_len:]
 
list parsed = []
 
string curr_seg = ''
 
int numq = 0
 
list unescaped = [self._unescape(self._get_type(x)) for x in parsed]
 
int total_sent = 0
 
tuple sent = self.socket.send(data[total_sent:])
 
string data = ''
 
tuple chunk = self.socket.recv(size-len(data))
 
tuple prefix = self._read(self.prefix_len)
 
tuple msg = self._read(self._extract_len(prefix))
 
string msg = 'sensor-update '
 

Member Function Documentation

def scratch.Scratch.disconnect (   self)
Connects to Scratch.
def scratch.Scratch.receive (   self)
Broadcasts msg to Scratch. msg can be a single message or an iterable
(list, tuple, set, generator, etc.) of messages.
def scratch.Scratch.sensorupdate (   self,
  data 
)
Closes connection to Scratch

Member Data Documentation

string scratch.Scratch.data = ''
static
Reads size number of bytes from Scratch and returns data as a string
string scratch.Scratch.escaped = ''
static
Escapes double quotes by adding another double quote as per the Scratch
protocol. Expects a string without its delimiting quotes. Returns a new
escaped string.
tuple scratch.Scratch.length = self._extract_len(msg[:self.prefix_len])
static
Returns True if message is a proper Scratch message, else return False.
string scratch.Scratch.msg = 'sensor-update '
static
Given a dict of sensors and values, updates those sensors with the
values in Scratch.
list scratch.Scratch.msg_type = msg[self.prefix_len:]
static
Parses a Scratch message and returns a tuple with the first element
as the message type, and the second element as the message payload. The
payload for a 'broadcast' message is a string, and the payload for a
'sensor-update' message is a dict whose keys are variables, and values
are updated variable values. Returns None if msg is not a message.
tuple scratch.Scratch.n = len(msg)
static
Packages msg according to Scratch message specification (encodes and
appends length prefix to msg). Credit to chalkmarrow from the
scratch.mit.edu forums for the prefix encoding code.
tuple scratch.Scratch.prefix = self._read(self.prefix_len)
static
Receives and returns a message from Scratch
int scratch.Scratch.total_sent = 0
static
Writes string data out to Scratch
string scratch.Scratch.unescaped = ''
static
Removes double quotes that were used to escape double quotes. Expects
a string without its delimiting quotes, or a number. Returns a new
unescaped string.
list scratch.Scratch.update = msg[self.sensorupdate_prefix_len:]
static
Given a sensor-update message, returns the sensors/variables that were
updated as a dict that maps sensors/variables to their updated values.

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