-
Notifications
You must be signed in to change notification settings - Fork 14
/
app_main.cpp
166 lines (134 loc) · 3.74 KB
/
app_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
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
/*
* Copyright (c) 2018 Oliver Stefan <o.stefan252@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*/
#include "app_RGBswitch.h"
#include "com_objs.h"
#include "params.h"
#include <sblib/eibBCU1.h>
#include <sblib/timeout.h>
APP_VERSION("SBrgb_sw", "1", "10")
// Digital pin for LED
#define PIO_LED PIO3_3
#define DIRECT_IO
// Debouncers for inputs
Debouncer inputDebouncer[NUM_CHANNELS];
const byte* channelParams;
const byte* LEDparams;
#ifdef DIRECT_IO
// Input pins
const int inputPins[] =
{ PIO1_7, PIO1_10, PIO0_1, PIO2_4, PIO2_8, PIO2_10 };
void setupIO(void)
{
// Configure the input pins and initialize the debouncers with the current
// value of the pin.
for (int channel = 0; channel < NUM_CHANNELS; ++channel)
{
pinMode(inputPins[channel], INPUT | HYSTERESIS | PULL_UP);
inputDebouncer[channel].init(digitalRead(inputPins[channel]));
}
}
void scanIO(void)
{
}
int readIO(int channel)
{
return !digitalRead(inputPins[channel]);
}
#else
#include <sblib/spi.h>
SPI spi(SPI_PORT_0);
static unsigned int ioState;
void setupIO(void)
{
ioState = 0;
pinMode(PIO0_8, OUTPUT | SPI_MISO); // IO6
pinMode(PIO0_9, OUTPUT); // IO7
pinMode(PIO2_11, OUTPUT | SPI_CLOCK); // IO8
spi.setClockDivider(128);
spi.begin();
spi.setDataSize(SPI_DATA_8BIT);
}
void scanIO(void)
{
digitalWrite(PIO0_9, 0);
digitalWrite(PIO0_9, 1);
ioState = spi.transfer(0x00);
}
int readIO(int channel)
{
return ioState & (1 << channel);
}
#endif
/**
* Application setup
*/
BcuBase* setup()
{
channelParams = bcu.userMemoryPtr(EE_CHANNEL_PARAMS_BASE);
LEDparams = bcu.userMemoryPtr(EE_LED_PARAMS_BASE);
bcu.setProgPin(PIO2_11);
bcu.begin(76, 0x474, 2); // We are a "Selfbus RGB Taster" device, version 0.2
// onboard LEDs ausschalten
pinMode(PIO1_0, OUTPUT);
digitalWrite(PIO1_0, 0);
pinMode(PIO2_10, OUTPUT);
digitalWrite(PIO2_10, 0);
setupIO();
// Handle configured power-up delay
unsigned int startupTimeout = calculateTime
( bcu.userEeprom->getUInt8(EE_BUS_RETURN_DELAY_BASE) >> 4
, bcu.userEeprom->getUInt8(EE_BUS_RETURN_DELAY_FACT) & 0x7F
);
Timeout delay;
int debounceTime = bcu.userEeprom->getUInt8(EE_INPUT_DEBOUNCE_TIME) >> 1;
delay.start(startupTimeout);
while (delay.started() && !delay.expired())
{ // while we wait for the power on delay to expire we debounce the input channels
scanIO();
for (int channel = 0; channel < NUM_CHANNELS; ++channel)
{
inputDebouncer[channel].debounce(readIO(channel), debounceTime);
}
waitForInterrupt();
}
initApplication();
return (&bcu);
}
/**
* The application's main.
*/
void loop()
{
int debounceTime = bcu.userEeprom->getUInt8(EE_INPUT_DEBOUNCE_TIME) >> 1;
int objno, channel, value, lastValue;
scanIO();
// Handle the input pins
for (channel = 0; channel < NUM_CHANNELS; ++channel)
{
lastValue = inputDebouncer[channel].value();
value = inputDebouncer[channel].debounce(readIO(channel), debounceTime);
if (lastValue != value)
inputChanged(channel, value);
}
// Handle updated communication objects
while ((objno = bcu.comObjects->nextUpdatedObject()) >= 0)
{
objectUpdated(objno);
}
// Handle timed functions (e.g. periodic update)
handlePeriodic();
// Sleep up to 1 millisecond if there is nothing to do
if (bcu.bus->idle())
waitForInterrupt();
}
/**
* The processing loop while no KNX-application is loaded
*/
void loop_noapp()
{
}