You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+51-18
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,8 @@ A Python client for [Scratch](scratch.mit.edu)
6
6
7
7
1. Start up Scratch
8
8
9
-
2.[Enable remote sensor connections](http://wiki.scratch.mit.edu/wiki/Remote_Sensor_Connections#Enabling), or "Host Mesh" in [BYOB](http://byob.berkeley.edu/)
or "Host Mesh" in [BYOB](http://byob.berkeley.edu/)
10
11
11
12
3. Create a variable for all sprites named `foo`
12
13
@@ -30,7 +31,8 @@ Or
30
31
# easy_install scratchpy
31
32
```
32
33
33
-
If you're installing from source, you can untar the source tarball, `cd` into the project directory and run
34
+
If you're installing from source, you can untar the source tarball, `cd` into
35
+
the project directory and run
34
36
35
37
```
36
38
# make install
@@ -46,7 +48,9 @@ import scratch
46
48
s = scratch.Scratch()
47
49
```
48
50
49
-
This will create a connection on `localhost` port 42001 and set `s.connected` to `True`. If you want to change the host or port, you can provide them to the constructor
51
+
This will create a connection on `localhost` port 42001 and set `s.connected`
52
+
to `True`. If you want to change the host or port, you can provide them to the
53
+
constructor
50
54
51
55
```
52
56
s = scratch.Scratch(host='0.0.0.0', port=40000)
@@ -59,7 +63,8 @@ s.connect()
59
63
```
60
64
61
65
### Broadcasting
62
-
Broadcasting messages to Scratch will function like a broadcast block in Scratch. You can broadcast either a single message
66
+
Broadcasting messages to Scratch will function like a broadcast block in
67
+
Scratch. You can broadcast either a single message
63
68
64
69
```
65
70
s.broadcast('Hello, Scratch!')
@@ -71,10 +76,13 @@ Or a list of messages
71
76
s.broadcast(['Hello, Scratch!', 'How are you doing?'])
72
77
```
73
78
74
-
Actually, you can give `broadcast` any iterable object (list, tuple, set, generator, etc.).
79
+
Actually, you can give `broadcast` any iterable object (list, tuple, set,
80
+
generator, etc.).
75
81
76
82
### Sensor updates
77
-
Sending sensor updates to Scratch will create new sensors in the Sensing category, or update sensors with new values. The `sensorupdate` method accepts a dict whose keys are sensor names, and values are sensor values.
83
+
Sending sensor updates to Scratch will create new sensors in the Sensing
84
+
category, or update sensors with new values. The `sensorupdate` method accepts
85
+
a dict whose keys are sensor names, and values are sensor values.
78
86
79
87
```
80
88
s.sensorupdate({'temperature' : 75})
@@ -87,28 +95,43 @@ Use the `receive` method to receive messages from Scratch
87
95
msg = s.receive()
88
96
```
89
97
90
-
A call to `receive` will block until it reads a message from Scratch. If the call is successful, it returns a tuple of the message received. If the call failed, it raises an exception. If the message received is not a proper Scratch message, `receive` returns `None`.
98
+
A call to `receive` will block until it reads a message from Scratch. If the
99
+
call is successful, it returns a tuple of the message received. If the call
100
+
failed, it raises an exception. If the message received is not a proper Scratch
101
+
message, `receive` returns `None`.
91
102
92
-
The first element of the tuple will be the message type and the second element will be the message data.
103
+
The first element of the tuple will be the message type and the second element
104
+
will be the message data.
93
105
94
-
Two types of messages can be received: broadcast messages and sensor update messages.
106
+
Two types of messages can be received: broadcast messages and sensor update
107
+
messages.
95
108
96
109
#### Broadcasts
97
-
Broadcast messages are received anytime a broadcast block is executed in Scratch. The message data is a string of the message that was broadcast. An example broadcast message returned from `receive` looks like this:
110
+
Broadcast messages are received anytime a broadcast block is executed in
111
+
Scratch. The message data is a string of the message that was broadcast. An
112
+
example broadcast message returned from `receive` looks like this:
98
113
99
114
```
100
115
('broadcast', 'Hello, Python!')
101
116
```
102
117
103
118
#### Sensor updates
104
-
Sensor updates are received when global variables (i.e. variables created for all sprites) are created, or when their value changes. The message data is a dict that maps global variable names to their values. Suppose you created two variables, `foo` and `bar`. Upon their creation, `receive` would return a message that looks like this:
119
+
Sensor updates are received when global variables (i.e. variables created for
120
+
all sprites) are created, or when their value changes. The message data is a
121
+
dict that maps global variable names to their values. Suppose you created two
122
+
variables, `foo` and `bar`. Upon their creation, `receive` would return a
123
+
message that looks like this:
105
124
106
125
```
107
126
('sensor-update', {'foo': 0, 'bar': 0})
108
127
```
109
128
110
129
#### Handling Multiple Messages
111
-
`receive` returns only one message. If Scratch has sent more than one message, they will stay on the network buffer until `receive` is called again. To receive all the messages from Scratch you must repeatedly call `receive`. A nice way to handle this is to have a generator function that yields a message everytime it receives, and exits on error.
130
+
`receive` returns only one message. If Scratch has sent more than one message,
131
+
they will stay on the network buffer until `receive` is called again. To
132
+
receive all the messages from Scratch you must repeatedly call `receive`. A
133
+
nice way to handle this is to have a generator function that yields a message
134
+
everytime it receives, and exits on error.
112
135
113
136
```
114
137
def listen():
@@ -129,7 +152,8 @@ for msg in listen():
129
152
# code to handle sensor updates
130
153
```
131
154
132
-
If an error occurs or the connection to Scratch is closed, Python simply exits the loop.
155
+
If an error occurs or the connection to Scratch is closed, Python simply exits
156
+
the loop.
133
157
134
158
### Disconnecting
135
159
To close a connection to Scratch
@@ -138,16 +162,25 @@ To close a connection to Scratch
138
162
s.disconnect()
139
163
```
140
164
141
-
A disconnection may occur without an explicit call to `disconnect`. These usually happen when either Scratch is closed, remote sensor connections are disabled, or when there is something up with the network.
165
+
A disconnection may occur without an explicit call to `disconnect`. These
166
+
usually happen when either Scratch is closed, remote sensor connections are
167
+
disabled, or when there is something up with the network.
142
168
143
169
### Errors
144
-
There are two kinds of errors that can be caught in `connect`, `receive`, `broadcast`, and `sensorupdate`:
170
+
There are two kinds of errors that can be caught in `connect`, `receive`,
171
+
`broadcast`, and `sensorupdate`:
145
172
146
-
`ScratchError` is raised when there are errors with the network (e.g. connection refused, connection established, etc. basically any error from [errno](http://docs.python.org/2/library/errno.html)). The error message returned is the error message from `errno`.
173
+
`ScratchError` is raised when there are errors with the network (e.g.
174
+
connection refused, connection established, etc. basically any error from
175
+
[errno](http://docs.python.org/2/library/errno.html)). The error message
176
+
returned is the error message from `errno`.
147
177
148
-
`ScratchConnectionError` is raised when reading or writing data has no effect (i.e. reading from Scratch returns an empty string, or writing to Scratch writes 0 bytes). This is usually a sign that Scratch has been disconnected.
178
+
`ScratchConnectionError` is raised when reading or writing data has no effect
179
+
(i.e. reading from Scratch returns an empty string, or writing to Scratch
180
+
writes 0 bytes). This is usually a sign that Scratch has been disconnected.
149
181
150
-
If you do not care about the difference, you can just except `ScratchError` and it will also catch `ScratchConnectionError`.
182
+
If you do not care about the difference, you can just except `ScratchError` and
0 commit comments