-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDemoSketch.ino
executable file
·201 lines (165 loc) · 5.96 KB
/
DemoSketch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <KonnektingDevice.h>
// include device related configuration code, created by "KONNEKTING CodeGenerator"
#include "kdevice_DemoSketch.h"
// ################################################
// ### BOARD CONFIGURATION
// ################################################
#ifdef __AVR_ATmega328P__
// Uno/Nano/ProMini
#define KNX_SERIAL Serial // D0=RX/D1=TX
#define PROG_LED_PIN 11 // External LED
#define TEST_LED LED_BUILTIN // On board LED on pin D13
#define PROG_BUTTON_PIN 3 // pin with interrupt
// ATmega328P has only one UART, lets use virtual one
#include <SoftwareSerial.h>
SoftwareSerial softserial(11, 10); // D11=RX/D10=TX
#define DEBUGSERIAL softserial
#elif __AVR_ATmega32U4__
// Leonardo/Micro/ProMicro
#define KNX_SERIAL Serial1 // D0=RX/D1=TX
#define PROG_LED_PIN 11 // External LED
#define TEST_LED LED_BUILTIN // On board LED on pin D13
#define PROG_BUTTON_PIN 3 // pin with interrupt
#define DEBUGSERIAL Serial // USB port
#elif ARDUINO_ARCH_SAMD
// Zero/M0
#define KNX_SERIAL Serial1 // D0=RX/D1=TX
#define PROG_LED_PIN 11 // External LED
#define TEST_LED LED_BUILTIN // On board LED on pin D13
#define PROG_BUTTON_PIN 3 // pin with interrupt
#define DEBUGSERIAL SerialUSB // USB port
// Arduino Zero has no "real" internal EEPROM,
// so we can use an external I2C EEPROM.
#include "EEPROM_24AA256.h" // external EEPROM
#elif ARDUINO_ARCH_STM32
// STM32 NUCLEO Boards
// Option Serial interface: "Enabled with generic Serial" should be enabled
// Create a new Serial on Pins PA10 & PA9
// Arduino-Header: D2(PA10)=RX/D8(PA9)=TX
HardwareSerial Serial1(PA10, PA9);
#define KNX_SERIAL Serial1
#define PROG_LED_PIN 11 // External LED
#define TEST_LED LED_BUILTIN // On board LED on pin D13
#define PROG_BUTTON_PIN USER_BTN // On board button
#define DEBUGSERIAL Serial // USB port
#elif ESP8266
// ESP8266
#define KNX_SERIAL Serial // swaped Serial on D7(GPIO13)=RX/GPIO15(D8)=TX
#define PROG_LED_PIN 14 // External LED
#define TEST_LED 16 // External LED
#define PROG_BUTTON_PIN 0 // Flash/IO0 button
#define DEBUGSERIAL Serial1 // the 2nd serial port with TX only (GPIO2/D4)
#else
// All other boards
#error "Sorry, you board is not supported"
#endif
// ################################################
// ### DEBUG CONFIGURATION
// ################################################
//#define KDEBUG // comment this line to disable DEBUG mode
#ifdef KDEBUG
#include <DebugUtil.h>
#endif
// ################################################
// ### Global variables, sketch related
// ################################################
unsigned long blinkDelay = 2500; // default value
unsigned long lastmillis = millis();
int laststate = false;
// ################################################
// ### SETUP
// ################################################
void setup()
{
// debug related stuff
#ifdef KDEBUG
// Start debug serial with 115200 bauds
DEBUGSERIAL.begin(115200);
#if defined(__AVR_ATmega32U4__) || defined(__SAMD21G18A__)
// wait for serial port to connect. Needed for Leonardo/Micro/ProMicro/Zero only
while (!DEBUGSERIAL)
#endif
// make debug serial port known to debug class
// Means: KONNEKTING will sue the same serial port for console debugging
Debug.setPrintStream(&DEBUGSERIAL);
#endif
Debug.print(F("KONNEKTING DemoSketch\n"));
pinMode(TEST_LED, OUTPUT);
/*
* Only required when using external eeprom (or similar) storage.
* function pointers should match the methods you have implemented above.
* If no external eeprom required, please remove all three Konnekting.setMemory* lines below
*/
#ifdef __SAMD21G18A__
Wire.begin();
Konnekting.setMemoryReadFunc(&readMemory);
Konnekting.setMemoryWriteFunc(&writeMemory);
Konnekting.setMemoryUpdateFunc(&updateMemory);
Konnekting.setMemoryCommitFunc(&commitMemory);
#endif
// Initialize KNX enabled Arduino Board
Konnekting.init(KNX_SERIAL,
PROG_BUTTON_PIN,
PROG_LED_PIN,
MANUFACTURER_ID,
DEVICE_ID,
REVISION);
// If device has been parametrized with KONNEKTING Suite, read params from EEPROM
// Otherwise continue with global default values from sketch
if (!Konnekting.isFactorySetting())
{
blinkDelay = (int)Konnekting.getUINT16Param(PARAM_blinkDelay); //blink every xxxx ms
}
lastmillis = millis();
Debug.println(F("Toggle LED every %d ms."), blinkDelay);
Debug.println(F("Setup is ready. go to loop..."));
}
// ################################################
// ### LOOP
// ################################################
void loop()
{
// Do KNX related stuff (like sending/receiving KNX telegrams)
// This is required in every KONNEKTING aplication sketch
Knx.task();
unsigned long currentmillis = millis();
/*
* only do measurements and other sketch related stuff if not in programming mode
* means: only when konnekting is ready for appliction
*/
if (Konnekting.isReadyForApplication())
{
if (currentmillis - lastmillis >= blinkDelay)
{
Debug.println(F("Actual state: %d"), laststate);
Knx.write(COMOBJ_trigger, laststate);
laststate = !laststate;
lastmillis = currentmillis;
digitalWrite(TEST_LED, HIGH);
Debug.println(F("DONE"));
}
}
}
// ################################################
// ### KNX EVENT CALLBACK
// ################################################
void knxEvents(byte index)
{
switch (index)
{
case COMOBJ_ledOnOff: // object index has been updated
if (Knx.read(COMOBJ_ledOnOff))
{
digitalWrite(TEST_LED, HIGH);
Debug.println(F("Toggle LED: on"));
}
else
{
digitalWrite(TEST_LED, LOW);
Debug.println(F("Toggle LED: off"));
}
break;
default:
break;
}
};