Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkerr committed Jul 24, 2015
0 parents commit 7de4980
Show file tree
Hide file tree
Showing 8 changed files with 1,565 additions and 0 deletions.
36 changes: 36 additions & 0 deletions AbstractPL1167.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* AbstractPL1167.h
*
* Created on: 29 May 2015
* Author: henryk
*/

#ifdef ARDUINO
#include "Arduino.h"
#else
#include <stdint.h>
#include <stdlib.h>
#endif

#ifndef ABSTRACTPL1167_H_
#define ABSTRACTPL1167_H_

class AbstractPL1167 {
public:
virtual int open() = 0;

virtual int setPreambleLength(uint8_t preambleLength) = 0;
virtual int setSyncword(uint16_t syncword0, uint16_t syncword3) = 0;
virtual int setTrailerLength(uint8_t trailerLength) = 0;
virtual int setMaxPacketLength(uint8_t maxPacketLength) = 0;
virtual int setCRC(bool crc) = 0;
virtual int writeFIFO(const uint8_t data[], size_t data_length) = 0;
virtual int transmit(uint8_t channel) = 0;
virtual int receive(uint8_t channel) = 0;
virtual int readFIFO(uint8_t data[], size_t &data_length) = 0;
};




#endif /* ABSTRACTPL1167_H_ */
675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

CC = g++
CFLAGS = -c -Wall
LIBS = -lrf24-bcm
SOURCES = PL1167_nRF24.cpp MiLightRadio.cpp openmilight.cpp
BIN = openmilight

all: $(SOURCES) $(BIN)

$(BIN): $(SOURCES:.cpp=.o)
$(CC) $^ -o $@ $(LIBS)

%.o: %.cpp
$(CC) $(CFLAGS) -o $@ $<

clean:
rm -f *.o $(BIN)

134 changes: 134 additions & 0 deletions MiLightRadio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* MiLightRadio.cpp
*
* Created on: 29 May 2015
* Author: henryk
*/

#include "MiLightRadio.h"

#define PACKET_ID(packet) ( ((packet[1] & 0xF0)<<24) | (packet[2]<<16) | (packet[3]<<8) | (packet[7]) )

static const uint8_t CHANNELS[] = {9, 40, 71};
#define NUM_CHANNELS (sizeof(CHANNELS)/sizeof(CHANNELS[0]))

MiLightRadio::MiLightRadio(AbstractPL1167 &pl1167)
: _pl1167(pl1167) {
_waiting = false;
}

int MiLightRadio::begin()
{
int retval = _pl1167.open();
if (retval < 0) {
return retval;
}

retval = _pl1167.setCRC(true);
if (retval < 0) {
return retval;
}

retval = _pl1167.setPreambleLength(3);
if (retval < 0) {
return retval;
}

retval = _pl1167.setTrailerLength(4);
if (retval < 0) {
return retval;
}

retval = _pl1167.setSyncword(0x147A, 0x258B);
if (retval < 0) {
return retval;
}

retval = _pl1167.setMaxPacketLength(8);
if (retval < 0) {
return retval;
}

available();

return 0;
}

bool MiLightRadio::available()
{
if (_waiting) {
return true;
}

if (_pl1167.receive(CHANNELS[0]) > 0) {
size_t packet_length = sizeof(_packet);
if (_pl1167.readFIFO(_packet, packet_length) < 0) {
return false;
}
if (packet_length == 0 || packet_length != _packet[0] + 1U) {
return false;
}

uint32_t packet_id = PACKET_ID(_packet);
if (packet_id == _prev_packet_id) {
_dupes_received++;
} else {
_prev_packet_id = packet_id;
_waiting = true;
}
}

return _waiting;
}

int MiLightRadio::dupesReceived()
{
return _dupes_received;
}


int MiLightRadio::read(uint8_t frame[], size_t &frame_length)
{
if (!_waiting) {
frame_length = 0;
return -1;
}

if (frame_length > sizeof(_packet) - 1) {
frame_length = sizeof(_packet) - 1;
}

if (frame_length > _packet[0]) {
frame_length = _packet[0];
}

memcpy(frame, _packet + 1, frame_length);
_waiting = false;

return _packet[0];
}

int MiLightRadio::write(uint8_t frame[], size_t frame_length)
{
if (frame_length > sizeof(_out_packet) - 1) {
return -1;
}

memcpy(_out_packet + 1, frame, frame_length);
_out_packet[0] = frame_length;

int retval = resend();
if (retval < 0) {
return retval;
}
return frame_length;
}

int MiLightRadio::resend()
{
for (size_t i = 0; i < NUM_CHANNELS; i++) {
_pl1167.writeFIFO(_out_packet, _out_packet[0] + 1);
_pl1167.transmit(CHANNELS[i]);
}
return 0;
}
41 changes: 41 additions & 0 deletions MiLightRadio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* MiLightRadio.h
*
* Created on: 29 May 2015
* Author: henryk
*/

#ifdef ARDUINO
#include "Arduino.h"
#else
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#endif

#include "AbstractPL1167.h"

#ifndef MILIGHTRADIO_H_
#define MILIGHTRADIO_H_

class MiLightRadio {
public:
MiLightRadio(AbstractPL1167 &pl1167);
int begin();
bool available();
int read(uint8_t frame[], size_t &frame_length);
int dupesReceived();
int write(uint8_t frame[], size_t frame_length);
int resend();
private:
AbstractPL1167 &_pl1167;
uint32_t _prev_packet_id;

uint8_t _packet[8], _out_packet[8];
bool _waiting;
int _dupes_received;
};



#endif /* MILIGHTRADIO_H_ */
Loading

0 comments on commit 7de4980

Please sign in to comment.