lancaster-university / codal-microbit-v2

CODAL target for the micro:bit v2.x series of devices
MIT License
44 stars 52 forks source link

Cannot convert PacketBuffer into/from ManagedString #89

Open tapanther opened 3 years ago

tapanther commented 3 years ago

According to the online API, a ManagedString can be created from a PacketBuffer. As I mentioned in #88 the PacketBuffer API is missing, but I would assume the conversion should be symmetric (afterall what good is sending a string if you can't receive it!)

This doesn't work on the latest commit on master though:

#include "MicroBit.h"

MicroBit uBit;

int main()
{
    uBit.init();
    uBit.radio.enable();

    ManagedString s = uBit.radio.datagram.recv(); // Error 1
    PacketBuffer p = ManagedString("This is a string"); // Error 2
}
Error 1: No viable conversion from 'codal::PacketBuffer' to 'codal::ManagedString'

Error 2: No viable conversion from 'codal::ManagedString' to 'codal::PacketBuffer'

There is a constructor from ManagedBuffer for , and with some rigamarole you can jump between the two, but it seems like there should be a more straightforward way to send and receive strings via radio.

martinwork commented 3 years ago

The DAL has a ManagedString(PacketBuffer) constructor, but in CODAL this is replaced with ManagedString(ManagedBuffer).

To send a string, there is int send(ManagedString data)

To receive a string, I think this will work:

PacketBuffer  p  = uBit.radio.datagram.recv();
ManagedString s = ManagedString( (const char *) p.getBytes(), p.length());

or

uint8_t buf[ MICROBIT_RADIO_MAX_PACKET_SIZE];
ManagedString s = ManagedString( buf, uBit.radio.datagram.recv( buf, MICROBIT_RADIO_MAX_PACKET_SIZE));

Possible enhancements...?