Repository navigation
pybricks.tools: Add hostbuffer to receive IDE data - #246
Conversation
| memcpy(hostbuffer_instance->buffer + offset, data, size); | ||
| } | ||
|
|
||
| STATIC mp_obj_t pb_type_hostbuffer_get_bytes(mp_obj_t self_in) { |
There was a problem hiding this comment.
To keep things a simple as possible, maybe we don't need this method?
There was a problem hiding this comment.
I suppose that works. Giving full access to the bytes seemed more generic, i.e. the host could even dictate the format if it is unknown and send it as a zero terminated string at the start of the buffer.
|
Maybe we could call it something a little less technical like (Being more technical in the Pybricks BLE protocol side of things is good though) |
|
Thanks for the name suggestions. I was thinking this could be the base class for various applications, e.g. BallSensor, giving The challenge with |
I like it. |
Looking ahead, since Or possibly something like Higher level uses cases that could make use of a lower level
|
Makes sense. # rx-only
app = AppData("b")
# same as
app = AppData(rx="b")
# tx-only
app = AppData(tx="b")
# bidirectional
app = AppData("b", "b")
# same as
app = AppData(rx="b", tx="b")
# get latest
x, = app.rx()
# send
app.tx(1) |
|
Depending on the buffer sizes we think could be used, it might be nice to somehow specify an offset, index, or slice, or something like it when reading and writing so you only have to send relevant or new data. For example |
|
Not as pretty, but can already do that if it returns a tuple: distance = app.rx()[3] |
|
That's how it currently implemented, and probably fine for reading if we assume buffers will stay relatively small. For sending, the buffer may potentially be larger than the MTU, so some kind of index or offset could be needed. It could be the data points of a SLAM model or something. Unless that could just be a separate use case, with sequential output like data logging instead of a fixed size buffer. |
could be the sending side of this. Since the PC has infinite resources, we don't have to pre-allocate any buffers or use offsets. It would pretty much work like raw standard output, but it wouldn't be printed to the output pane. |
|
Are there any builds/versions where the AppData is also implemented for the bidirectional transfer? As far as I understand current implementation of |
|
That is not implemented yet. It would probably work a bit like In the firmware, it could perhaps have a separate buffer, similar to
|
Since this is only transmitting/receiving a fixed sized state, no ring buffer should be needed. If we want to guarantee that every state change is sent, the write should be a blocking/awaitable function. The ring buffer for stdout is mainly to improve throughput of |
Excellent point, thanks. Yes, this should keep it fairly simple. |
Since the Move Hub (MTU 23) probably won't support these features anyway, we could keep this even simpler by restricting outgoing messages to @afarago , what is the largest message you would typically need for a single portview information message? Is |
Now that we know a bit more about the planned features in #246, we can use improved terminology.
|
Just pushed an in-progress version that includes sending. The following works: from pybricks.tools import AppData
app = AppData("bb")
print(app.get_values())
print("Seen on stdout...")
app.write_bytes(b"Won't see this, but it will be sent!") # This is also awaitable
Each line is also guaranteed to be sent in one chunk rather than occasionally buffered like I'd like to clean up the firmware side implementation a bit though. Not entirely happy with how it works with btstack at the moment. |
I think so. |
Now that we know a bit more about the planned features in #246, we can use improved terminology.
|
Sending longer bStatus_t ATT_HandleValueNoti(uint16_t connHandle, attHandleValueNoti_t *pNoti) {
uint8_t buf[32]; // <------- This seems very small. Should it be 158 - 3 + 5 = 160?
buf[0] = connHandle & 0xFF;
buf[1] = (connHandle >> 8) & 0xFF;
buf[2] = 0; // authenticated link not required
buf[3] = pNoti->handle & 0xFF;
buf[4] = (pNoti->handle >> 8) & 0xFF;
memcpy(&buf[5], pNoti->pValue, pNoti->len);
return HCI_sendHCICommand(ATT_CMD_HANDLE_VALUE_NOTI, buf, 5 + pNoti->len);
}What do you think, @dlech ? I'm also wondering if this could potentially also explain some other related printing issues. EDIT: If I change the |
|
Assuming that the central has a MTU >= the hub is probably a fairly safe assumption, but it is entirely possible that the central could be something like a Move Hub with max MTU of 23 bytes. So ideally, the As for the CC2640 chips, it could very well be that the 20 byte max payload for notifications is hard-coded in the Bluetooth chip firmware. But I do see a
The stdout buffer is chunked into 20 bytes ( |
👍 I think I'll adjust this PR to use
I was thinking of pybricks/support#1152, but maybe this is just a different issue. That should indeed be protected by the |
Now that we know a bit more about the planned features in #246, we can use improved terminology.
More isn't currently supported on some hubs.
Sometimes the class is just used for outgoing data, so receiving should be optional.
e6a6d59 to
ef9f7fd
Compare
|
Hello, I have a few questions about AppData. |
This adds a class that allows a user program to allocate a buffer for receiving data from an IDE.
It has similarities to receiving data over standard in, but it is a dedicated separate command. The data is not queued but (over)written to a fixed size buffer. This is a bit like downloading a program, but with user-readable data.
This is useful for applications where the buffer contains "sensor data"-like contents prepared by the IDE.
Usage is as follows:
Before merging this as-is, it might be useful to thinking about the complementary case as well, where the user program can write data back to the IDE. This could facilitate things like live sensor port view without messing with stdout.