Giter VIP home page Giter VIP logo

radiopacket's Introduction

RadioPacket

A packet format for sending/receiving data using the Arduino Manchester library.

lint status

using RadioPacket;

Message m(arr, sizeof(arr));
m.setRawAction(UPDATE_DB_CMD);

RadioPacket p(&m);
p.setRawTransmitterId(TRANSMITTER_ID);
p.setRawReceiverId(RECEIVER_ID);
p.setRawCrc8(p.generateChecksum());

man.transmitArray(
    p.getRawPacketLength(),
    const_cast<uint8_t*>(p.getData()));
RadioPacket* p;
Message* m;

if(RadioPacket::parse(&p, buffer, BUFFER_SIZE) == RadioPacket::PARSE_OK) {
    if(Message::parse(&m, p->getBodyData(), p->getRawBodyLength()) == Message::PARSE_OK) {
        Serial.println(m->getBodyData());
    }
}

delete p;
delete m;

Packet Format

0 1 2 4 6

[PACKET LENGTH][VERSION][TRANSMITTER ID][RECEIVER ID]

6 7 8 9

[FRAGMENT][BODY LENGTH][CRC8]

9

[BODY DATA]

Message Format

0 2 4

[BODY LENGTH][ACTION]

4

[BODY DATA]

Extending Messages

// MeasurementMessage.h

#include <RadioPacket.h>

/**
 * MeasurementMessage holds a single 16 bit integer
 */
class MeasurementMessage : public RadioPacket::Message {

protected:
    static const uint8_t _MEASUREMENT_OFFSET = 0;
    static const uint8_t _ACTION_VALUE = 1;

public:
    MeasurementMessage() : Message() {
        this->setRawAction(_ACTION_VALUE);
        this->resizeBody(sizeof(uint16_t));
    }

    uint16_t getMeasurement() const noexcept {
        return this->_data.getUInt16(this->fromBaseBodyOffset(
            _MEASUREMENT_OFFSET));
    }

    void setMeasurement(const uint16_t m) noexcept {
        this->_data.setUInt16(m, this->fromBaseBodyOffset(
            _MEASUREMENT_OFFSET));
    }

};
// example use of MeasurementMessage
// read analog value of A1 into message object
MeasurementMessage mm;
mm.setMeasurement(::analogRead(A1));

// then encapsulate the message into a RadioPacket as
// in the example transmitter code above

// when receiving, cast Message to a MeasurementMessage,
// where m is a Message* as in the example receiver code above
MeasurementMessage* mm = static_cast<MeasurementMessage*>(m);
mm->getMeasurement();

radiopacket's People

Contributors

endail avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.