Initial commit

This commit is contained in:
Nigreon 2025-02-22 23:27:11 +01:00
commit d377fa89a4
13 changed files with 2364 additions and 0 deletions

345
src/DLinkedList.h Normal file
View file

@ -0,0 +1,345 @@
/*
LinkedList.h - V1.1 - Generic LinkedList implementation
Works better with FIFO, because LIFO will need to
search the entire List to find the last one;
For instructions, go to https://github.com/ivanseidel/LinkedList
Created by Ivan Seidel Gomes, March, 2013.
Released into the public domain.
*/
#ifndef DLinkedList_h
#define DLinkedList_h
#include <stddef.h>
template <class T>
struct DListNode
{
T data;
DListNode<T> *next;
};
template <typename T>
class DLinkedList
{
protected:
int _size;
DListNode<T> *root;
DListNode<T> *last;
// Helps "get" method, by saving last position
DListNode<T> *lastNodeGot;
int lastIndexGot;
// isCached should be set to FALSE
// everytime the list suffer changes
bool isCached;
DListNode<T> *getNode(int index);
public:
DLinkedList();
~DLinkedList();
/*
Returns current size of LinkedList
*/
virtual int size();
/*
Adds a T object in the specified index;
Unlink and link the LinkedList correcly;
Increment _size
*/
virtual bool add(int index, T);
/*
Adds a T object in the end of the LinkedList;
Increment _size;
*/
virtual bool add(T);
/*
Adds a T object in the start of the LinkedList;
Increment _size;
*/
virtual bool unshift(T);
/*
Set the object at index, with T;
Increment _size;
*/
virtual bool set(int index, T);
/*
Remove object at index;
If index is not reachable, returns false;
else, decrement _size
*/
virtual T remove(int index);
/*
Remove last object;
*/
virtual T pop();
/*
Remove first object;
*/
virtual T shift();
/*
Get the index'th element on the list;
Return Element if accessible,
else, return false;
*/
virtual T get(int index);
/*
Clear the entire array
*/
virtual void clear();
};
// Initialize LinkedList with false values
template <typename T>
DLinkedList<T>::DLinkedList()
{
root = NULL;
last = NULL;
_size = 0;
lastNodeGot = root;
lastIndexGot = 0;
isCached = false;
}
// Clear Nodes and free Memory
template <typename T>
DLinkedList<T>::~DLinkedList()
{
DListNode<T> *tmp;
while (root != NULL)
{
tmp = root;
root = root->next;
delete tmp;
}
last = NULL;
_size = 0;
isCached = false;
}
/*
Actualy "logic" coding
*/
template <typename T>
DListNode<T> *DLinkedList<T>::getNode(int index)
{
int _pos = 0;
DListNode<T> *current = root;
// Check if the node trying to get is
// immediatly AFTER the previous got one
if (isCached && lastIndexGot <= index)
{
_pos = lastIndexGot;
current = lastNodeGot;
}
while (_pos < index && current)
{
current = current->next;
_pos++;
}
// Check if the object index got is the same as the required
if (_pos == index)
{
isCached = true;
lastIndexGot = index;
lastNodeGot = current;
return current;
}
return NULL;
}
template <typename T>
int DLinkedList<T>::size()
{
return _size;
}
template <typename T>
bool DLinkedList<T>::add(int index, T _t)
{
if (index >= _size)
return add(_t);
if (index == 0)
return unshift(_t);
DListNode<T> *tmp = new DListNode<T>(),
*_prev = getNode(index - 1);
tmp->data = _t;
tmp->next = _prev->next;
_prev->next = tmp;
_size++;
isCached = false;
return true;
}
template <typename T>
bool DLinkedList<T>::add(T _t)
{
DListNode<T> *tmp = new DListNode<T>();
tmp->data = _t;
tmp->next = NULL;
if (root)
{
// Already have elements inserted
last->next = tmp;
last = tmp;
}
else
{
// First element being inserted
root = tmp;
last = tmp;
}
_size++;
isCached = false;
return true;
}
template <typename T>
bool DLinkedList<T>::unshift(T _t)
{
if (_size == 0)
return add(_t);
DListNode<T> *tmp = new DListNode<T>();
tmp->next = root;
tmp->data = _t;
root = tmp;
_size++;
isCached = false;
return true;
}
template <typename T>
bool DLinkedList<T>::set(int index, T _t)
{
// Check if index position is in bounds
if (index < 0 || index >= _size)
return false;
getNode(index)->data = _t;
return true;
}
template <typename T>
T DLinkedList<T>::pop()
{
if (_size <= 0)
return T();
isCached = false;
if (_size >= 2)
{
DListNode<T> *tmp = getNode(_size - 2);
T ret = tmp->next->data;
delete (tmp->next);
tmp->next = NULL;
last = tmp;
_size--;
return ret;
}
else
{
// Only one element left on the list
T ret = root->data;
delete (root);
root = NULL;
last = NULL;
_size = 0;
return ret;
}
}
template <typename T>
T DLinkedList<T>::shift()
{
if (_size <= 0)
return T();
if (_size > 1)
{
DListNode<T> *_next = root->next;
T ret = root->data;
delete (root);
root = _next;
_size--;
isCached = false;
return ret;
}
else
{
// Only one left, then pop()
return pop();
}
}
template <typename T>
T DLinkedList<T>::remove(int index)
{
if (index < 0 || index >= _size)
{
return T();
}
if (index == 0)
return shift();
if (index == _size - 1)
{
return pop();
}
DListNode<T> *tmp = getNode(index - 1);
DListNode<T> *toDelete = tmp->next;
T ret = toDelete->data;
tmp->next = tmp->next->next;
delete (toDelete);
_size--;
isCached = false;
return ret;
}
template <typename T>
T DLinkedList<T>::get(int index)
{
DListNode<T> *tmp = getNode(index);
return (tmp ? tmp->data : T());
}
template <typename T>
void DLinkedList<T>::clear()
{
while (size() > 0)
shift();
}
#endif

63
src/Dictionary.h Normal file
View file

@ -0,0 +1,63 @@
#include <DLinkedList.h>
template <class T, class U>
class Dictionary
{
private:
DLinkedList<T> KeyList = DLinkedList<T>();
DLinkedList<U> ValList = DLinkedList<U>();
public:
void set(T key, U val)
{
for (int i = 0; i < KeyList.size(); i++)
{
if (KeyList.get(i) == key) {
ValList.set(i, val);
return;
}
}
KeyList.add(key);
ValList.add(val);
}
U get(T key)
{
for (int i = 0; i < KeyList.size(); i++)
{
if (KeyList.get(i) == key)
{
return ValList.get(i);
}
}
}
T getKey(U val)
{
for (int i = 0; i < ValList.size(); i++)
{
if (ValList.get(i) == val)
{
return KeyList.get(i);
}
}
}
int length()
{
return KeyList.size();
}
bool contains(T key) {
for (int x = 0; x < length(); x++) {
if(KeyList.get(x) == key) {
return true;
}
}
return false;
}
T getKeyByIndex(int index) {
return KeyList.get(index);
}
};

132
src/main.ino Normal file
View file

@ -0,0 +1,132 @@
#include <ArduinoJson.h>
#include <rtl_433_ESP.h>
#include <RCSwitch.h>
#include "Dictionary.h"
#define _DICT_PACK_STRUCTURES
#define LED 8
CC1101 radiotx = RADIO_LIB_MODULE;
RCSwitch mySwitch = RCSwitch();
#ifndef RF_MODULE_FREQUENCY
# define RF_MODULE_FREQUENCY 433.92
#endif
#define JSON_MSG_BUFFER 512
#define MINDELAY 10L
Dictionary<String, unsigned long> d1;
char messageBuffer[JSON_MSG_BUFFER];
rtl_433_ESP rf; // use -1 to disable transmitter
int count = 0;
void rtl_433_Callback(char* message) {
JsonDocument jsonDocument;
deserializeJson(jsonDocument,message);
logJson(jsonDocument);
count++;
}
void logJson(JsonDocument jsondata) {
if(jsondata["model"].is<String>())
{
String ref = jsondata["model"];
if(jsondata["channel"].is<int>()) { ref = ref + jsondata["channel"].as<String>(); }
if(jsondata["id"].is<int>()) { ref = ref + jsondata["id"].as<String>(); }
if(!d1.contains(ref.c_str()) || millis() > (d1.get(ref.c_str()) + (MINDELAY * 1000)) || d1.get(ref.c_str()) > millis()) {
//Serial.println(ref.c_str());
//if(d1.contains(ref.c_str())) { Serial.println(d1.get(ref.c_str())); }
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
char JSONmessageBuffer[measureJson(jsondata) + 1];
serializeJson(jsondata, JSONmessageBuffer, measureJson(jsondata) + 1);
#else
char JSONmessageBuffer[JSON_MSG_BUFFER];
serializeJson(jsondata, JSONmessageBuffer, JSON_MSG_BUFFER);
#endif
d1.set(ref.c_str(), millis());
ledblink();
Serial.println(JSONmessageBuffer);
}
}
}
void ledblink() {
digitalWrite(LED, LOW);
delay(150);
digitalWrite(LED, HIGH);
}
void setup() {
Serial.begin(115200);
delay(1000);
for (int i=0 ; i<10; i++) {
Serial.print("Hello");
delay(1000);
}
if(SS != 21 || MOSI != 20 || MISO != 10 || SCK != 7)
{
for ( ; ; ) {
Serial.println("Please define pin assignment for SPI in pins_arduino.h (~/.platformio/packages/framework-arduinoespressif32/variants/esp32c3) / SS: 21 MOSI : 20 MISO : 10 SCK : 7");
delay(1000);
}
}
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
rf.initReceiver(RF_MODULE_RECEIVER_GPIO, RF_MODULE_FREQUENCY);
rf.setCallback(rtl_433_Callback, messageBuffer, JSON_MSG_BUFFER);
rf.enableReceiver();
rf.getModuleStatus();
//d = new Dictionary(25);
//d("aa", "bb");
//if(d1("aa")) { Serial.println("toto"); }
}
void loop() {
rf.loop();
if (Serial.available())
{
// Read the JSON document from the "link" serial port
JsonDocument doc;
DeserializationError err = deserializeJson(doc, Serial);
if (err == DeserializationError::Ok)
{
Serial.print("cmd = ");
Serial.println(doc["cmd"].as<String>());
Serial.print("value = ");
Serial.println(doc["value"].as<uint32_t>());
if(doc["cmd"].as<String>().compareTo(String("rcsend"))==0) {
Serial.println("Transmit RCSwitch");
ledblink();
//rf.getModuleStatus();
rf.disableReceiver();
radiotx.SPIsendCommand(RADIOLIB_CC1101_CMD_TX);
mySwitch.enableTransmit(RF_MODULE_GDO0);
mySwitch.setRepeatTransmit(8);
mySwitch.send(doc["value"].as<uint32_t>(), 24);
mySwitch.disableTransmit();
radiotx.SPIsendCommand(RADIOLIB_CC1101_CMD_RX);
rf.enableReceiver();
//rf.getModuleStatus();
}
}
else
{
// Print error to the "debug" serial port
Serial.print("deserializeJson() returned ");
Serial.println(err.c_str());
// Flush all bytes in the "link" serial port buffer
while (Serial.available() > 0)
Serial.read();
}
}
}

39
src/pinsME_arduino.h Normal file
View file

@ -0,0 +1,39 @@
#ifndef PinsME_Arduino_h
#define PinsME_Arduino_h
#include <stdint.h>
#include "soc/soc_caps.h"
#define EXTERNAL_NUM_INTERRUPTS 22
#define NUM_DIGITAL_PINS 22
#define NUM_ANALOG_INPUTS 6
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+8;
#define BUILTIN_LED LED_BUILTIN // backward compatibility
#define LED_BUILTIN LED_BUILTIN
#define RGB_BUILTIN LED_BUILTIN
#define RGB_BRIGHTNESS 64
#define analogInputToDigitalPin(p) (((p)<NUM_ANALOG_INPUTS)?(analogChannelToDigitalPin(p)):-1)
#define digitalPinToInterrupt(p) (((p)<NUM_DIGITAL_PINS)?(p):-1)
#define digitalPinHasPWM(p) (p < EXTERNAL_NUM_INTERRUPTS)
static const uint8_t TX = 21;
static const uint8_t RX = 20;
static const uint8_t SDA = 8;
static const uint8_t SCL = 9;
static const uint8_t SS = 21;
static const uint8_t MOSI = 20;
static const uint8_t MISO = 10;
static const uint8_t SCK = 7;
static const uint8_t A0 = 0;
static const uint8_t A1 = 1;
static const uint8_t A2 = 2;
static const uint8_t A3 = 3;
static const uint8_t A4 = 4;
static const uint8_t A5 = 5;
#endif /* PinsME_Arduino_h */