Add key/value store for the beduino controller

This commit is contained in:
Joachim Stolberg 2022-06-09 21:46:56 +02:00
parent 6a71892799
commit deb9678b54
4 changed files with 226 additions and 3 deletions

60
beduino/kv_store.lua Normal file
View File

@ -0,0 +1,60 @@
--[[
TechAge
=======
Copyright (C) 2019-2022 Joachim Stolberg
AGPL v3
See LICENSE.txt for more information
K/V Store for the Beduino controller
]]--
local COSTS = 400
local function ta_kv_init(cpu_pos, address, regA, regB, regC)
local nvm = techage.get_nvm(cpu_pos)
nvm.kv_store = {}
return 1, COSTS
end
local function ta_kv_add(cpu_pos, address, regA, regB, regC)
local nvm = techage.get_nvm(cpu_pos)
local text = vm16.read_ascii(cpu_pos, regA, 32)
nvm.kv_store[text] = regB
return 1, COSTS
end
local function ta_kv_get(cpu_pos, address, regA, regB, regC)
local nvm = techage.get_nvm(cpu_pos)
local text = vm16.read_ascii(cpu_pos, regA, 32)
return nvm.kv_store[text] or 0, COSTS
end
local kvstore_c = [[
// Initialize the key/value store
func ta_kv_init() {
return system(0x140, 0);
}
// Add a key/value pair to the store
func ta_kv_add(key_str, value) {
return system(0x141, key_str, value);
}
// Returns the value for the given key string
func ta_kv_get(key_str) {
return system(0x142, key_str);
}
]]
minetest.register_on_mods_loaded(function()
if minetest.global_exists("beduino") and minetest.global_exists("vm16") then
beduino.lib.register_SystemHandler(0x140, ta_kv_init)
beduino.lib.register_SystemHandler(0x141, ta_kv_add)
beduino.lib.register_SystemHandler(0x142, ta_kv_get)
vm16.register_ro_file("beduino", "ta_kvstore.c", kvstore_c)
end
end)

View File

@ -413,8 +413,8 @@ dofile(MP.."/fusion_reactor/generator.lua")
dofile(MP.."/fusion_reactor/turbine.lua")
dofile(MP.."/fusion_reactor/ta5_pump.lua")
-- Beduino extensions
dofile(MP.."/beduino/kv_store.lua")
-- Prevent other mods from using IE
techage.IE = nil
function techage.icta_register_condition(key, tData) end

85
manuals/ta_iom.md Normal file
View File

@ -0,0 +1,85 @@
# Techage/Beduino I/O Module
I/O modules support the following functions:
### event
Every signal that is sent to an I/O module triggers an event on the controller.
Events can be queried using the `event()` function.
If the function returns the value `1`, one or more signals have been received.
Calling `event()` resets the event flag.
```c
event()
```
### read
Read a value from a remote techage block.
- *port* is the I/O module port number
- *cmnd* is the command, like `IO_STATE` (see example code "ta_cmnd.c")
```c
read(port, cmnd)
```
### send_cmnd
Send a command to a techage block (see [commands](https://github.com/joe7575/beduino/blob/main/BEPs/bep-005_ta_cmnd.md)).
- *port* is the I/O module port number
- *topic* is a number from the list of [Beduino commands](https://github.com/joe7575/beduino/blob/main/BEPs/bep-005_ta_cmnd.md)
- *payload* is an array or a string with additional information, depending on the command. If no additional commands are required, "" can be used.
```c
send_cmnd(port, topic, payload)
```
### request_data
Request information from a techage block (see [commands](https://github.com/joe7575/beduino/blob/main/BEPs/bep-005_ta_cmnd.md)).
- *port* is the I/O module port number
- *topic* is a number from the list of [Beduino commands](https://github.com/joe7575/beduino/blob/main/BEPs/bep-005_ta_cmnd.md)
- *payload* is an array or a string with additional information, depending on the command. If no additional commands are required, "" can be used.
- *resp* is an array for the response data. The array must be defined large enough to hold the response data.
```c
request_data(port, topic, payload, resp)
```
## Functions for TA4 Display and TA4 Display XL
### clear_screen
Clear the display.
- *port* is the I/O module port number
```c
clear_screen(port)
```
### append_line
Add a new line to the display.
- *port* is the I/O module port number
- *text* is the text for one line
```c
append_line(port, text)
```
### write_line
Overwrite a text line with the given string.
- *port* is the I/O module port number
- *row* ist the display line/row (1-5)
- *text* is the text for one line
```c
write_line(port, row, text)
```

78
manuals/ta_kvstore.md Normal file
View File

@ -0,0 +1,78 @@
# Techage/Beduino Key/Value Store
The key/value store simplifies the handling/comparison of strings.
The following example shows the use of the Key/Value Store, here to check the names from the Player Detector:
```c
import "ta_kvstore.c"
import "ta_iom.c"
var s[16];
func init() {
// Init and fill-up the k/v store
ta_kv_init();
ta_kv_add("singleplayer", 1);
ta_kv_add("Tom", 2);
ta_kv_add("Betty", 3);
ta_kv_add("Joe", 4);
}
func loop() {
var val;
if(event()) { // Signal from player detector received
request_data(5, 144, "", s); // Request player name from player detector
val = ta_kv_get(s); // Read value for the given name in 's'
if(val == 1) {
// do A...
} else if(val == 2) {
// do B...
} else if(val == 3) {
// do C...
}
}
}
```
Each controller has a key/value store that must be initialized via `ta_kv_init()` and filled via `ta_kv_add` before it can be used.
### ta_kv_init
Initializes the key/value store. Has to be called once at the beginning.
```c
ta_kv_init()
```
### ta_kv_add
Add a new key/value pair to the store.
- *key_str* is the string
- *value* is the value to be stored, which can be read again using the key string
```c
ta_kv_add(key_str, value)
```
### ta_kv_get
Read a value from thre store.
- *key_str* is the string
The function returns 0, if *key_str* is unknown.
```c
ta_kv_get(key_str)
```