B4R Question Wrapping Radiohead RH_Serial

janderkan

Well-Known Member
Licensed User
Longtime User
Hi
I have succesfully wrapped RH_ASK class in Radiohead library by cut and paste Erels RFM95 class :)

Now I need the RH_Serial class, but it needs a Hardware serial in the constructor.
I managed to compile it using a Esp8266 but get a hardware reset when I try to send data.

This is my addition to rRadiohead.h

B4X:
    class B4RRHSERIAL {

        private:     
            uint8_t backend[sizeof(RH_Serial)];
            RH_Serial* rhserial;
            PollerNode pnode;
            SubVoidByteArray DataAvailableSub;
            Byte buffer[RH_SERIAL_MAX_PAYLOAD_LEN];
            static void looper(void* b);
        
        public:                 
            bool Initialize(SubVoidByteArray DataAvailableSub, byte ThisAddress);
            bool Send(ArrayByte* Data);
    };

and this is a new rRHSerial.cpp

B4X:
#include "B4RDefines.h"

namespace B4R {
    bool B4RRHSERIAL::Initialize(SubVoidByteArray DataAvailableSub, byte ThisAddress) {
        this->DataAvailableSub = DataAvailableSub;
        FunctionUnion fu;
        fu.PollerFunction = looper;
        pnode.functionUnion = fu;
        pnode.tag = this;

        HardwareSerial MySerial(0);
        MySerial.begin(115200);
        rhserial = new (backend) RH_Serial(MySerial);

        bool r = rhserial->init();
        if (r) {
        if (pnode.next == NULL)
                pollers.add(&pnode);
        }
        return r;
    }

    void B4RRHSERIAL::looper(void* b) {
        B4RRHSERIAL* me = (B4RRHSERIAL*)b;
        
        if (me->rhserial->available()) {
            const UInt cp = B4R::StackMemory::cp;
            ArrayByte* arrPayload = CreateStackMemoryObject(ArrayByte);
            arrPayload->data = me->buffer;
            Byte len = RH_SERIAL_MAX_PAYLOAD_LEN;
            me->rhserial->recv(me->buffer, &len);
            arrPayload->length = len;
            me->DataAvailableSub(arrPayload);
            B4R::StackMemory::cp = cp;
        }
    }

    bool B4RRHSERIAL::Send(Array* Data) {
        return rhserial->send((Byte*)Data->data, Data->length);
    }
}

Please help me......
 
Top