lignite 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ # Default Bluetooth address for https://rubygems.org/gems/lignite
2
+
3
+ # Put this file to ~/.config/lignite-btaddr
4
+ # and edit the address to match your device.
5
+ # You can use `hcitool scan` to find the address.
6
+
7
+ 00:11:22:DD:EE:FF
data/data/sysops.yml ADDED
@@ -0,0 +1,290 @@
1
+ sysops:
2
+ BEGIN_DOWNLOAD:
3
+ desc: "Begin download (from PC to VM)"
4
+ value: 0x92
5
+ params:
6
+ -
7
+ name: SIZE
8
+ type: U32
9
+ dir: in
10
+ -
11
+ name: FILENAME
12
+ type: ZBYTES
13
+ dir: in
14
+ -
15
+ name: HANDLE
16
+ type: U8
17
+ dir: out
18
+
19
+ CONTINUE_DOWNLOAD:
20
+ desc: "Continue download (from PC to VM)"
21
+ value: 0x93
22
+ # EXAMPLE: no reply required (0x81)
23
+ params:
24
+ -
25
+ name: HANDLE
26
+ type: U8
27
+ dir: in
28
+ -
29
+ name: PAYLOAD
30
+ type: BYTES
31
+ dir: in
32
+ -
33
+ name: HANDLE
34
+ type: U8
35
+ dir: out
36
+
37
+ BEGIN_UPLOAD:
38
+ desc: "Begin upload (from VM to PC)"
39
+ value: 0x94
40
+ params:
41
+ -
42
+ name: BUF_SIZE
43
+ type: U16
44
+ dir: in
45
+ -
46
+ name: FILENAME
47
+ type: ZBYTES
48
+ dir: in
49
+ -
50
+ name: FILE_SIZE
51
+ type: U32
52
+ dir: out
53
+ -
54
+ name: HANDLE
55
+ type: U8
56
+ dir: out
57
+ -
58
+ name: PAYLOAD
59
+ type: BYTES
60
+ dir: out
61
+
62
+ CONTINUE_UPLOAD:
63
+ desc: "Continue upload (from VM to PC)"
64
+ value: 0x95
65
+ params:
66
+ -
67
+ name: HANDLE
68
+ type: U8
69
+ dir: in
70
+ -
71
+ name: BUF_SIZE
72
+ type: U16
73
+ dir: in
74
+ -
75
+ name: HANDLE
76
+ type: U8
77
+ dir: out
78
+ -
79
+ name: PAYLOAD
80
+ type: BYTES
81
+ dir: out
82
+
83
+ BEGIN_GETFILE:
84
+ desc: "datalog to PC"
85
+ value: 0x96
86
+ params:
87
+ -
88
+ name: BUF_SIZE
89
+ type: U16
90
+ dir: in
91
+ -
92
+ name: FILENAME
93
+ type: ZBYTES
94
+ dir: in
95
+ -
96
+ name: FILE_SIZE
97
+ type: U32
98
+ dir: out
99
+ -
100
+ name: HANDLE
101
+ type: U8
102
+ dir: out
103
+ -
104
+ name: PAYLOAD
105
+ type: BYTES
106
+ dir: out
107
+
108
+ CONTINUE_GETFILE:
109
+ desc: "Continue getting a dafalog file (from VM to PC)"
110
+ value: 0x97
111
+ params:
112
+ -
113
+ name: HANDLE
114
+ type: U8
115
+ dir: in
116
+ -
117
+ name: BUF_SIZE
118
+ type: U16
119
+ dir: in
120
+ -
121
+ name: FILE_SIZE
122
+ type: U32
123
+ dir: out
124
+ -
125
+ name: HANDLE
126
+ type: U8
127
+ dir: out
128
+ -
129
+ name: PAYLOAD
130
+ type: BYTES
131
+ dir: out
132
+
133
+ CLOSE_FILEHANDLE:
134
+ desc: "Close a file handle"
135
+ value: 0x98
136
+ params:
137
+ -
138
+ name: HANDLE
139
+ type: U8
140
+ dir: in
141
+ -
142
+ name: HASH
143
+ type: BYTES
144
+ desc: "WTF, hash?"
145
+ dir: in
146
+
147
+ LIST_FILES:
148
+ desc: "List files"
149
+ value: 0x99
150
+ params:
151
+ -
152
+ name: BUF_SIZE
153
+ type: U16
154
+ dir: in
155
+ -
156
+ name: FILENAME
157
+ type: ZBYTES
158
+ dir: in
159
+ -
160
+ name: LIST_SIZE
161
+ type: U32
162
+ dir: out
163
+ -
164
+ name: HANDLE
165
+ type: U8
166
+ dir: out
167
+ -
168
+ name: PAYLOAD
169
+ type: BYTES
170
+ dir: out
171
+
172
+ CONTINUE_LIST_FILES:
173
+ desc: "Continue listing files"
174
+ value: 0x9a
175
+ params:
176
+ -
177
+ name: HANDLE
178
+ type: U8
179
+ dir: in
180
+ -
181
+ name: BUF_SIZE
182
+ type: U16
183
+ dir: in
184
+ -
185
+ name: HANDLE
186
+ type: U8
187
+ dir: out
188
+ -
189
+ name: PAYLOAD
190
+ type: BYTES
191
+ dir: out
192
+
193
+ CREATE_DIR:
194
+ desc: "Create a directory"
195
+ value: 0x9b
196
+ params:
197
+ -
198
+ name: FILENAME
199
+ desc: "Full path"
200
+ type: ZBYTES
201
+ dir: in
202
+
203
+ DELETE_FILE:
204
+ desc: "Delete a file"
205
+ value: 0x9c
206
+ params:
207
+ -
208
+ name: FILENAME
209
+ desc: "Full path"
210
+ type: ZBYTES
211
+ dir: in
212
+
213
+ LIST_OPEN_HANDLES:
214
+ desc: "Get a list of open handles"
215
+ value: 0x9d
216
+ params:
217
+ -
218
+ name: HANDLE_BITS
219
+ desc: "bits indicating whether handles are busy (open) or not."
220
+ type: BYTES
221
+ dir: out
222
+
223
+ WRITE_MAILBOX:
224
+ # NO REPLY
225
+ desc: "Write to a mailbox"
226
+ value: 0x9e
227
+ params:
228
+ -
229
+ name: NAME_LENGTH
230
+ type: U8
231
+ dir: in
232
+ -
233
+ name: NAME
234
+ type: ZBYTES
235
+ dir: in
236
+ -
237
+ name: PAYLOAD_SIZE
238
+ type: U16
239
+ dir: in
240
+ -
241
+ name: PAYLOAD
242
+ type: BYTES
243
+ dir: in
244
+
245
+ BLUETOOTH_PIN:
246
+ desc: "Set the Bluetooth PIN code"
247
+ value: 0x9f
248
+ params:
249
+ -
250
+ name: MAC_LENGTH
251
+ desc: "Length of MAC, not including the NUL terminator"
252
+ type: U8
253
+ dir: in
254
+ -
255
+ name: MAC
256
+ type: BYTES
257
+ dir: in
258
+ -
259
+ name: PIN_LENGTH
260
+ desc: "Length of PIN, not including the NUL terminator"
261
+ type: U8
262
+ dir: in
263
+ -
264
+ name: PIN
265
+ type: BYTES
266
+ dir: in
267
+ -
268
+ name: MAC_LENGTH
269
+ desc: "Length of MAC, not including the NUL terminator"
270
+ type: U8
271
+ dir: out
272
+ -
273
+ name: MAC
274
+ type: BYTES
275
+ dir: out
276
+ -
277
+ name: PIN_LENGTH
278
+ desc: "Length of PIN, not including the NUL terminator"
279
+ type: U8
280
+ dir: out
281
+ -
282
+ name: PIN
283
+ type: BYTES
284
+ dir: out
285
+
286
+ ENTER_FW_UPDATE:
287
+ # NO REPLY
288
+ desc: "Enter Firmware update mode"
289
+ value: 0xa0
290
+ params: []
data/examples/hello.rb ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/ruby
2
+ require "lignite"
3
+
4
+ dc = Lignite::DirectCommands.new
5
+
6
+ dc.ui_draw_clean
7
+ dc.ui_draw_text(Lignite::FG_COLOR, 20, 40, "Hello, world!")
8
+ dc.ui_draw_update
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/ruby
2
+ require "lignite"
3
+
4
+ dc = Lignite::DirectCommands.new
5
+
6
+ dc.ui_write_led(Lignite::LED_RED_FLASH)
7
+ sleep 3
8
+ dc.ui_write_led(Lignite::LED_ORANGE_PULSE)
9
+ sleep 3
10
+ dc.ui_write_led(Lignite::LED_GREEN)
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/ruby
2
+ require "lignite"
3
+
4
+ dc = Lignite::DirectCommands.new
5
+
6
+ layer = 0
7
+ nos = Lignite::PORT_B | Lignite::PORT_C
8
+ speed = 100
9
+ step_begin = 0
10
+ step_do = 360
11
+ step_end = 0
12
+ brake = Lignite::COAST
13
+ dc.output_step_speed(layer, nos, speed,
14
+ step_begin, step_do, step_end, brake)
15
+ dc.output_ready(layer, nos)
16
+
17
+ speed = -100
18
+ dc.output_step_speed(layer, nos, speed,
19
+ step_begin, step_do, step_end, brake)
data/examples/sound.rb ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/ruby
2
+ require "lignite"
3
+
4
+ dc = Lignite::DirectCommands.new
5
+
6
+ dc.sound_tone(50, 880, 200)
7
+ dc.sound_ready
8
+ dc.sound_tone(50, 1760, 400)
9
+ dc.sound_ready
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/ruby
2
+ require "lignite"
3
+
4
+ sc = Lignite::SystemCommands.new
5
+
6
+ result = ""
7
+ name = "/home/root/lms2012/prjs/"
8
+ bufsize = 42 # to demonstrate chunking
9
+ fsize, handle, data = sc.list_files(bufsize, name)
10
+ loop do
11
+ result += data
12
+ fsize -= data.bytesize
13
+ break if fsize.zero?
14
+ handle, data = sc.continue_list_files(handle, bufsize)
15
+ end
16
+ puts result
data/lib/lignite.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "lignite/bytes"
2
+ require "lignite/logger"
3
+
4
+ require "lignite/assembler"
5
+ require "lignite/body_compiler"
6
+ require "lignite/connection"
7
+ require "lignite/connection/bluetooth"
8
+ require "lignite/connection/usb"
9
+ require "lignite/direct_commands"
10
+ require "lignite/message"
11
+ require "lignite/message_sender"
12
+ require "lignite/op_compiler"
13
+ require "lignite/rbf_object"
14
+ require "lignite/system_commands"
15
+ require "lignite/variables"
16
+
17
+ module Lignite
18
+ PORT_A = 1
19
+ PORT_B = 2
20
+ PORT_C = 4
21
+ PORT_D = 8
22
+
23
+ COAST = 0
24
+ BRAKE = 1
25
+
26
+ # A String of bytes (with Encoding::BINARY)
27
+ class ByteString < String
28
+ # empty class, just for documentation purposes
29
+ end
30
+ end
@@ -0,0 +1,49 @@
1
+ module Lignite
2
+ class Assembler
3
+ include Bytes
4
+ include Logger
5
+
6
+ SIGNATURE = "LEGO"
7
+ def image_header(image_size:, version:, object_count:, global_bytes:)
8
+ SIGNATURE + u32(image_size) + u16(version) + u16(object_count) +
9
+ u32(global_bytes)
10
+ end
11
+
12
+ def assemble(rb_filename, rbf_filename)
13
+ rb_text = File.read(rb_filename)
14
+ @objects = []
15
+ @global_bytes = 0
16
+
17
+ instance_eval(rb_text, rb_filename, 1)
18
+
19
+ File.open(rbf_filename, "w") do |f|
20
+ dummy_header = image_header(image_size:0, version: 0, object_count: 0, global_bytes: 0)
21
+ f.write(dummy_header)
22
+ @objects.each do |obj|
23
+ h = obj.header(f.tell)
24
+ f.write(h)
25
+ f.write(obj.body)
26
+ # align??
27
+ end
28
+ size = f.tell
29
+ f.pos = 0
30
+ header = image_header(image_size: size,
31
+ version: 109,
32
+ object_count: @objects.size,
33
+ global_bytes: @global_bytes)
34
+ f.write(header)
35
+ end
36
+ end
37
+
38
+ def vmthread(id, &body)
39
+ @locals = Variables.new
40
+ bodyc = BodyCompiler.new(@locals)
41
+ bodyc.instance_exec(&body)
42
+ bodyc.instance_exec { object_end }
43
+ logger.debug "VMTHREAD #{id}"
44
+ logger.debug " size #{bodyc.bytes.bytesize}"
45
+ logger.debug " " + hexdump(bodyc.bytes)
46
+ @objects << RbfObject.vmthread(body: bodyc.bytes, local_bytes: @locals.bytesize)
47
+ end
48
+ end
49
+ end