-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSomfy.ino
185 lines (163 loc) · 3.55 KB
/
Somfy.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
/*
* Modul to controll a Somfy Telis 4 RTS
* remote control via an Arduino and serial
* communication to send commands.
*/
#include <SerialCommand.h>
// Serial protocol command object
SerialCommand SCmd;
// Telis 4 remote control buttons
int myBtn = 3;
int chSelBtn = 4;
int upBtn = 5;
int downBtn = 6;
// Telis 4 remote control leds
int chOneLed = 12;
int chFourLed = 13;
int activeCh;
// Timings [milli seconds]
unsigned long lastchSwitchTime;
int btnPressureTime = 50;
unsigned long minTimeBetweenchSwitching = 5000;
/**
* Initialize variables, pin modes, etc.
*/
void setup() {
Serial.begin(9600);
// input
pinMode(chOneLed, INPUT);
pinMode(chFourLed, INPUT);
// output
pinMode(myBtn, OUTPUT);
pinMode(upBtn, OUTPUT);
pinMode(downBtn, OUTPUT);
pinMode(chSelBtn, OUTPUT);
// timings
lastchSwitchTime = millis();
initActiveCh();
// Setup callbacks for SerialCommand commands
SCmd.addCommand("ACTIVE",printActiveCh);
SCmd.addCommand("UP",cmdUp);
SCmd.addCommand("MY",cmdMy);
SCmd.addCommand("STOP",cmdMy);
SCmd.addCommand("DOWN",cmdDown);
SCmd.addCommand("SWITCH",cmdSwitchch);
SCmd.setDefaultHandler(cmdUnrecognized);
Serial.println("Ready");
}
/**
* Initialize activeCh variable and switch the
* somfy remote to the corresponding selection.
*/
void initActiveCh() {
Serial.println("initActiveCh");
while (!isChOneSelected()) {
switchch(1);
}
activeCh = 1;
}
/**
* Main loop
*/
void loop() {
SCmd.readSerial(); // process serial commands
}
/**
* TODO: add documentation
*/
void cmdSwitchch() {
int chNumber;
char *ch = SCmd.next();
if (ch != NULL) {
chNumber=atoi(ch);
switchch(chNumber);
}
}
/**
* TODO: add documentation
*/
void cmdUp() {
cmdSwitchch();
pressButton(upBtn);
}
/**
* TODO: add documentation
*/
void cmdMy() {
cmdSwitchch();
pressButton(myBtn);
}
/**
* TODO: add documentation
*/
void cmdDown() {
cmdSwitchch();
pressButton(downBtn);
}
/**
* This gets set as the default handler,
* and gets called when no other command matches.
*/
void cmdUnrecognized(const char *command) {
Serial.print("Valid commands: 'ACTIVE', 'UP $ch'");
Serial.println(", 'DOWN $ch', 'MY $ch', 'STOP $ch', 'SWITCH $ch'");
}
/**
* Checks the selected ch of the somfy remote and
* returns true if the ch one is active.
*/
boolean isChOneSelected() {
pressButton(chSelBtn); // let blink the leds
boolean chOneInput = digitalRead(chOneLed);
boolean chFourInput = digitalRead(chFourLed);
return chOneInput && !chFourInput;
}
/**
* Switch remote control to the given channel.
*
* The remote supports the following channel
* - ch 1
* - ch 2
* - ch 3
* - ch 4
* - ch 5 -> all channel together
*
* First pressure on the button shows the actual
* active channel, seconde one switch to the
* next channel.
*/
void switchch(int ch) {
if (lastchSwitchTime + minTimeBetweenchSwitching < millis()) {
int times;
if (ch > activeCh) {
times = ch - activeCh;
} else {
// example: activeCh = 4, ch = 2 -> times must be 3
times = 5 - activeCh + ch;
}
lastchSwitchTime = millis();
int var = 0;
while(var < times + 1){
pressButton(chSelBtn);
var++;
}
activeCh = ch;
printActiveCh();
}
}
/**
* Simulate pressing a remote control button
*/
void pressButton(int btn) {
digitalWrite(btn, HIGH);
delay(btnPressureTime);
digitalWrite(btn, LOW);
delay(btnPressureTime);
}
/**
* Print the active ch to Serial output for
* debugging purposes.
*/
void printActiveCh() {
Serial.println(activeCh);
}