Skip to content

Commit d2aed46

Browse files
committed
memory driver updated. implement simple JSON read/write to wyldcard memory
1 parent bad6bc5 commit d2aed46

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

napi-rust-drivers/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ napi = { version = "2.12.1", default-features = false, features = ["napi4"] }
1414
napi-derive = "2.12.2"
1515
embedded-hal = { version = "0.2.7", features = ["unproven"] }
1616
rppal = { version = "0.12.0", features = ["hal","hal-unproven"] }
17-
m95320 = "1.0.2"
17+
m95320 = "1.0.3"
1818
shared-bus = "0.2.4"
1919
signal-hook = "0.3.9"
2020
rand = "0.8.0"

napi-rust-drivers/demo.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async function main() {
4949
let getData = function(well) {
5050
return () => {
5151
let data = well.getData()
52-
console.log('data for card in well', well.id, data)
52+
return data
5353
}
5454
}
5555

@@ -58,7 +58,6 @@ async function main() {
5858
let buf = Buffer.alloc(4096, ' '.charCodeAt(0))
5959
buf[0] = '{'.charCodeAt(0)
6060
buf[1] = '}'.charCodeAt(0)
61-
console.log(buf.toString())
6261
well._writeMemory(buf)
6362
console.log('erased well', well.id)
6463
}
@@ -70,11 +69,11 @@ async function main() {
7069
// })
7170

7271
erase(plinth.wells[0])
73-
console.log('erased. now getting data')
74-
getData(plinth.wells[0])()
75-
72+
console.log(plinth.wells[0].getData())
7673
plinth.wells[0].storeData({ hello: 'chukwudi'})
77-
getData(plinth.wells[0])()
74+
console.log(plinth.wells[0].getData())
75+
plinth.wells[0].storeData({ hello: 'chukwudi', ohyeah: 'coolaid'})
76+
console.log(plinth.wells[0].getData())
7877
}
7978

8079
main()

napi-rust-drivers/plinth.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ class Well {
4242
}
4343

4444
storeData = function(object) {
45-
this._writeMemory(Buffer.from(JSON.stringify(object)))
45+
let text = JSON.stringify(object)
46+
if (text.length > this.maxMemory) {
47+
throw new Error(`attempted to store too much data. JSON stringified data is of length ${text.length}, which is more than the maximum of ${this.maxMemory}. You could try using the _writeMemory and _readMemory functions directly, storing data in binary rather than ascii JSON.`)
48+
}
49+
50+
let buf = Buffer.alloc(4096, ' ')
51+
buf.write(text)
52+
53+
this._writeMemory(buf)
4654
}
4755

4856
_readMemory = function() {

0 commit comments

Comments
 (0)