Skip to content

Commit

Permalink
Dimmer dev...
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jun 15, 2024
1 parent 49f29a1 commit 0d3bbc4
Show file tree
Hide file tree
Showing 22 changed files with 2,032 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cpplint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
--repository=. \
--recursive \
--filter=-whitespace/line_length,-whitespace/braces,-whitespace/comments,-runtime/indentation_namespace,-whitespace/indent,-readability/braces,-whitespace/newline,-readability/todo,-build/c++11,-runtime/references \
--exclude=lib/DimmableLight \
--exclude=lib/ElegantOTAPro \
--exclude=lib/ESPDASHPro \
--exclude=lib/WebSerialPro \
Expand Down
2 changes: 1 addition & 1 deletion include/YaSolRWebsite.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ namespace YaSolR {
Statistic _gridEnergy = Statistic(&dashboard, "Grid: Energy");
Statistic _gridEnergyReturned = Statistic(&dashboard, "Grid: Energy Returned");
Statistic _gridFrequency = Statistic(&dashboard, "Grid: Frequency");
Statistic _gridVoltage = Statistic(&dashboard, "Grid: Voltage");

Statistic _networkHostname = Statistic(&dashboard, "Network: Hostname");
Statistic _networkInterface = Statistic(&dashboard, "Network: Interface");
Expand Down Expand Up @@ -77,6 +76,7 @@ namespace YaSolR {
Card _routerPowerFactor = Card(&dashboard, ENERGY_CARD, "Router Power Factor");
Card _routerTHDi = Card(&dashboard, ENERGY_CARD, "Router THDi", "%");
Card _gridPower = Card(&dashboard, ENERGY_CARD, "Grid Power", "W");
Card _gridVoltage = Card(&dashboard, ENERGY_CARD, "Grid Voltage", "V");
Card _routerDS18State = Card(&dashboard, TEMPERATURE_CARD, "System Temperature", "°C");

#ifdef APP_MODEL_PRO
Expand Down
504 changes: 504 additions & 0 deletions lib/DimmableLight/LICENSE

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions lib/DimmableLight/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"name": "Dimmable Light for Arduino",
"version": "1.6.0",
"authors": {
"name": "Fabiano Riccardi",
"email": "[email protected]"
},
"description": "This library allows to easily control dimmers (also known as thyristors).",
"keywords": [
"dimmer",
"triac",
"thyristor",
"timer",
"light",
"arduino-library",
"phase-fired control"
],
"repository": {
"type": "git",
"url": "https://github.com/fabianoriccardi/dimmable-light"
},
"licence": "LGPL-2.1-or-later",
"frameworks": [
"arduino"
],
"platforms": [
"espressif8266",
"espressif32",
"atmelavr",
"atmelsam",
"raspberrypi"
],
"dependencies": [
{
"owner": "mike-matera",
"name": "ArduinoSTL",
"version": "^1.3.3",
"platforms": [
"atmelavr"
]
}
],
"examples": [
{
"name": "1_dimmable_light",
"base": "examples/1_dimmable_light",
"files": [
"1_dimmable_light.ino"
]
},
{
"name": "2_dimmable_lights",
"base": "examples/2_dimmable_lights",
"files": [
"2_dimmable_lights.ino"
]
},
{
"name": "3_dimmable_light_5_light",
"base": "examples/3_dimmable_light_5_light",
"files": [
"3_dimmable_light_5_light.ino"
]
},
{
"name": "4_lights_manager",
"base": "examples/4_lights_manager",
"files": [
"4_lights_manager.ino"
]
},
{
"name": "5_dimmable_manager_n_lights",
"base": "examples/5_dimmable_manager_n_lights",
"files": [
"5_dimmable_manager_n_lights.ino"
]
},
{
"name": "6_8_lights_effects",
"base": "examples/6_8_lights_effects",
"files": [
"6_8_lights_effects.ino"
]
},
{
"name": "7_linearized_dimmable_light",
"base": "examples/7_linearized_dimmable_light",
"files": [
"7_linearized_dimmable_light.ino"
]
},
{
"name": "8_set_frequency_automatically",
"base": "examples/8_set_frequency_automatically",
"files": [
"8_set_frequency_automatically.ino"
]
}
]
}
10 changes: 10 additions & 0 deletions lib/DimmableLight/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=Dimmable Light for Arduino
version=1.6.0
author=Fabiano Riccardi <[email protected]>
maintainer=Fabiano Riccardi <[email protected]>
sentence=This library allows to easily control dimmers (also known as thyristors).
paragraph=This library was born to control dimmable light bulbs, but actually dimmers are fully compatible with other AC loads like electrical heaters and motors (be aware of what you are doing!). Actually it works on ESP8266, ESP32, AVR and SAMD.
category=Device Control
url=https://github.com/fabianoriccardi/dimmable-light
architectures=esp8266,esp32,avr,samd,rp2040
depends=ArduinoSTL
82 changes: 82 additions & 0 deletions lib/DimmableLight/src/circular_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/******************************************************************************
* This file is part of Dimmable Light for Arduino, a library to control *
* dimmers. *
* *
* Copyright (C) 2018-2023 Fabiano Riccardi *
* *
* Dimmable Light for Arduino is free software; you can redistribute *
* it and/or modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, see <http://www.gnu.org/licenses/>. *
******************************************************************************/
#ifndef CIRCULAR_QUEUE_H
#define CIRCULAR_QUEUE_H

/*
* A minimal static circular queue.
* It supports only insertion, and older values are automatically overwritten.
*/
template<typename T, int N> class CircularQueue {
public:
/**
* Construct a new Circular Queue object filling it with zeros or
* using the default constructor of type T.
*/
CircularQueue() : arr{ 0 } {};

/**
* Insert an element. If full, it returns the overwritten element.
*/
T insert(T value) {
T ret = arr[index];
arr[index] = value;
if (value > max) { max = value; }
total += value;
total -= ret;
index++;
if (index == N) { index = 0; }
if (n < N) { n++; }

return ret;
};

/**
* Empty the queue.
*/
void reset() {
for (int i = 0; i < N; i++) { arr[i] = 0; }
n = 0;
index = 0;
total = 0;
}

/**
* Return the number of stored elements.
*/
int getCount() const { return n; };
T getTotal() const { return total; }
T getMax() const { return max; }
T getLast() const { return index == 0 ? arr[N - 1] : arr[index - 1]; }

private:
T arr[N];

T total = 0;
T max = 0;

// The position for the next element to be inserted
int index = 0;

// Number of elements currently stored
int n = 0;
};

#endif // END CIRCULAR_QUEUE_H
75 changes: 75 additions & 0 deletions lib/DimmableLight/src/hw_timer_esp32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/******************************************************************************
* This file is part of Dimmable Light for Arduino, a library to control *
* dimmers. *
* *
* Copyright (C) 2018-2023 Fabiano Riccardi *
* *
* Dimmable Light for Arduino is free software; you can redistribute *
* it and/or modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, see <http://www.gnu.org/licenses/>. *
******************************************************************************/
#ifdef ESP32

#include "hw_timer_esp32.h"

const static int TIMER_ID = 0;

static hw_timer_t* timer = nullptr;

void timerInit(void (*callback)()) {
#if ESP_IDF_VERSION_MAJOR >= 5
timer = timerBegin(1000000);
timerWrite(timer, 0);
timerAttachInterrupt(timer, callback);
#else
// Use 1st timer of 4 (counted from zero).
// Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more
// info), count up. The counter starts to increase its value.
timer = timerBegin(TIMER_ID, 80, true);
timerStop(timer);
timerWrite(timer, 0);

timerAttachInterrupt(timer, callback, false);
#endif
}

void ARDUINO_ISR_ATTR startTimerAndTrigger(uint32_t delay) {
#if ESP_IDF_VERSION_MAJOR >= 5
timerRestart(timer);
timerAlarm(timer, delay, false, 0);
#else
timerWrite(timer, 0);
timerAlarmWrite(timer, delay, false);
timerAlarmEnable(timer);
timerStart(timer);
#endif
}

void ARDUINO_ISR_ATTR setAlarm(uint32_t delay) {
#if ESP_IDF_VERSION_MAJOR >= 5
timerAlarm(timer, delay, false, 0);
#else
timerAlarmWrite(timer, delay, false);

// On core v2.0.0-2.0.1, the timer alarm is automatically disabled after triggering,
// so re-enable the alarm
timerAlarmEnable(timer);
#endif
}

void ARDUINO_ISR_ATTR stopTimer() {
#if ESP_IDF_VERSION_MAJOR < 5
timerStop(timer);
#endif
}

#endif // END ESP32
38 changes: 38 additions & 0 deletions lib/DimmableLight/src/hw_timer_esp32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/******************************************************************************
* This file is part of Dimmable Light for Arduino, a library to control *
* dimmers. *
* *
* Copyright (C) 2018-2023 Fabiano Riccardi *
* *
* Dimmable Light for Arduino is free software; you can redistribute *
* it and/or modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, see <http://www.gnu.org/licenses/>. *
******************************************************************************/
#ifndef HW_TIMER_ESP32_H
#define HW_TIMER_ESP32_H

#include <Arduino.h>

// This workaround is necessary to support compilation on ESP32-Arduino v1.0.x
#ifndef ARDUINO_ISR_ATTR
#define ARDUINO_ISR_ATTR
#endif

void timerInit(void (*callback)());

void startTimerAndTrigger(uint32_t delay);

void setAlarm(uint32_t delay);

void stopTimer();

#endif // END HW_TIMER_ESP32_H
Loading

0 comments on commit 0d3bbc4

Please sign in to comment.