first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
{
"C_Cpp.dimInactiveRegions": false
}

View File

@@ -0,0 +1,187 @@
<table style="border-collapse: separate;
border-spacing: 0;
padding: 5px;">
<tbody>
<tr style="background-color: lightblue">
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;"><b>Board</b></td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2"><b>INPUT Pin <br>Zero Cross</b></td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2"><b> OUTPUT Pin</b> </td>
</tr>
<tr>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">Leonardo</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">D7 (NOT CHANGABLE)</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">D0-D6, D8-D13</td>
</tr>
<tr>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">Mega</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">D2 (NOT CHANGABLE)</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">D0-D1, D3-D70</td>
</tr>
<tr>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" >UNO<br>NANO</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">D2 (NOT CHANGABLE)</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">D0-D1, D3-D20</td>
</tr>
<tr>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">ESP8266</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">
<ul style="list-style-type: none; margin-left: -30px;">
<li >D1(IO5)</li>
<li >D5(IO14)</li>
<li >D7(IO13)</li>
</ul>
</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">
<ul style="list-style-type: none; margin-left: -30px;">
<li >D2(IO4)</li>
<li >D6(IO12)</li>
<li >D8(IO15)</li>
</ul>
</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">
<ul style="list-style-type: none; margin-left: -30px;">
<li >D0(IO16)</li>
<li >D2(IO4)</li>
<li >D6(IO12)</li>
<li >D8(IO15)</li>
</ul>
</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">
<ul style="list-style-type: none; margin-left: -30px;">
<li >D1(IO5)</li>
<li >D5(IO14)</li>
<li >D7(IO13)</li>
</ul>
</td>
</tr>
<tr>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">ESP32</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">
<ul style="list-style-type: none; margin-left: -30px;">
<li >4(GPI36)</li>
<li >5(GPI39)</li>
<li >8(GPO32)</li>
<li >10(GPI025)</li>
<li >12(GPIO27)</li>
<li >14(GPIO12)</li>
<li >21(GPIO7)</li>
<li >24(GPIO2)</li>
<li >26(GPIO4)</li>
<li >28(GPIO17)</li>
<li >30(GPIO18)</li>
<li >33(GPIO21)</li>
<li >36(GPIO22)</li>
</ul>
</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">
<ul style="list-style-type: none; margin-left: -30px;">
<li >6(GPI34)</li>
<li >7(GPI35)</li>
<li >9(GP033)</li>
<li >11(GPIO26)</li>
<li >13(GPIO14)</li>
<li >16(GPIO13)</li>
<li >23(GPIO15)</li>
<li >25(GPIO0)</li>
<li >27(GPIO16)</li>
<li >29(GPIO5)</li>
<li >31(GPIO19)</li>
<li >35(GPIO1)</li>
<li >37(GPIO23)</li>
</ul>
</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">
<ul style="list-style-type: none; margin-left: -30px;">
<li >8(GPO32)</li>
<li >10(GPIO25)</li>
<li >12(GPIO27)</li>
<li >14(GPIO12</li>
<li >23(GPIO15)</li>
<li >25(GPIO0)</li>
<li >27(GPIO16)</li>
<li >29(GPIO5)</li>
<li >31(GPIO19)</li>
<li >34(GPIO3)</li>
<li >36(GPIO22)</li>
</ul>
</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">
<ul style="list-style-type: none; margin-left: -30px;">
<li >9(GP033)</li>
<li >11(GPIO26)</li>
<li >13(GPIO14)</li>
<li >16(GPIO13)</li>
<li >24(GPIO2)</li>
<li >26(GPIO4)</li>
<li >28(GPIO17)</li>
<li >30(GPIO18)</li>
<li >33(GPIO21)</li>
<li >35(GPIO1)</li>
<li >37(GPIO23)</li>
</ul>
</td>
</tr>
<tr>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;">Arduino M0 <br>Arduino Zero</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">D7 (NOT CHANGABLE)</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">D0-D6, D8-D13</td>
</tr>
<tr>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;"> Arduino Due</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2"> D0-D53</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">D0-D53 </td>
</tr>
<tr>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;"> STM32 <br> Black Pill
<br> Blue Pill <br> Etc...</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2"> PA0-PA15, PB0-PB15
<br> PC13-PC15</td>
<td style="border: 1px solid #bbb; border-bottom: 1px solid #bbb; padding: 10px 15px 10px 15px;" colspan="2">PA0-PA15, PB0-PB15
<br> PC13-PC15</td>
</tr>
</tbody>
</table>
This library can simplify user code with following functions:
1. Function dimmerLamp - this function initializes the number of operating pin and is defined by the user
Example:
a.dimmerLamp dimmer(4); dimmer output DIM/PSM is initialized on the pin 4 for the bords WITHOUT changable ZERO-CROSS input pin (AVR, Arduino M0/Zero)
b.dimmerLamp dimmer(4, 2); dimmer output DIM/PSM is initialized on the pin 4 and zero-cross initialized on pin 2. Only for boards whith changable zero-cross(ESP32, ESP8266, Arduino Due)
2. Function begin port initialization, timer and external interrupt from zero-cross.
Example:
dimmer.begin(NORMAL_MODE, ON/OFF); port initialization, work mode choice, ON/OFF.
Parameter 1: dimmer working modes consist of two choices - NORMAL_MODE and TOGGLE_MODE
a. NORMAL_MODE to make dimmer work in defined value from 0 to 100 (%) (integer)
Example of this mode located in \RBDdimmer\examples\SimpleDimmer
b. TOGGLE_MODE smooth change of dimming value up or down in a defined range. This solutions implies change of dimming values by means of hardware timer, without using the cycle code.
Example of this mode located in \RBDdimmer\examples\SimpleToggleDimmer
Parameter 2: ON/OFF.
a. ON - turns timer ON, allows to use dimmer.
b. OFF - turns timer parameters OFF, prevents the use of dimmer.
3. Function setPower sets dimming value from 0 to 100%
Example: dimmer.setPower(90);
4. Function getPower to display current dimming value
Example: Serial.print(dimmer.getPower()); Result 0~100 int
5. Function setMode sets and changes the work mode (NORMAL_MODE and TOGGLE_MODE)
dimmer.setMode(NORMAL_MODE/TOGGLE_MODE)
6. Function getMode displays values of current work mode
Example: Serial.print(dimmer.getPower()); Result 0 (NORMAL_MODE) or 1 (TOGGLE_MODE)
7. Function setState sets dimming state ON/OFF
Example: dimmer.setState(ON); delay(100); dimmer.setState(OFF);
8. Function getState displays current state of dimmer
Serial.print(dimmer.getState()); Result 0 (OFF) or 1 (ON)
9. Function changeState changes dimmer state to the opposite one
Пример dimmer.setState(ON); delay(100); dimmer.changeState; delay(100);
10. Function toggleSettings smooth change of dimming value up or down in a defined range
Example located in \RBDdimmer\examples\SimpleToggleDimmer

View File

@@ -0,0 +1,88 @@
The following library is used for work with dimmer, it gives ability to control large ammoun of dimmer by the means of hardware timer and external interrupt.
When used this libarary better discover pin table
* ---------------------- OUTPUT & INPUT Pin table ---------------------
* +---------------+-------------------------+-------------------------+
* | Board | INPUT Pin | OUTPUT Pin |
* | | Zero-Cross | |
* +---------------+-------------------------+-------------------------+
* | Leonardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* +---------------+-------------------------+-------------------------+
* | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
* +---------------+-------------------------+-------------------------+
* | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
* +---------------+-------------------------+-------------------------+
* | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
* | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
* | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
* | | | D8(IO15) |
* +---------------+-------------------------+-------------------------+
* | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
* | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
* | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
* | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
* | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
* | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
* | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
* | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
* | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
* | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
* | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
* | | 33(GPIO21), 35(GPIO1), | |
* | | 36(GPIO22), 37(GPIO23), | |
* +---------------+-------------------------+-------------------------+
* | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* | Arduino Zero | | |
* +---------------+-------------------------+-------------------------+
* | Arduino Due | D0-D53 | D0-D53 |
* +---------------+-------------------------+-------------------------+
* | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
* | Black Pill | PC13-PC15 | PC13-PC15 |
* | BluePill | | |
* | Etc... | | |
* +---------------+-------------------------+-------------------------+
This library can simplify user code with following functions:
1. Function dimmerLamp - this function initializes the number of operating pin and is defined by the user
Example:
a.dimmerLamp dimmer(4); dimmer output DIM/PSM is initialized on the pin 4 for the bords WITHOUT changable ZERO-CROSS input pin (AVR, Arduino M0/Zero)
b.dimmerLamp dimmer(4, 2); dimmer output DIM/PSM is initialized on the pin 4 and zero-cross initialized on pin 2. Only for boards whith changable zero-cross(ESP32, ESP8266, Arduino Due)
2. Function begin port initialization, timer and external interrupt from zero-cross.
Example:
dimmer.begin(NORMAL_MODE, ON/OFF); port initialization, work mode choice, ON/OFF.
Parameter 1: dimmer working modes consist of two choices - NORMAL_MODE and TOGGLE_MODE
a. NORMAL_MODE to make dimmer work in defined value from 0 to 100 (%) (integer)
Example of this mode located in \RBDdimmer\examples\SimpleDimmer
b. TOGGLE_MODE smooth change of dimming value up or down in a defined range. This solutions implies change of dimming values by means of hardware timer, without using the cycle code.
Example of this mode located in \RBDdimmer\examples\SimpleToggleDimmer
Parameter 2: ON/OFF.
a. ON - turns timer ON, allows to use dimmer.
b. OFF - turns timer parameters OFF, prevents the use of dimmer.
3. Function setPower sets dimming value from 0 to 100%
Example: dimmer.setPower(90);
4. Function getPower to display current dimming value
Example: Serial.print(dimmer.getPower()); Result 0~100 int
5. Function setMode sets and changes the work mode (NORMAL_MODE and TOGGLE_MODE)
dimmer.setMode(NORMAL_MODE/TOGGLE_MODE)
6. Function getMode displays values of current work mode
Example: Serial.print(dimmer.getPower()); Result 0 (NORMAL_MODE) or 1 (TOGGLE_MODE)
7. Function setState sets dimming state ON/OFF
Example: dimmer.setState(ON); delay(100); dimmer.setState(OFF);
8. Function getState displays current state of dimmer
Serial.print(dimmer.getState()); Result 0 (OFF) or 1 (ON)
9. Function changeState changes dimmer state to the opposite one
Пример dimmer.setState(ON); delay(100); dimmer.changeState; delay(100);
10. Function toggleSettings smooth change of dimming value up or down in a defined range
Example located in \RBDdimmer\examples\SimpleToggleDimmer

View File

@@ -0,0 +1,88 @@
Данная библиотека предназначена для работы с диммером, дает возможность управления большим количеством диммеров используя аппаратный таймер и внешние прерывания.
При использовании библиотеки стоит заранее ознакомится с таблицей задаваемых пинов.
* ---------------------- OUTPUT & INPUT Pin table ---------------------
* +---------------+-------------------------+-------------------------+
* | Board | INPUT Pin | OUTPUT Pin |
* | | Zero-Cross | |
* +---------------+-------------------------+-------------------------+
* | Leonardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* +---------------+-------------------------+-------------------------+
* | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
* +---------------+-------------------------+-------------------------+
* | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
* +---------------+-------------------------+-------------------------+
* | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
* | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
* | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
* | | | D8(IO15) |
* +---------------+-------------------------+-------------------------+
* | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
* | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
* | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
* | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
* | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
* | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
* | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
* | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
* | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
* | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
* | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
* | | 33(GPIO21), 35(GPIO1), | |
* | | 36(GPIO22), 37(GPIO23), | |
* +---------------+-------------------------+-------------------------+
* | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* | Arduino Zero | | |
* +---------------+-------------------------+-------------------------+
* | Arduino Due | D0-D53 | D0-D53 |
* +---------------+-------------------------+-------------------------+
* | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
* | Black Pill | PC13-PC15 | PC13-PC15 |
* | BluePill | | |
* | Etc... | | |
* +---------------+-------------------------+-------------------------+
Использование данной библиотеки упрощает код пользователя при помощи функций:
1. функция dimmerLamp. Данная функция инициализирует значение пина которое будет использоватся, задается пользователем
а.Пример dimmerLamp dimmer(4); диммер выход DIM/PSM инциализируется на пин 4 для плат, в которых пин Zero-Cros не переназначается (NOT CHANGABLE для AVR, SAMD)
б.Пример dimmerLamp dimmer(4, 2); диммер выход DIM/PSM инциализируется на пин 4 для плат, на которых Zero-Cross можно переназначать (ESP32, ESP8266, Arduino Due)
2. функция begin - инициализация портов, таймера и внешнего прерывания от zero-cross.
Пример dimmer.begin(NORMAL_MODE, ON); инициализация портов, выбор режима работы, вкл/выкл.
Параметр 1: Выбор режим работы диммера состоит из двух вариантов NORMAL_MODE и TOGGLE_MODE
а. NORMAL_MODE работа диммера в заданном значении от 0 до 100 (%) (целочисленное)
Пример работы режима находится в папке \RBDdimmer\examples\SimpleDimmer
б. TOGGLE_MODE плавное изменение значения димминга увеличение и снижение в заданных границах. Данное решение построенно на изменении значения диминга за счет
аппаратного таймера и не использует код цикла.
Пример работы режима находится в папке \RBDdimmer\examples\SimpleToggleDimmer
Параметр 2: ON/OFF.
a. ON - включение таймера, диммер использовать можно.
a. OFF - выключение параметров для таймера, диммер использовать нельзя.
3. Функция setPower установка значения димминга от 0 до 100%
Пример dimmer.setPower(90);
4. Функция getPower используется для того что бы получить текущее значение димминга
Пример Serial.print(dimmer.getPower()); Result 0~100 int
5. Функция setMode установка или изменение мода режимами работы (NORMAL_MODE и TOGGLE_MODE)
dimmer.setMode(NORMAL_MODE/TOGGLE_MODE)
6. Функция getMode полученние нынешнего значения режима работы
Пример Serial.print(dimmer.getPower()); Result 0 (NORMAL_MODE) or 1 (TOGGLE_MODE)
7. Функция setState задет состояние димминга ON/OFF
Пример dimmer.setState(ON); delay(100); dimmer.setState(OFF);
8. Функция getState полученние нынешнего значения состояния диммера
Serial.print(dimmer.getState()); Result 0 (OFF) or 1 (ON)
9. Функция changeState изменяет состояние диммера на противоположное
Пример dimmer.setState(ON); delay(100); dimmer.changeState; delay(100);
10. Функция toggleSettings плавное изменение значения димминга увеличение и снижение в заданных границах
Пример находится в папке \RBDdimmer\examples\SimpleToggleDimmer

View File

@@ -0,0 +1,100 @@
/**************
* RobotDyn
* Dimmer Library
* **************
*
* The following sketch is meant to define the dimming value through the serial port of the controller,
* using USE_SERIAL.begin
* void printSpace() function is used for adding of space after functional data
* void loop() serial port evaluator, used to register and define values in dimmer.setPower(outVal);
*
*
* ---------------------- OUTPUT & INPUT Pin table ---------------------
* +---------------+-------------------------+-------------------------+
* | Board | INPUT Pin | OUTPUT Pin |
* | | Zero-Cross | |
* +---------------+-------------------------+-------------------------+
* | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* +---------------+-------------------------+-------------------------+
* | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
* +---------------+-------------------------+-------------------------+
* | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
* +---------------+-------------------------+-------------------------+
* | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
* | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
* | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
* | | | D8(IO15) |
* +---------------+-------------------------+-------------------------+
* | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
* | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
* | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
* | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
* | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
* | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
* | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
* | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
* | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
* | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
* | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
* | | 33(GPIO21), 35(GPIO1), | |
* | | 36(GPIO22), 37(GPIO23), | |
* +---------------+-------------------------+-------------------------+
* | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* | Arduino Zero | | |
* +---------------+-------------------------+-------------------------+
* | Arduino Due | D0-D53 | D0-D53 |
* +---------------+-------------------------+-------------------------+
* | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
* | Black Pill | PC13-PC15 | PC13-PC15 |
* | BluePill | | |
* | Etc... | | |
* +---------------+-------------------------+-------------------------+
*/
#include <RBDdimmer.h>//
//#define USE_SERIAL SerialUSB //Serial for boards whith USB serial port
#define USE_SERIAL Serial
#define outputPin 12
#define zerocross 5 // for boards with CHANGEBLE input pins
//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
int outVal = 0;
void setup() {
USE_SERIAL.begin(9600);
dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
USE_SERIAL.println("Dimmer Program is starting...");
USE_SERIAL.println("Set value");
}
void printSpace(int val)
{
if ((val / 100) == 0) USE_SERIAL.print(" ");
if ((val / 10) == 0) USE_SERIAL.print(" ");
}
void loop() {
int preVal = outVal;
if (USE_SERIAL.available())
{
int buf = USE_SERIAL.parseInt();
if (buf != 0) outVal = buf;
delay(200);
}
dimmer.setPower(outVal); // setPower(0-100%);
if (preVal != outVal)
{
USE_SERIAL.print("lampValue -> ");
printSpace(dimmer.getPower());
USE_SERIAL.print(dimmer.getPower());
USE_SERIAL.println("%");
}
delay(50);
}

View File

@@ -0,0 +1,83 @@
/**************
* RobotDyn
* Dimmer Library
* **************
*
* The following sketch is meant to turn the lamp on/off with use of a button.
* pinMode(14, INPUT); button is connected to 14th pin
* void loop() ON/OFF button evaluator of dimmer in dim4.setState(ON/OFF);
*
*
* ---------------------- OUTPUT & INPUT Pin table ---------------------
* +---------------+-------------------------+-------------------------+
* | Board | INPUT Pin | OUTPUT Pin |
* | | Zero-Cross | |
* +---------------+-------------------------+-------------------------+
* | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* +---------------+-------------------------+-------------------------+
* | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
* +---------------+-------------------------+-------------------------+
* | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
* +---------------+-------------------------+-------------------------+
* | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
* | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
* | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
* | | | D8(IO15) |
* +---------------+-------------------------+-------------------------+
* | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
* | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
* | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
* | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
* | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
* | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
* | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
* | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
* | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
* | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
* | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
* | | 33(GPIO21), 35(GPIO1), | |
* | | 36(GPIO22), 37(GPIO23), | |
* +---------------+-------------------------+-------------------------+
* | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* | Arduino Zero | | |
* +---------------+-------------------------+-------------------------+
* | Arduino Due | D0-D53 | D0-D53 |
* +---------------+-------------------------+-------------------------+
* | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
* | Black Pill | PC13-PC15 | PC13-PC15 |
* | BluePill | | |
* | Etc... | | |
* +---------------+-------------------------+-------------------------+
*/
#include <RBDdimmer.h>//
//#define USE_SERIAL SerialUSB //Serial for boards whith USB serial port
#define USE_SERIAL Serial
#define outputPin 12
#define zerocross 5 // for boards with CHANGEBLE input pins
//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
int buttonRed = 0;
void setup() {
dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
dimmer.setPower(50);
pinMode(13, INPUT);
}
void loop() {
buttonRed = digitalRead(13);
if (buttonRed == 1)
{
delay(10);
dimmer.setState(ON); //name.setState(ON/OFF);
}
if (buttonRed == 0)
{
delay(10);
dimmer.setState(OFF); //name.setState(ON/OFF);
}
}

View File

@@ -0,0 +1,76 @@
/**************
* RobotDyn
* Dimmer Library
* **************
*
* The following sketch is meant to to define dimming value through potentiometer,
* The potentiometer values are changing in range from 0 to 1023
* potentiometer values are converted through the map function to values from 0 to 100% and saved in dimmer.setPower(outVal);
* Serial.begin is used to display dimming values
*
*
* ---------------------- OUTPUT & INPUT Pin table ---------------------
* +---------------+-------------------------+-------------------------+
* | Board | INPUT Pin | OUTPUT Pin |
* | | Zero-Cross | |
* +---------------+-------------------------+-------------------------+
* | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* +---------------+-------------------------+-------------------------+
* | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
* +---------------+-------------------------+-------------------------+
* | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
* +---------------+-------------------------+-------------------------+
* | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
* | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
* | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
* | | | D8(IO15) |
* +---------------+-------------------------+-------------------------+
* | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
* | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
* | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
* | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
* | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
* | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
* | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
* | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
* | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
* | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
* | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
* | | 33(GPIO21), 35(GPIO1), | |
* | | 36(GPIO22), 37(GPIO23), | |
* +---------------+-------------------------+-------------------------+
* | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* | Arduino Zero | | |
* +---------------+-------------------------+-------------------------+
* | Arduino Due | D0-D53 | D0-D53 |
* +---------------+-------------------------+-------------------------+
* | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
* | Black Pill | PC13-PC15 | PC13-PC15 |
* | BluePill | | |
* | Etc... | | |
* +---------------+-------------------------+-------------------------+
*/
#include <RBDdimmer.h>//
//#define USE_SERIAL SerialUSB //Serial for boards whith USB serial port
#define USE_SERIAL Serial
#define outputPin 12
#define zerocross 5 // for boards with CHANGEBLE input pins
//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
int outVal = 0;
void setup() {
USE_SERIAL.begin(9600);
dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
}
void loop()
{
outVal = map(analogRead(0), 1, 1024, 100, 0); // analogRead(analog_pin), min_analog, max_analog, 100%, 0%);
USE_SERIAL.println(outVal);
dimmer.setPower(outVal); // name.setPower(0%-100%)
}

View File

@@ -0,0 +1,72 @@
/**************
* RobotDyn
* Dimmer Library
* **************
* The following sketch is meant to define by function the smooth changes of dimming values in a range of values defined by user
* values are defined in range from 0 to 100%
*
* ---------------------- OUTPUT & INPUT Pin table ---------------------
* +---------------+-------------------------+-------------------------+
* | Board | INPUT Pin | OUTPUT Pin |
* | | Zero-Cross | |
* +---------------+-------------------------+-------------------------+
* | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* +---------------+-------------------------+-------------------------+
* | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
* +---------------+-------------------------+-------------------------+
* | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
* +---------------+-------------------------+-------------------------+
* | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
* | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
* | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
* | | | D8(IO15) |
* +---------------+-------------------------+-------------------------+
* | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
* | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
* | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
* | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
* | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
* | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
* | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
* | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
* | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
* | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
* | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
* | | 33(GPIO21), 35(GPIO1), | |
* | | 36(GPIO22), 37(GPIO23), | |
* +---------------+-------------------------+-------------------------+
* | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* | Arduino Zero | | |
* +---------------+-------------------------+-------------------------+
* | Arduino Due | D0-D53 | D0-D53 |
* +---------------+-------------------------+-------------------------+
* | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
* | Black Pill | PC13-PC15 | PC13-PC15 |
* | BluePill | | |
* | Etc... | | |
* +---------------+-------------------------+-------------------------+
*/
#include <RBDdimmer.h>//
//#define USE_SERIAL SerialUSB //Serial for boards whith USB serial port
#define USE_SERIAL Serial
#define outputPin 12
#define zerocross 5 // for boards with CHANGEBLE input pins
//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
void setup() {
USE_SERIAL.begin(9600);
dimmer.begin(TOGGLE_MODE, OFF); //dimmer initialisation: name.begin(MODE, STATE)
USE_SERIAL.println("--- Toggle dimmer example ---");
dimmer.toggleSettings(0, 100); //Name.toggleSettings(MIN, MAX);
dimmer.setState(ON); // state: dimmer1.setState(ON/OFF);
pinMode(14, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
}

View File

@@ -0,0 +1,105 @@
/**************
* RobotDyn
* Dimmer Library
* **************
* The following sketch is meant to smoothly turn the dimmer ON after pressing the first button and turn it OFF after pressing a second one
*
* ---------------------- OUTPUT & INPUT Pin table ---------------------
* +---------------+-------------------------+-------------------------+
* | Board | INPUT Pin | OUTPUT Pin |
* | | Zero-Cross | |
* +---------------+-------------------------+-------------------------+
* | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* +---------------+-------------------------+-------------------------+
* | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
* +---------------+-------------------------+-------------------------+
* | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
* +---------------+-------------------------+-------------------------+
* | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
* | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
* | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
* | | | D8(IO15) |
* +---------------+-------------------------+-------------------------+
* | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
* | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
* | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
* | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
* | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
* | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
* | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
* | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
* | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
* | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
* | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
* | | 33(GPIO21), 35(GPIO1), | |
* | | 36(GPIO22), 37(GPIO23), | |
* +---------------+-------------------------+-------------------------+
* | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* | Arduino Zero | | |
* +---------------+-------------------------+-------------------------+
* | Arduino Due | D0-D53 | D0-D53 |
* +---------------+-------------------------+-------------------------+
* | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
* | Black Pill | PC13-PC15 | PC13-PC15 |
* | BluePill | | |
* | Etc... | | |
* +---------------+-------------------------+-------------------------+
*/
#include <RBDdimmer.h>
//#define USE_SERIAL SerialUSB //Serial for boards whith USB serial port
#define USE_SERIAL Serial
#define outputPin 12
#define zerocross 5 // for boards with CHANGEBLE input pins
#define LAMPMAXVALUE 100
//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
int stateL = 0, valLamp;
int DIM4 = 0;
int mainLamp = 0;
int buttonRed = 0;
int buttonBlue = 0;
bool setLamp = true;
void setup() {
USE_SERIAL.begin(9600);
dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
}
void RiseFallLamp(bool RiseFallInt)
{
if ((RiseFallInt == true) && (mainLamp < LAMPMAXVALUE)) mainLamp++;
else if ((RiseFallInt != true) && (mainLamp > 0)) mainLamp--;
}
bool setLampState(int val)
{
bool ret;
if (val >= 1) ret = true;
else ret = false;
return ret;
}
void readButtonState()
{
buttonRed = digitalRead(13);
buttonBlue = digitalRead(15);
if (buttonRed < 1) stateL++;
if (buttonBlue < 1) stateL--;
if (stateL < 0) stateL = 0;
if (stateL > 1) stateL = 1;
}
void loop() {
readButtonState();
dimmer.setPower(mainLamp); // setPower(0-100%);
RiseFallLamp(setLampState(stateL));
delay(25);
}

View File

@@ -0,0 +1,64 @@
{
"packages":
[
{
"name": "RBDdimmer",
"maintainer": "RobotDyn",
"websiteURL": "https://robotdyn.com",
"help":
{
"online": "https://robotdyn.com"
},
"platforms":
[
{
"name": "Atmel SAMD21 Xplained-pros",
"architecture": "samd",
"version": "3.0",
"category": "Contributed",
"url": "https://github.com/AtmelUniversityFrance/atmel-samd21-xpro-boardmanagermodule/releases/download/v0.3.0/atmel-samd21-xpro-boardmanagermodule-0.3.0.tar.bz2",
"archiveFileName": "atmel-samd21-xpro-boardmanagermodule-0.3.0.tar.bz2",
"checksum": "SHA-256:70b351f50e4408bf4384519ffcc3547fbf27612fc551dbd66a7d2d48ea8fafdc",
"size": "55213",
"boards": [
{
"name": "atmel_samd21_xpro_v1"
},
{
"name": "atmel_samr21_xpro"
},
{
"name": "atmel_samw25_xpro"
}
],
"toolsDependencies":
[
{
"packager": "arduino",
"name": "arm-none-eabi-gcc",
"version": "4.8.3-2014q1"
},
{
"packager": "arduino",
"name": "bossac",
"version": "1.6.1-arduino"
},
{
"packager": "arduino",
"name": "openocd",
"version": "0.9.0-arduino"
},
{
"packager": "arduino",
"name": "CMSIS",
"version": "4.0.0-atmel"
}
]
}
],
"tools":
[
]
}
]
}

View File

@@ -0,0 +1,30 @@
#######################################
# Syntax Colouring Map For RBDdimmerESP32
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
dimmerLamp KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
setPower KEYWORD2
getPower KEYWORD2
setMode KEYWORD2
getMode KEYWORD2
setState KEYWORD2
getState KEYWORD2
changeState KEYWORD2
toggleSettings KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
ON LITERAL1
OFF LITERAL1
NORMAL_MODE LITERAL1
TOGGLE_MODE LITERAL1

View File

@@ -0,0 +1,14 @@
{
"name": "RBDdimmer",
"keywords": "RBDdimmer, dimmer, lamp, AVR,M0, SAM3, ESP8266, ESP32, STM32",
"description": "Arduino AC-dimming library for AVR, ESP8266, ESP32 processor",
"repository": {
"type": "git",
"url": "https://github.com/Lib-RBD-Dimmer-for-ESP32/RBDdimmerESP32.git"
},
"frameworks": "arduino",
"platforms": [
"atmelavr",
"espressif"
]
}

View File

@@ -0,0 +1,9 @@
name=RBDdimmer
version=1.0
author=RobotDyn (Vladislav Snegirev & Ivan Kryukov)
maintainer=Shavkat Begishev <robotdyn.com>
sentence=A library to control dimmable lamps and other AC loads.
paragraph=Use triacs and a zero cross circuit to control the power of connected devices.
category=Device Control
url=*
architectures=*

View File

@@ -0,0 +1,74 @@
#ifndef RBDDIMMER_H
#define RBDDIMMER_H
#include <stdlib.h>
#if defined(ARDUINO_ARCH_AVR)
#include "avr/RBDmcuAVR.h"
#elif defined(ARDUINO_ARCH_ESP32)
#include "esp32/RBDmcuESP32.h"
#elif defined(ARDUINO_ARCH_ESP8266)
#include "esp8266/RBDmcuESP8266.h"
#elif defined(ARDUINO_ARCH_SAMD)
#include "samd/RBDmcuSAMD21.h"
#elif defined(ARDUINO_ARCH_SAM)
#include "sam/RBDmcuSAM.h"
#elif defined(ARDUINO_ARCH_STM32F1)
#include "stm32duino/STM32F1/RBDmcuSTM32F1.h"
#elif defined(ARDUINO_ARCH_STM32F4)
#include "stm32duino/STM32F4/RBDmcuSTM32F4.h"
#else
#error "This library only supports boards with an AVR, ESP32, ESP8266, SAMD, SAM, STM32F1/F4 processor."
#endif
typedef enum
{
NORMAL_MODE = 0,
TOGGLE_MODE = 1
} DIMMER_MODE_typedef;
typedef enum
{
OFF = false,
ON = true
} ON_OFF_typedef;
#define ALL_DIMMERS 30
class dimmerLamp
{
private:
int current_num;
int timer_num;
bool toggle_state;
int tog_current;
void port_init(void);
void timer_init(void);
void ext_int_init(void);
public:
uint16_t pulse_begin;
int dimmer_pin;
int tog_max;
int tog_min;
#if !(defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD))
int zc_pin;
dimmerLamp(int user_dimmer_pin, int zc_dimmer_pin);
#else
dimmerLamp(int user_dimmer_pin);
#endif
void begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF);
void setPower(int power);
int getPower(void);
void setState(ON_OFF_typedef ON_OFF);
bool getState(void);
void changeState(void);
void setMode(DIMMER_MODE_typedef DIMMER_MODE);
DIMMER_MODE_typedef getMode(void);
void toggleSettings(int minValue, int maxValue);
};
#endif

View File

@@ -0,0 +1,225 @@
#if defined(ARDUINO_ARCH_AVR)
#include "RBDmcuAVR.h"
#define _TCNT(X) TCNT ## X
#define TCNT(X) _TCNT(X)
#define _TCCRxA(X) TCCR ## X ## A
#define TCCRxA(X) _TCCRxA(X)
#define _TCCRxB(X) TCCR ## X ## B
#define TCCRxB(X) _TCCRxB(X)
#define _TIMSKx(X) TIMSK ## X
#define TIMSKx(X) _TIMSKx(X)
#define _TIFRx(X) TIFR ## X
#define TIFRx(X) _TIFRx(X)
#define _OCFxA(X) OCF ## X ## A
#define OCFxA(X) _OCFxA(X)
#define _OCFxB(X) OCF ## X ## B
#define OCFxB(X) _OCFxB(X)
#define _TOIEx(X) TOIE ## X
#define TOIEx(X) _TOIEx(X)
#define _OCIExA(X) OCIE ## X ## A
#define OCIExA(X) _OCIExA(X)
#define _TIMER_COMPA_VECTOR(X) TIMER ## X ## _COMPA_vect
#define TIMER_COMPA_VECTOR(X) _TIMER_COMPA_VECTOR(X)
int pulseWidth = 1;
volatile int current_dim = 0;
int all_dim = 3;
int rise_fall = true;
char user_zero_cross = '0';
static int toggleCounter = 0;
static int toggleReload = 25;
static dimmerLamp* dimmer[ALL_DIMMERS];
volatile uint16_t dimPower[ALL_DIMMERS];
volatile uint16_t dimOutPin[ALL_DIMMERS];
volatile uint16_t zeroCross[ALL_DIMMERS];
volatile DIMMER_MODE_typedef dimMode[ALL_DIMMERS];
volatile ON_OFF_typedef dimState[ALL_DIMMERS];
volatile uint16_t dimCounter[ALL_DIMMERS];
static uint16_t dimPulseBegin[ALL_DIMMERS];
volatile uint16_t togMax[ALL_DIMMERS];
volatile uint16_t togMin[ALL_DIMMERS];
volatile bool togDir[ALL_DIMMERS];
dimmerLamp::dimmerLamp(int user_dimmer_pin):
dimmer_pin(user_dimmer_pin)
{
current_dim++;
dimmer[current_dim-1] = this;
current_num = current_dim-1;
timer_num = DIMMER_TIMER;
toggle_state = false;
dimPulseBegin[current_dim-1] = 1;
dimOutPin[current_dim-1] = user_dimmer_pin;
dimCounter[current_dim-1] = 0;
zeroCross[current_dim-1] = 0;
dimMode[current_dim-1] = NORMAL_MODE;
togMin[current_dim-1] = 0;
togMax[current_dim-1] = 1;
pinMode(user_dimmer_pin, OUTPUT);
}
void dimmerLamp::timer_init(void)
{
TCCRxA(DIMMER_TIMER) &= ~(0xFF); // clean TCCRxA register
TCCRxB(DIMMER_TIMER) &= ~(0xFF); // clean TCCRxB register
TIMSKx(DIMMER_TIMER) |= (1 << OCIExA(DIMMER_TIMER)); //устанавливаем бит разрешения прерывания 1ого счетчика по совпадению с OCR1A(H и L)
TCCRxB(DIMMER_TIMER) = TCCRxB_VALUE;
if (DIMMER_TIMER != 2) OCRxAH(DIMMER_TIMER) = OCRxAH_VALUE;
OCRxAL(DIMMER_TIMER) = OCRxAL_VALUE;
TIMSKx(DIMMER_TIMER) |= (1 << TOIEx(DIMMER_TIMER)); //timer interrupt enable
}
void dimmerLamp::ext_int_init(void)
{
EICRX &= ~0xFF;
EIMSK |= (1 << INTx);
EICRX |= (1 << ISCx1)|(1 << ISCx0);//0b00001100
}
void dimmerLamp::begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF)
{
dimMode[this->current_num] = DIMMER_MODE;
dimState[this->current_num] = ON_OFF;
timer_init();
ext_int_init();
}
void dimmerLamp::setPower(int power)
{
if (power >= 99)
{
power = 99;
}
dimPower[this->current_num] = power;
dimPulseBegin[this->current_num] = powerBuf[power];
delay(1);
}
int dimmerLamp::getPower(void)
{
if (dimState[this->current_num] == ON)
return dimPower[this->current_num];
else return 0;
}
void dimmerLamp::setState(ON_OFF_typedef ON_OFF)
{
dimState[this->current_num] = ON_OFF;
}
bool dimmerLamp::getState(void)
{
bool ret;
if (dimState[this->current_num] == ON) ret = true;
else ret = false;
return ret;
}
void dimmerLamp::changeState(void)
{
if (dimState[this->current_num] == ON) dimState[this->current_num] = OFF;
else
dimState[this->current_num] = ON;
}
DIMMER_MODE_typedef dimmerLamp::getMode(void)
{
return dimMode[this->current_num];
}
void dimmerLamp::setMode(DIMMER_MODE_typedef DIMMER_MODE)
{
dimMode[this->current_num] = DIMMER_MODE;
}
void dimmerLamp::toggleSettings(int minValue, int maxValue)
{
if (maxValue > 99)
{
maxValue = 99;
}
if (minValue < 1)
{
minValue = 1;
}
dimMode[this->current_num] = TOGGLE_MODE;
togMin[this->current_num] = minValue;
togMax[this->current_num] = maxValue;
toggleReload = 50;
}
ISR(INT_vect)
{
for (int i = 0; i < current_dim; i++ )
if (dimState[i] == ON)
{
zeroCross[i] = 1;
}
}
static int k;
ISR (TIMER_COMPA_VECTOR(DIMMER_TIMER))
{
toggleCounter++;
for (k = 0; k < current_dim; k++)
{
if (zeroCross[k] == 1 )
{
dimCounter[k]++;
if (dimMode[k] == TOGGLE_MODE)
{
/*****
* TOGGLE DIMMING MODE
*****/
if (dimPulseBegin[k] >= togMax[k])
{
// if reach max dimming value
togDir[k] = false; // downcount
}
if (dimPulseBegin[k] <= togMin[k])
{
// if reach min dimming value
togDir[k] = true; // upcount
}
if (toggleCounter == toggleReload)
{
if (togDir[k] == true) dimPulseBegin[k]++;
else dimPulseBegin[k]--;
}
}
/*****
* DEFAULT DIMMING MODE (NOT TOGGLE)
*****/
if (dimCounter[k] >= dimPulseBegin[k] )
{
digitalWrite(dimOutPin[k], HIGH);
}
if (dimCounter[k] >= (dimPulseBegin[k] + pulseWidth) )
{
digitalWrite(dimOutPin[k], LOW);
zeroCross[k] = 0;
dimCounter[k] = 0;
}
}
}
if (toggleCounter >= toggleReload) toggleCounter = 1;
TIFRx(DIMMER_TIMER) |= ((1<<OCFxB(DIMMER_TIMER))|(1<<OCFxA(DIMMER_TIMER)));
}
#endif

View File

@@ -0,0 +1,99 @@
#ifndef RBDMCUAVR_H
#define RBDMCUAVR_H
#include "Arduino.h"
#include "RBDdimmer.h"
#include <avr/io.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <util/atomic.h>
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define DIMMER_TIMER 4
#define INT_vect INT4_vect
#define INTx INT4
#define EICRX EICRB
#define ISCx1 ISC41
#define ISCx0 ISC40
#define ALL_DIMMERS 50
#define TCCRxA_VALUE 0x00 // CTC mode
#define TCCRxB_VALUE 0x0A // 0b1011 // (1 << WGMx2)|(1 << CSx1)|(1 << CSx0)
#define OCRxAH_VALUE 0x00
#define OCRxAL_VALUE 0x0F
#define _OCRxAH(X) OCR ## X ## AH
#define OCRxAH(X) _OCRxAH(X)
#define _OCRxAL(X) OCR ## X ## AL
#define OCRxAL(X) _OCRxAL(X)
#elif defined(__AVR_ATmega32U4__)
#define DIMMER_TIMER 1
#define INT_vect INT6_vect
#define INTx INT6
#define EICRX EICRB
#define ISCx1 ISC61
#define ISCx0 ISC60
#define ALL_DIMMERS 30
#define TCCRxA_VALUE 0x00 // CTC mode
#define TCCRxB_VALUE 0x09// 0b1011 (1 << WGMx2)|(1 << CSx1)|(1 << CSx0)
#define OCRxAH_VALUE 0x00
#define OCRxAL_VALUE 0xBC
#define _OCRxAH(X) OCR ## X ## AH
#define OCRxAH(X) _OCRxAH(X)
#define _OCRxAL(X) OCR ## X ## AL
#define OCRxAL(X) _OCRxAL(X)
#else
#define DIMMER_TIMER 2
#define INT_vect INT0_vect
#define INTx INT0
#define EICRX EICRA
#define ISCx1 ISC01
#define ISCx0 ISC00
#define ALL_DIMMERS 13
#define TCCRxA_VALUE 0x02
#define TCCRxB_VALUE 0x09 // 0b1010 // (1 << WGMx2)|(1 << CSx1)
#define OCRxAH_VALUE 0x00
#define OCRxAL_VALUE 0xFF
#define _OCRxAH(X) OCR ## X ## A
#define OCRxAH(X) _OCRxAH(X)
#define _OCRxAL(X) OCR ## X ## A
#define OCRxAL(X) _OCRxAL(X)
#endif
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__)
static const uint16_t powerBuf[] = {
610, 604, 598, 592, 586, 580, 574, 568, 562, 556,
550, 544, 538, 532, 526, 520, 514, 508, 502, 496,
490, 484, 478, 472, 466, 460, 454, 448, 442, 436,
430, 424, 418, 412, 406, 400, 394, 388, 382, 376,
370, 364, 358, 352, 346, 340, 334, 328, 322, 316,
310, 304, 298, 292, 286, 280, 274, 268, 262, 256,
250, 244, 238, 232, 226, 220, 214, 208, 202, 196,
180, 174, 168, 162, 156, 150, 144, 138, 132, 126,
120, 114, 108, 102, 96, 90, 84, 78, 72, 66,
60, 54, 48, 42, 36, 30, 24, 18, 12, 6
};
#else
static const uint16_t powerBuf[] = {
600, 600, 598, 592, 586, 580, 574, 568, 562, 556,
550, 544, 538, 532, 526, 520, 514, 508, 502, 496,
490, 484, 478, 472, 466, 460, 454, 448, 442, 436,
430, 424, 418, 412, 406, 400, 394, 388, 382, 376,
370, 364, 358, 352, 346, 340, 334, 328, 322, 316,
310, 304, 298, 292, 286, 280, 274, 268, 262, 256,
250, 244, 238, 232, 226, 220, 214, 208, 202, 196,
180, 174, 168, 162, 156, 150, 144, 138, 132, 126,
120, 114, 108, 102, 96, 90, 84, 78, 72, 66,
60, 54, 48, 42, 36, 30, 24, 18, 12, 8
};
#endif
#endif

View File

@@ -0,0 +1,206 @@
#if defined(ARDUINO_ARCH_ESP32)
#include "RBDmcuESP32.h"
int pulseWidth = 1;
volatile int current_dim = 0;
int all_dim = 3;
int rise_fall = true;
char user_zero_cross = '0';
static int toggleCounter = 0;
static int toggleReload = 25;
static dimmerLamp* dimmer[ALL_DIMMERS];
volatile uint16_t dimPower[ALL_DIMMERS];
volatile uint16_t dimOutPin[ALL_DIMMERS];
volatile uint16_t dimZCPin[ALL_DIMMERS];
volatile uint16_t zeroCross[ALL_DIMMERS];
volatile DIMMER_MODE_typedef dimMode[ALL_DIMMERS];
volatile ON_OFF_typedef dimState[ALL_DIMMERS];
volatile uint16_t dimCounter[ALL_DIMMERS];
static uint16_t dimPulseBegin[ALL_DIMMERS];
volatile uint16_t togMax[ALL_DIMMERS];
volatile uint16_t togMin[ALL_DIMMERS];
volatile bool togDir[ALL_DIMMERS];
dimmerLamp::dimmerLamp(int user_dimmer_pin, int zc_dimmer_pin):
dimmer_pin(user_dimmer_pin),
zc_pin(zc_dimmer_pin)
{
current_dim++;
dimmer[current_dim-1] = this;
current_num = current_dim-1;
toggle_state = false;
dimPulseBegin[current_dim-1] = 1;
dimOutPin[current_dim-1] = user_dimmer_pin;
dimZCPin[current_dim-1] = zc_dimmer_pin;
dimCounter[current_dim-1] = 0;
zeroCross[current_dim-1] = 0;
dimMode[current_dim-1] = NORMAL_MODE;
togMin[current_dim-1] = 0;
togMax[current_dim-1] = 1;
pinMode(user_dimmer_pin, OUTPUT);
}
void dimmerLamp::timer_init(void)
{
hw_timer_t * timer = NULL;
// Use 1st timer of 4 (counted from zero).
// Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more info).
timer = timerBegin(0, 250, true);
// Attach onTimer function to our timer.
timerAttachInterrupt(timer, &onTimerISR, true);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter)
timerAlarmWrite(timer, 30, true);
// Start an alarm
timerAlarmEnable(timer);
}
void dimmerLamp::ext_int_init(void)
{
int inPin = dimZCPin[this->current_num];
pinMode(inPin, INPUT_PULLUP);
attachInterrupt(inPin, isr_ext, RISING);
}
void dimmerLamp::begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF)
{
dimMode[this->current_num] = DIMMER_MODE;
dimState[this->current_num] = ON_OFF;
timer_init();
ext_int_init();
}
void dimmerLamp::setPower(int power)
{
if (power >= 99)
{
power = 99;
}
dimPower[this->current_num] = power;
dimPulseBegin[this->current_num] = powerBuf[power];
delay(1);
}
int dimmerLamp::getPower(void)
{
if (dimState[this->current_num] == ON)
return dimPower[this->current_num];
else return 0;
}
void dimmerLamp::setState(ON_OFF_typedef ON_OFF)
{
dimState[this->current_num] = ON_OFF;
}
bool dimmerLamp::getState(void)
{
bool ret;
if (dimState[this->current_num] == ON) ret = true;
else ret = false;
return ret;
}
void dimmerLamp::changeState(void)
{
if (dimState[this->current_num] == ON) dimState[this->current_num] = OFF;
else
dimState[this->current_num] = ON;
}
DIMMER_MODE_typedef dimmerLamp::getMode(void)
{
return dimMode[this->current_num];
}
void dimmerLamp::setMode(DIMMER_MODE_typedef DIMMER_MODE)
{
dimMode[this->current_num] = DIMMER_MODE;
}
void dimmerLamp::toggleSettings(int minValue, int maxValue)
{
if (maxValue > 99)
{
maxValue = 99;
}
if (minValue < 1)
{
minValue = 1;
}
dimMode[this->current_num] = TOGGLE_MODE;
togMin[this->current_num] = powerBuf[maxValue];
togMax[this->current_num] = powerBuf[minValue];
toggleReload = 50;
}
void IRAM_ATTR isr_ext()
{
for (int i = 0; i < current_dim; i++ )
if (dimState[i] == ON)
{
zeroCross[i] = 1;
}
}
static int k;
void IRAM_ATTR onTimerISR()
{
toggleCounter++;
for (k = 0; k < current_dim; k++)
{
if (zeroCross[k] == 1 )
{
dimCounter[k]++;
if (dimMode[k] == TOGGLE_MODE)
{
/*****
* TOGGLE DIMMING MODE
*****/
if (dimPulseBegin[k] >= togMax[k])
{
// if reach max dimming value
togDir[k] = false; // downcount
}
if (dimPulseBegin[k] <= togMin[k])
{
// if reach min dimming value
togDir[k] = true; // upcount
}
if (toggleCounter == toggleReload)
{
if (togDir[k] == true) dimPulseBegin[k]++;
else dimPulseBegin[k]--;
}
}
/*****
* DEFAULT DIMMING MODE (NOT TOGGLE)
*****/
if (dimCounter[k] >= dimPulseBegin[k] )
{
digitalWrite(dimOutPin[k], HIGH);
}
if (dimCounter[k] >= (dimPulseBegin[k] + pulseWidth) )
{
digitalWrite(dimOutPin[k], LOW);
zeroCross[k] = 0;
dimCounter[k] = 0;
}
}
}
if (toggleCounter >= toggleReload) toggleCounter = 1;
}
#endif

View File

@@ -0,0 +1,35 @@
#ifndef RBDMCUESP32_H
#define RBDMCUESP32_H
#include "Arduino.h"
#include "RBDdimmer.h"
#include <stdio.h>
#include <esp32-hal-gpio.h>
#include "rom/ets_sys.h"
#include "esp_attr.h"
#include "esp_intr.h"
#include "rom/gpio.h"
#include "soc/gpio_reg.h"
#include "soc/io_mux_reg.h"
#include "soc/gpio_struct.h"
#include "soc/rtc_io_reg.h"
#define ALL_DIMMERS 50
static const uint8_t powerBuf[] = {
100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
90, 89, 88, 87, 86, 85, 84, 83, 82, 81,
80, 79, 78, 77, 76, 75, 74, 73, 72, 71,
70, 69, 68, 67, 66, 65, 64, 63, 62, 61,
60, 59, 58, 57, 56, 55, 54, 53, 52, 51,
50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
};
void IRAM_ATTR isr_ext();
void IRAM_ATTR onTimerISR();
#endif

View File

@@ -0,0 +1,203 @@
#if defined(ARDUINO_ARCH_ESP8266)
#include "RBDmcuESP8266.h"
int pulseWidth = 1;
volatile int current_dim = 0;
int all_dim = 3;
int rise_fall = true;
char user_zero_cross = '0';
int timeoutPin = 435; // 80us
int extIntPin = 2; //z-c
static int toggleCounter = 0;
static int toggleReload = 25;
static dimmerLamp* dimmer[ALL_DIMMERS];
volatile uint16_t dimPower[ALL_DIMMERS];
volatile uint16_t dimOutPin[ALL_DIMMERS];
volatile uint16_t dimZCPin[ALL_DIMMERS];
volatile uint16_t zeroCross[ALL_DIMMERS];
volatile DIMMER_MODE_typedef dimMode[ALL_DIMMERS];
volatile ON_OFF_typedef dimState[ALL_DIMMERS];
volatile uint16_t dimCounter[ALL_DIMMERS];
static uint16_t dimPulseBegin[ALL_DIMMERS];
volatile uint16_t togMax[ALL_DIMMERS];
volatile uint16_t togMin[ALL_DIMMERS];
volatile bool togDir[ALL_DIMMERS];
dimmerLamp::dimmerLamp(int user_dimmer_pin, int zc_dimmer_pin):
dimmer_pin(user_dimmer_pin),
zc_pin(zc_dimmer_pin)
{
current_dim++;
dimmer[current_dim-1] = this;
current_num = current_dim-1;
toggle_state = false;
dimPulseBegin[current_dim-1] = 1;
dimOutPin[current_dim-1] = user_dimmer_pin;
dimZCPin[current_dim-1] = zc_dimmer_pin;
dimCounter[current_dim-1] = 0;
zeroCross[current_dim-1] = 0;
dimMode[current_dim-1] = NORMAL_MODE;
togMin[current_dim-1] = 0;
togMax[current_dim-1] = 1;
pinMode(user_dimmer_pin, OUTPUT);
}
void dimmerLamp::timer_init(void)
{
timer1_attachInterrupt(onTimerISR);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
timer1_write(timeoutPin); //100 us
}
void dimmerLamp::ext_int_init(void)
{
int inPin = dimZCPin[this->current_num];
pinMode(inPin, INPUT_PULLUP);
attachInterrupt(inPin, isr_ext, FALLING);
}
void dimmerLamp::begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF)
{
dimMode[this->current_num] = DIMMER_MODE;
dimState[this->current_num] = ON_OFF;
timer_init();
ext_int_init();
}
void dimmerLamp::setPower(int power)
{
if (power >= 99)
{
power = 99;
}
dimPower[this->current_num] = power;
dimPulseBegin[this->current_num] = powerBuf[power];
delay(1);
}
int dimmerLamp::getPower(void)
{
if (dimState[this->current_num] == ON)
return dimPower[this->current_num];
else return 0;
}
void dimmerLamp::setState(ON_OFF_typedef ON_OFF)
{
dimState[this->current_num] = ON_OFF;
}
bool dimmerLamp::getState(void)
{
bool ret;
if (dimState[this->current_num] == ON) ret = true;
else ret = false;
return ret;
}
void dimmerLamp::changeState(void)
{
if (dimState[this->current_num] == ON) dimState[this->current_num] = OFF;
else
dimState[this->current_num] = ON;
}
DIMMER_MODE_typedef dimmerLamp::getMode(void)
{
return dimMode[this->current_num];
}
void dimmerLamp::setMode(DIMMER_MODE_typedef DIMMER_MODE)
{
dimMode[this->current_num] = DIMMER_MODE;
}
void dimmerLamp::toggleSettings(int minValue, int maxValue)
{
if (maxValue > 99)
{
maxValue = 99;
}
if (minValue < 1)
{
minValue = 1;
}
dimMode[this->current_num] = TOGGLE_MODE;
togMin[this->current_num] = powerBuf[maxValue];
togMax[this->current_num] = powerBuf[minValue];
toggleReload = 50;
}
ICACHE_RAM_ATTR void isr_ext()
{
for (int i = 0; i < current_dim; i++ )
if (dimState[i] == ON)
{
zeroCross[i] = 1;
}
}
static int k;
void ICACHE_RAM_ATTR onTimerISR()
{
toggleCounter++;
for (k = 0; k < current_dim; k++)
{
if (zeroCross[k] == 1 )
{
dimCounter[k]++;
if (dimMode[k] == TOGGLE_MODE)
{
/*****
* TOGGLE DIMMING MODE
*****/
if (dimPulseBegin[k] >= togMax[k])
{
// if reach max dimming value
togDir[k] = false; // downcount
}
if (dimPulseBegin[k] <= togMin[k])
{
// if reach min dimming value
togDir[k] = true; // upcount
}
if (toggleCounter == toggleReload)
{
if (togDir[k] == true) dimPulseBegin[k]++;
else dimPulseBegin[k]--;
}
}
/*****
* DEFAULT DIMMING MODE (NOT TOGGLE)
*****/
if (dimCounter[k] >= dimPulseBegin[k] )
{
digitalWrite(dimOutPin[k], HIGH);
}
if (dimCounter[k] >= (dimPulseBegin[k] + pulseWidth) )
{
digitalWrite(dimOutPin[k], LOW);
zeroCross[k] = 0;
dimCounter[k] = 0;
}
}
}
if (toggleCounter >= toggleReload) toggleCounter = 1;
timer1_write(timeoutPin);
}
#endif

View File

@@ -0,0 +1,28 @@
#ifndef RBDMCUESP8266_H
#define RBDMCUESP8266_H
#include "Arduino.h"
#include "RBDdimmer.h"
#include "pins_arduino.h"
#include <stdio.h>
#include "ets_sys.h"
static const uint8_t powerBuf[] = {
100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
90, 89, 88, 87, 86, 85, 84, 83, 82, 81,
80, 79, 78, 77, 76, 75, 74, 73, 72, 71,
70, 69, 68, 67, 66, 65, 64, 63, 62, 61,
60, 59, 58, 57, 56, 55, 54, 53, 52, 51,
50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
};
#define ALL_DIMMERS 20
void isr_ext(void);
void ICACHE_RAM_ATTR onTimerISR();
#endif

View File

@@ -0,0 +1,214 @@
#if defined(ARDUINO_ARCH_SAM)
#include "RBDmcuSAM.h"
#define __cplusplus
int pulseWidth = 2;
volatile int current_dim = 0;
int all_dim = 3;
int rise_fall = true;
char user_zero_cross = '0';
static int toggleCounter = 0;
static int toggleReload = 25;
static dimmerLamp* dimmer[ALL_DIMMERS];
static uint16_t dimPower[ALL_DIMMERS];
static uint16_t dimOutPin[ALL_DIMMERS];
static uint16_t dimZCPin[ALL_DIMMERS];
static uint16_t zeroCross[ALL_DIMMERS];
static DIMMER_MODE_typedef dimMode[ALL_DIMMERS];
static ON_OFF_typedef dimState[ALL_DIMMERS];
static uint16_t dimCounter[ALL_DIMMERS];
static uint16_t dimPulseBegin[ALL_DIMMERS];
static uint16_t togMax[ALL_DIMMERS];
static uint16_t togMin[ALL_DIMMERS];
static bool togDir[ALL_DIMMERS];
dimmerLamp::dimmerLamp(int user_dimmer_pin, int zc_dimmer_pin):
dimmer_pin(user_dimmer_pin),
zc_pin(zc_dimmer_pin)
{
current_dim++;
dimmer[current_dim-1] = this;
current_num = current_dim-1;
toggle_state = false;
dimPulseBegin[current_dim-1] = 1;
dimOutPin[current_dim-1] = user_dimmer_pin;
dimZCPin[current_dim-1] = zc_dimmer_pin;
dimCounter[current_dim-1] = 0;
zeroCross[current_dim-1] = 0;
dimMode[current_dim-1] = NORMAL_MODE;
togMin[current_dim-1] = 0;
togMax[current_dim-1] = 1;
pinMode(user_dimmer_pin, OUTPUT);
}
void dimmerLamp::timer_init(void)
{
PMC->PMC_PCER0 |= PMC_PCER0_PID29; // Timer Counter 0 channel 2 is TC2
TC0->TC_CHANNEL[2].TC_CCR = TC_CCR_CLKDIS;
TC0->TC_CHANNEL[2].TC_CMR |= TC_CMR_TCCLKS_TIMER_CLOCK1
| TC_CMR_CPCTRG
| TC_CMR_ACPC_SET
| TC_CMR_CPCDIS;
TC0->TC_CHANNEL[2].TC_RC = 4100;
TC0->TC_CHANNEL[2].TC_IER |= TC_IER_CPCS;
TC0->TC_CHANNEL[2].TC_CCR |= TC_CCR_SWTRG | TC_CCR_CLKEN;
NVIC_EnableIRQ(TC2_IRQn);
}
void dimmerLamp::ext_int_init(void)
{
int inPin = dimZCPin[this->current_num];
pinMode(inPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(inPin), isr_ext, RISING);
}
void dimmerLamp::begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF)
{
dimMode[this->current_num] = DIMMER_MODE;
dimState[this->current_num] = ON_OFF;
timer_init();
ext_int_init();
}
void dimmerLamp::setPower(int power)
{
if (power >= 99)
{
power = 99;
}
dimPower[this->current_num] = power;
dimPulseBegin[this->current_num] = powerBuf[power];
delay(1);
}
int dimmerLamp::getPower(void)
{
if (dimState[this->current_num] == ON)
return dimPower[this->current_num];
else return 0;
}
void dimmerLamp::setState(ON_OFF_typedef ON_OFF)
{
dimState[this->current_num] = ON_OFF;
}
bool dimmerLamp::getState(void)
{
bool ret;
if (dimState[this->current_num] == ON) ret = true;
else ret = false;
return ret;
}
void dimmerLamp::changeState(void)
{
if (dimState[this->current_num] == ON) dimState[this->current_num] = OFF;
else
dimState[this->current_num] = ON;
}
DIMMER_MODE_typedef dimmerLamp::getMode(void)
{
return dimMode[this->current_num];
}
void dimmerLamp::setMode(DIMMER_MODE_typedef DIMMER_MODE)
{
dimMode[this->current_num] = DIMMER_MODE;
}
void dimmerLamp::toggleSettings(int minValue, int maxValue)
{
if (maxValue > 99)
{
maxValue = 99;
}
if (minValue < 1)
{
minValue = 1;
}
dimMode[this->current_num] = TOGGLE_MODE;
togMin[this->current_num] = powerBuf[maxValue];
togMax[this->current_num] = powerBuf[minValue];
toggleReload = 50;
}
void isr_ext(void)
{
for (int i = 0; i < current_dim; i++ )
if (dimState[i] == ON)
{
zeroCross[i] = 1;
}
}
static int k, rc;
void TC2_Handler()
{
rc = (TC0->TC_CHANNEL[2].TC_SR & TC_SR_CPCS);
TC0->TC_CHANNEL[2].TC_IMR &= ~TC_SR_CPCS;
toggleCounter++;
for (k = 0; k < current_dim; k++)
{
if (zeroCross[k] == 1 )
{
dimCounter[k]++;
if (dimMode[k] == TOGGLE_MODE)
{
/*****
* TOGGLE DIMMING MODE
*****/
if (dimPulseBegin[k] >= togMax[k])
{
// if reach max dimming value
togDir[k] = false; // downcount
}
if (dimPulseBegin[k] <= togMin[k])
{
// if reach min dimming value
togDir[k] = true; // upcount
}
if (toggleCounter == toggleReload)
{
if (togDir[k] == true) dimPulseBegin[k]++;
else dimPulseBegin[k]--;
}
}
/*****
* DEFAULT DIMMING MODE (NOT TOGGLE)
*****/
if (dimCounter[k] >= dimPulseBegin[k] )
{
digitalWrite(dimOutPin[k], HIGH);
}
if (dimCounter[k] >= (dimPulseBegin[k] + pulseWidth) )
{
digitalWrite(dimOutPin[k], LOW);
zeroCross[k] = 0;
dimCounter[k] = 0;
}
}
}
if (toggleCounter >= toggleReload) toggleCounter = 1;
TC0->TC_CHANNEL[2].TC_IMR |= TC_SR_CPCS;
}
#endif

View File

@@ -0,0 +1,25 @@
#ifndef RBDMCUSAM_H
#define RBDMCUSAM_H
#include "Arduino.h"
#include "RBDdimmer.h"
#include <stdio.h>
static const uint8_t powerBuf[] = {
100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
90, 89, 88, 87, 86, 85, 84, 83, 82, 81,
80, 79, 78, 77, 76, 75, 74, 73, 72, 71,
70, 69, 68, 67, 66, 65, 64, 63, 62, 61,
60, 59, 58, 57, 56, 55, 54, 53, 52, 51,
50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
};
#define ALL_DIMMERS 80
void isr_ext(void);
#endif

View File

@@ -0,0 +1,247 @@
#if defined(ARDUINO_ARCH_SAMD)
#include "RBDmcuSAMD21.h"
int dim_tim[10];
int dim_max[10];
//int dim_begin = 90;
int pulseWidth = 2;
volatile int current_dim = 0;
int all_dim = 3;
int rise_fall = true;
char user_zero_cross = '0';
static int toggleCounter = 0;
static int toggleReload = 25;
static dimmerLamp* dimmer[ALL_DIMMERS];
volatile uint16_t dimPower[ALL_DIMMERS];
volatile uint16_t dimOutPin[ALL_DIMMERS];
volatile uint16_t zeroCross[ALL_DIMMERS];
volatile DIMMER_MODE_typedef dimMode[ALL_DIMMERS];
volatile ON_OFF_typedef dimState[ALL_DIMMERS];
volatile uint16_t dimCounter[ALL_DIMMERS];
static uint16_t dimPulseBegin[ALL_DIMMERS];
volatile uint16_t togMax[ALL_DIMMERS];
volatile uint16_t togMin[ALL_DIMMERS];
volatile bool togDir[ALL_DIMMERS];
dimmerLamp::dimmerLamp(int user_dimmer_pin):
dimmer_pin(user_dimmer_pin)
{
current_dim++;
dimmer[current_dim-1] = this;
current_num = current_dim-1;
toggle_state = false;
dimPulseBegin[current_dim-1] = 1;
dimOutPin[current_dim-1] = user_dimmer_pin;
dimCounter[current_dim-1] = 0;
zeroCross[current_dim-1] = 0;
dimMode[current_dim-1] = NORMAL_MODE;
togMin[current_dim-1] = 0;
togMax[current_dim-1] = 1;
pinMode(user_dimmer_pin, OUTPUT);
__enable_irq();
}
void dimmerLamp::timer_init(void)
{
SYSCTRL->OSC8M.bit.PRESC = 0; // no prescaler (is 8 on reset)
SYSCTRL->OSC8M.reg |= SYSCTRL_OSC8M_ENABLE; // enable source
GCLK->GENDIV.bit.ID = 0x07; // select GCLK_GEN[1]
GCLK->GENDIV.bit.DIV = 0; // no prescaler
GCLK->GENCTRL.bit.SRC = 0x06;
GCLK->GENCTRL.bit.ID = 0x07; // select GCLK_GEN[1]
GCLK->GENCTRL.bit.GENEN = 1; // enable generator
PM->APBCSEL.bit.APBCDIV = 0; // no prescaler
PM->APBCMASK.bit.TC3_ = 1; // enable TC3 interface
// Configure asynchronous clock source
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_TCC2_TC3; // select TC3 peripheral channel
GCLK->CLKCTRL.reg |= GCLK_CLKCTRL_GEN_GCLK7; // select source GCLK_GEN[0]
GCLK->CLKCTRL.bit.CLKEN = 1; // enable TC3 generic clock
// Configure synchronous bus clock
TC3->COUNT8.CTRLA.bit.MODE = 0x1; // Configure Count Mode (16-bit)
TC3->COUNT8.CTRLA.bit.WAVEGEN = 0x01;
TC3->COUNT8.CTRLA.bit.PRESCALER = 0x02; // Configure Prescaler for divide by 2 (500kHz clock to COUNT)
TC3->COUNT8.CTRLBCLR.bit.DIR = 1;
TC3->COUNT8.CTRLC.bit.CPTEN1 = 1;
TC3->COUNT8.INTENSET.bit.MC0 = 1; // Enable TC3 compare mode interrupt generation // Enable match interrupts on compare channel 0
TC3->COUNT8.CC[0].reg = 190; // Initialize compare value for 100mS @ 500kHz
while(TC3->COUNT8.STATUS.bit.SYNCBUSY == 1); // Wait until TC3 is enabled
TC3->COUNT8.CTRLA.bit.ENABLE = 1; // Enable TC3
NVIC_EnableIRQ(TC3_IRQn); // Enable TC3 NVIC Interrupt Line
}
void dimmerLamp::ext_int_init(void)
{
// Configure interrupt port (D8)
PORT->Group[PORTA].WRCONFIG.reg |=
PORT_WRCONFIG_WRPINCFG | // Enables the configuration of PINCFG
PORT_WRCONFIG_WRPMUX | // Enables the configuration of the PMUX for the selected pins
PORT_WRCONFIG_PMUX(MUX_PA15A_EIC_EXTINT15) | // Bulk configuration for PMUX EIC for PA06
PORT_WRCONFIG_PMUXEN | // Enables the PMUX for the pins
PORT_WRCONFIG_PINMASK(PORT_PA15)|
PORT_WRCONFIG_PULLEN |
PORT_WRCONFIG_INEN; // Enable input
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_EIC | GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_CLKEN;
REG_PM_APBCMASK |= PM_APBAMASK_EIC;
while (EIC->CTRL.bit.SWRST | EIC->STATUS.bit.SYNCBUSY);
EIC->CTRL.reg |= EIC_CTRL_SWRST;
EIC->CONFIG[1].reg |= EIC_CONFIG_FILTEN7 | EIC_CONFIG_SENSE7_RISE;
EIC->INTENSET.reg |= EIC_INTENSET_EXTINT15;
EIC->CTRL.reg |= EIC_CTRL_ENABLE; // Enable EIC
NVIC_EnableIRQ(EIC_IRQn);
}
void dimmerLamp::begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF)
{
dimMode[this->current_num] = DIMMER_MODE;
dimState[this->current_num] = ON_OFF;
timer_init();
ext_int_init();
}
void dimmerLamp::setPower(int power)
{
if (power >= 99)
{
power = 99;
}
dimPower[this->current_num] = power;
dimPulseBegin[this->current_num] = powerBuf[power];
delay(1);
}
int dimmerLamp::getPower(void)
{
if (dimState[this->current_num] == ON)
return dimPower[this->current_num];
else return 0;
}
void dimmerLamp::setState(ON_OFF_typedef ON_OFF)
{
dimState[this->current_num] = ON_OFF;
}
bool dimmerLamp::getState(void)
{
bool ret;
if (dimState[this->current_num] == ON) ret = true;
else ret = false;
return ret;
}
void dimmerLamp::changeState(void)
{
if (dimState[this->current_num] == ON) dimState[this->current_num] = OFF;
else
dimState[this->current_num] = ON;
}
DIMMER_MODE_typedef dimmerLamp::getMode(void)
{
return dimMode[this->current_num];
}
void dimmerLamp::setMode(DIMMER_MODE_typedef DIMMER_MODE)
{
dimMode[this->current_num] = DIMMER_MODE;
}
void dimmerLamp::toggleSettings(int minValue, int maxValue)
{
if (maxValue > 99)
{
maxValue = 99;
}
if (minValue < 1)
{
minValue = 1;
}
dimMode[this->current_num] = TOGGLE_MODE;
togMin[this->current_num] = powerBuf[maxValue];
togMax[this->current_num] = powerBuf[minValue];
toggleReload = 50;
}
void EIC_Handler(void)
{
for (int i = 0; i < current_dim; i++ )
if (dimState[i] == ON)
{
zeroCross[i] = 1;
}
EIC->INTFLAG.reg |= EIC_INTFLAG_EXTINT15; // clear interrupt flag
}
static int k;
void TC3_Handler(void)
{
toggleCounter++;
for (k = 0; k < current_dim; k++)
{
if (zeroCross[k] == 1 )
{
dimCounter[k]++;
if (dimMode[k] == TOGGLE_MODE)
{
/*****
* TOGGLE DIMMING MODE
*****/
if (dimPulseBegin[k] >= togMax[k])
{
// if reach max dimming value
togDir[k] = false; // downcount
}
if (dimPulseBegin[k] <= togMin[k])
{
// if reach min dimming value
togDir[k] = true; // upcount
}
if (toggleCounter == toggleReload)
{
if (togDir[k] == true) dimPulseBegin[k]++;
else dimPulseBegin[k]--;
}
}
/*****
* DEFAULT DIMMING MODE (NOT TOGGLE)
*****/
if (dimCounter[k] >= dimPulseBegin[k] )
{
digitalWrite(dimOutPin[k], HIGH);
}
if (dimCounter[k] >= (dimPulseBegin[k] + pulseWidth) )
{
digitalWrite(dimOutPin[k], LOW);
zeroCross[k] = 0;
dimCounter[k] = 0;
}
}
}
if (toggleCounter >= toggleReload) toggleCounter = 1;
TC3->COUNT8.INTFLAG.reg = TC_INTFLAG_MC0; // Acknowledge the interrupt (clear MC0 interrupt flag to re-arm)
}
#endif

View File

@@ -0,0 +1,24 @@
#ifndef RBDMCUSAMD21_H
#define RBDMCUSAMD21_H
#include "Arduino.h"
#include "RBDdimmer.h"
#include "sam.h"
#include <stdio.h>
static const uint8_t powerBuf[] = {
100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
90, 89, 88, 87, 86, 85, 84, 83, 82, 81,
80, 79, 78, 77, 76, 75, 74, 73, 72, 71,
70, 69, 68, 67, 66, 65, 64, 63, 62, 61,
60, 59, 58, 57, 56, 55, 54, 53, 52, 51,
50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
};
#define ALL_DIMMERS 80
#endif

View File

@@ -0,0 +1,200 @@
#if defined(ARDUINO_ARCH_STM32F1)
#include "RBDmcuSTM32F1.h"
#define __cplusplus
int pulseWidth = 2;
volatile int current_dim = 0;
int all_dim = 3;
int rise_fall = true;
char user_zero_cross = '0';
static int toggleCounter = 0;
static int toggleReload = 25;
static dimmerLamp* dimmer[ALL_DIMMERS];
static uint16_t dimPower[ALL_DIMMERS];
static uint16_t dimOutPin[ALL_DIMMERS];
static uint16_t dimZCPin[ALL_DIMMERS];
static uint16_t zeroCross[ALL_DIMMERS];
static DIMMER_MODE_typedef dimMode[ALL_DIMMERS];
static ON_OFF_typedef dimState[ALL_DIMMERS];
static uint16_t dimCounter[ALL_DIMMERS];
static uint16_t dimPulseBegin[ALL_DIMMERS];
static uint16_t togMax[ALL_DIMMERS];
static uint16_t togMin[ALL_DIMMERS];
static bool togDir[ALL_DIMMERS];
dimmerLamp::dimmerLamp(int user_dimmer_pin, int zc_dimmer_pin):
dimmer_pin(user_dimmer_pin),
zc_pin(zc_dimmer_pin)
{
current_dim++;
dimmer[current_dim-1] = this;
current_num = current_dim-1;
toggle_state = false;
dimPulseBegin[current_dim-1] = 1;
dimOutPin[current_dim-1] = user_dimmer_pin;
dimZCPin[current_dim-1] = zc_dimmer_pin;
dimCounter[current_dim-1] = 0;
zeroCross[current_dim-1] = 0;
dimMode[current_dim-1] = NORMAL_MODE;
togMin[current_dim-1] = 0;
togMax[current_dim-1] = 1;
pinMode(user_dimmer_pin, OUTPUT);
}
void dimmerLamp::timer_init(void)
{
Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
Timer3.setPeriod(95); // in microseconds
Timer3.setCompare(TIMER_CH1, 1); // overflow might be small
Timer3.attachInterrupt(TIMER_CH1, onTimerISR);
}
void dimmerLamp::ext_int_init(void)
{
int inPin = dimZCPin[this->current_num];
pinMode(inPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(inPin), isr_ext, RISING);
}
void dimmerLamp::begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF)
{
dimMode[this->current_num] = DIMMER_MODE;
dimState[this->current_num] = ON_OFF;
timer_init();
ext_int_init();
}
void dimmerLamp::setPower(int power)
{
if (power >= 99)
{
power = 99;
}
dimPower[this->current_num] = power;
dimPulseBegin[this->current_num] = powerBuf[power];
delay(1);
}
int dimmerLamp::getPower(void)
{
if (dimState[this->current_num] == ON)
return dimPower[this->current_num];
else return 0;
}
void dimmerLamp::setState(ON_OFF_typedef ON_OFF)
{
dimState[this->current_num] = ON_OFF;
}
bool dimmerLamp::getState(void)
{
bool ret;
if (dimState[this->current_num] == ON) ret = true;
else ret = false;
return ret;
}
void dimmerLamp::changeState(void)
{
if (dimState[this->current_num] == ON) dimState[this->current_num] = OFF;
else
dimState[this->current_num] = ON;
}
DIMMER_MODE_typedef dimmerLamp::getMode(void)
{
return dimMode[this->current_num];
}
void dimmerLamp::setMode(DIMMER_MODE_typedef DIMMER_MODE)
{
dimMode[this->current_num] = DIMMER_MODE;
}
void dimmerLamp::toggleSettings(int minValue, int maxValue)
{
if (maxValue > 99)
{
maxValue = 99;
}
if (minValue < 1)
{
minValue = 1;
}
dimMode[this->current_num] = TOGGLE_MODE;
togMin[this->current_num] = powerBuf[maxValue];
togMax[this->current_num] = powerBuf[minValue];
toggleReload = 50;
}
void isr_ext(void)
{
for (int i = 0; i < current_dim; i++ )
if (dimState[i] == ON)
{
zeroCross[i] = 1;
}
}
static int k;
void onTimerISR()
{
digitalWrite(PC13, !(digitalRead(PC13)));
toggleCounter++;
for (k = 0; k < current_dim; k++)
{
if (zeroCross[k] == 1 )
{
dimCounter[k]++;
if (dimMode[k] == TOGGLE_MODE)
{
/*****
* TOGGLE DIMMING MODE
*****/
if (dimPulseBegin[k] >= togMax[k])
{
// if reach max dimming value
togDir[k] = false; // downcount
}
if (dimPulseBegin[k] <= togMin[k])
{
// if reach min dimming value
togDir[k] = true; // upcount
}
if (toggleCounter == toggleReload)
{
if (togDir[k] == true) dimPulseBegin[k]++;
else dimPulseBegin[k]--;
}
}
/*****
* DEFAULT DIMMING MODE (NOT TOGGLE)
*****/
if (dimCounter[k] >= dimPulseBegin[k] )
{
digitalWrite(dimOutPin[k], HIGH);
}
if (dimCounter[k] >= (dimPulseBegin[k] + pulseWidth) )
{
digitalWrite(dimOutPin[k], LOW);
zeroCross[k] = 0;
dimCounter[k] = 0;
}
}
}
if (toggleCounter >= toggleReload) toggleCounter = 1;
}
#endif

View File

@@ -0,0 +1,24 @@
#ifndef RBDMCUSTM32F1_H
#define RBDMCUSTM32F1_H
#include "Arduino.h"
#include "RBDdimmer.h"
#include <stdio.h>
static const uint8_t powerBuf[] = {
100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
90, 89, 88, 87, 86, 85, 84, 83, 82, 81,
80, 79, 78, 77, 76, 75, 74, 73, 72, 71,
70, 69, 68, 67, 66, 65, 64, 63, 62, 61,
60, 59, 58, 57, 56, 55, 54, 53, 52, 51,
50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
};
void isr_ext(void);
void onTimerISR();
#endif