Convert Hotdog sketch to PlatformIO

This commit is contained in:
2023-12-11 07:11:17 +11:00
parent 981c28f72e
commit f80cc9a6b9
15 changed files with 502 additions and 395 deletions

View File

@@ -0,0 +1,34 @@
#ifndef PACKETBUILDER_H
#define PACKETBUILDER_H
#include <Arduino.h>
#include <PacketCRC.h>
class PacketBuilder {
public:
PacketBuilder(uint16_t address, uint8_t rollingCode1, uint8_t rollingCode2);
~PacketBuilder();
void buildUpPacket(uint8_t *buffer, uint16_t shadeID);
void buildDownPacket(uint8_t *buffer, uint16_t shadeID);
void buildStopPacket(uint8_t *buffer, uint16_t shadeID);
void buildPositionPacket(uint8_t *buffer, uint16_t shadeID, float percentage);
private:
uint16_t address;
uint8_t rollingCode1;
uint8_t rollingCode2;
PacketCRC packetCRC;
void setPacketSize(uint8_t *buffer, uint8_t);
void setConstants(uint8_t *buffer);
void setSourceAddress(uint8_t *buffer, uint16_t);
void setDestinationAddress(uint8_t *buffer, uint16_t);
void setRollingCodes(uint8_t *buffer);
void setProtocolVersion(uint8_t *buffer, uint8_t);
void calculateCRC(uint8_t *buffer);
void incrementRollingCodes();
};
#endif // PACKETBUILDER_H

View File

@@ -0,0 +1,18 @@
{
"name": "PacketBuilder",
"version": "0.0.1",
"description": "A tool for building PowerView packets",
"keywords": "powerview, hunterdouglas",
"authors":
[
{
"name": "Matt Way",
"email": "mattyway@gmail.com"
}
],
"license": "MIT",
"dependencies": {
"PacketCRC": "*"
},
"frameworks": "arduino"
}

View File

@@ -0,0 +1,149 @@
#include "PacketBuilder.h"
PacketBuilder::PacketBuilder(uint16_t address, uint8_t rollingCode1, uint8_t rollingCode2) :
address(address),
rollingCode1(rollingCode1),
rollingCode2(rollingCode2)
{
}
PacketBuilder::~PacketBuilder()
{
}
void PacketBuilder::buildUpPacket(uint8_t *buffer, uint16_t shadeID)
{
setPacketSize(buffer, 0x11);
setConstants(buffer);
setProtocolVersion(buffer, 0x06);
setSourceAddress(buffer, address);
setDestinationAddress(buffer, shadeID);
setRollingCodes(buffer);
buffer[16] = 0x52; // ?
buffer[17] = 0x55; // Command
buffer[18] = 0x00; // Empty?
calculateCRC(buffer);
incrementRollingCodes();
}
void PacketBuilder::buildDownPacket(uint8_t *buffer, uint16_t shadeID)
{
setPacketSize(buffer, 0x11);
setConstants(buffer);
setProtocolVersion(buffer, 0x06);
setSourceAddress(buffer, address);
setDestinationAddress(buffer, shadeID);
setRollingCodes(buffer);
buffer[16] = 0x52; // ?
buffer[17] = 0x44; // Command
buffer[18] = 0x00; // Empty?
calculateCRC(buffer);
incrementRollingCodes();
}
void PacketBuilder::buildStopPacket(uint8_t *buffer, uint16_t shadeID)
{
setPacketSize(buffer, 0x11);
setConstants(buffer);
setProtocolVersion(buffer, 0x06);
setSourceAddress(buffer, address);
setDestinationAddress(buffer, shadeID);
setRollingCodes(buffer);
buffer[16] = 0x52; // ?
buffer[17] = 0x53; // Command
buffer[18] = 0x00; // Empty?
calculateCRC(buffer);
incrementRollingCodes();
}
void PacketBuilder::buildPositionPacket(uint8_t *buffer, uint16_t shadeID, float percentage)
{
setPacketSize(buffer, 0x15);
setConstants(buffer);
setProtocolVersion(buffer, 0x05);
setSourceAddress(buffer, address);
setDestinationAddress(buffer, shadeID);
setRollingCodes(buffer);
buffer[16] = 0x3F; // Command
buffer[17] = 0x5A; // Empty?
buffer[18] = 0x04; // Constant in this size packet?
buffer[19] = 0x40; // Constant in this size packet?
buffer[20] = 0x50; // Constant in this size packet?
uint16_t position = (uint16_t)(0xFFFF * percentage);
buffer[21] = (uint8_t)(position & 0x00FF);
buffer[22] = (uint8_t)((position & 0xFF00) >> 8);
calculateCRC(buffer);
incrementRollingCodes();
}
void PacketBuilder::setPacketSize(uint8_t *buffer, uint8_t length) {
buffer[1] = length; // Packet size
}
void PacketBuilder::setConstants(uint8_t *buffer) {
buffer[0] = 0xC0; // Header byte
buffer[2] = 0x00; // Constant when sending, can be 0x10 when receiving a packet
buffer[3] = 0x05; // Constant
buffer[5] = 0xFF; // Constant
buffer[6] = 0xFF; // Constant
buffer[9] = 0x86; // Constant?
}
void PacketBuilder::setSourceAddress(uint8_t *buffer, uint16_t sourceID) {
// Physical source address (could be the address of a repeater when receiving a packet)
buffer[7] = (uint8_t)((sourceID & 0xFF00) >> 8);
buffer[8] = (uint8_t)(sourceID & 0x00FF);
// Logical source address (usually the same as the physical source address)
buffer[14] = (uint8_t)((sourceID & 0xFF00) >> 8);
buffer[15] = (uint8_t)(sourceID & 0x00FF);
}
void PacketBuilder::setDestinationAddress(uint8_t *buffer, uint16_t targetID) {
// Logical target address
buffer[12] = (uint8_t)((targetID & 0xFF00) >> 8);
buffer[13] = (uint8_t)(targetID & 0x00FF);
}
void PacketBuilder::setRollingCodes(uint8_t *buffer) {
buffer[4] = rollingCode1; // Rolling code 1
buffer[11] = rollingCode2; // Rolling code 2
}
void PacketBuilder::setProtocolVersion(uint8_t *buffer, uint8_t version) {
buffer[10] = version; // Protocol version?
}
void PacketBuilder::calculateCRC(uint8_t *buffer) { // must be called after the buffer has been filled
uint8_t length = buffer[1];
uint16_t result = packetCRC.calculate(buffer);
uint8_t checksum1 = (uint8_t)((result & 0xFF00) >> 8);
uint8_t checksum2 = (uint8_t)(result & 0x00FF);
buffer[length + 2] = checksum1; // Checksum
buffer[length + 3] = checksum2; // Checksum
}
void PacketBuilder::incrementRollingCodes() {
rollingCode1++;
rollingCode2++;
}

46
lib/README Normal file
View File

@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html