-
Notifications
You must be signed in to change notification settings - Fork 100
/
main.cpp
130 lines (100 loc) · 3.01 KB
/
main.cpp
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
#include <Arduino.h>
#include <Adafruit_TinyUSB.h>
#include <knx.h>
/*
* USB stuff
*/
#define STRINGIFY(s) XSTRINGIFY(s)
#define XSTRINGIFY(s) #s
#pragma message ("USB_VID=" STRINGIFY(USB_VID))
#pragma message ("USB_PID=" STRINGIFY(USB_PID))
#pragma message ("USB_MANUFACTURER=" STRINGIFY(USB_MANUFACTURER))
#pragma message ("USB_PRODUCT=" STRINGIFY(USB_PRODUCT))
Adafruit_USBD_HID usb_hid;
// Invoked when received SET_REPORT control request or
// received data on interrupt OUT endpoint
void setReportCallback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* data, uint16_t bufSize)
{
// we don't use multiple report and report ID
(void) report_id;
(void) report_type;
UsbTunnelInterface::receiveHidReport(data, bufSize);
}
bool sendHidReport(uint8_t* data, uint16_t length)
{
// We do not use reportId of the TinyUSB sendReport()-API here but instead provide it in the first byte of the buffer
return usb_hid.sendReport(0, data, length);
}
bool isSendHidReportPossible()
{
return usb_hid.ready();
}
/*
* KNX stuff
*/
// create macros easy access to group objects
#define goTemperature knx.getGroupObject(1)
#define goHumidity knx.getGroupObject(2)
uint32_t cyclSend = 0;
uint8_t sendCounter = 0;
long lastsend = 0;
/******************************************************************************************/
/*
* setup()
*/
void setup(void)
{
Serial1.begin(115200);
ArduinoPlatform::SerialDebug = &Serial1;
Serial1.println("Start.");
usb_hid.enableOutEndpoint(true);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(UsbTunnelInterface::getKnxHidReportDescriptor(), UsbTunnelInterface::getHidReportDescriptorLength());
usb_hid.setReportCallback(NULL, setReportCallback);
usb_hid.begin();
// wait until device mounted
while ( !USBDevice.mounted() )
delay(1);
println("KNX USB Interface enabled.");
// read adress table, association table, groupobject table and parameters from eeprom
knx.readMemory();
if (knx.individualAddress() == 0)
knx.progMode(true);
if (knx.configured())
{
cyclSend = knx.paramInt(0);
Serial1.print("Zykl. send:");
Serial1.println(cyclSend);
goTemperature.dataPointType(Dpt(9, 1));
goHumidity.dataPointType(Dpt(9, 1));
}
// start the framework.
knx.start();
}
/*
* loop()
*/
void loop(void)
{
// don't delay here to much. Otherwise you might lose packages or mess up the timing with ETS
knx.loop();
// only run the application code if the device was configured with ETS
if (!knx.configured())
return;
long now = millis();
if ((now - lastsend) < 3000)
return;
lastsend = now;
float temp = 1.2345;
float humi = 60.2;
String output = String(millis());
output += ", " + String(temp);
output += ", " + String(humi);
Serial1.println(output);
if (sendCounter++ == cyclSend)
{
sendCounter = 0;
goTemperature.value(temp);
goHumidity.value(humi);
}
}