]> the.earth.li Git - energenie-attiny.git/blob - main.c
Add a wait before sending via the 433MHz transmitter
[energenie-attiny.git] / main.c
1 /*
2  * Basic firmware to control Some Energenie 433MHz sockets (ENER002-4)
3  * as if they were a www.dcttech.com 4 port USB Relay board
4  *
5  * Copyright 2018 Jonathan McDowell <noodles@earth.li>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <stdbool.h>
21 #include <avr/eeprom.h>
22 #include <avr/interrupt.h>
23 #include <avr/io.h>
24 #include <avr/wdt.h>
25 #include <util/delay.h>
26
27 #include <avr/pgmspace.h>
28 #include "usbdrv.h"
29 #include "libs-device/osccal.h"
30
31 #define CMD_ALL_ON 0xfe
32 #define CMD_ALL_OFF 0xfc
33 #define CMD_ON 0xff
34 #define CMD_OFF 0xfd
35 #define CMD_SET_SERIAL 0xfa
36
37 /* Energenie 24 bit address , last 4 all 0 for code */
38 #define ENER_ADDR 0x123450
39
40 uchar serno_read = 0;
41 uchar serno[6];
42 unsigned long cmd = 0;
43 int repeat = 0, wait = 0;
44
45 PROGMEM const char usbHidReportDescriptor[22] = {
46         0x06, 0x00, 0xff,               /* USAGE PAGE (Generic Desktop) */
47         0x09, 0x01,                     /* USAGE (Vendor Usage 1) */
48         0xa1, 0x01,                     /* COLLECTION (Application) */
49         0x15, 0x00,                     /*   LOGICAL_MINIMUM (0) */
50         0x26, 0xff, 0x00,               /*   LOGICAL_MAXIMUM (255) */
51         0x75, 0x08,                     /*   REPORT_SIZE (8) */
52         0x95, 0x08,                     /*   REPORT_COUNT (8) */
53         0x09, 0x00,                     /*   USAGE (Undefined) */
54         0xb2, 0x02, 0x01,               /*   FEATURE (Data, Var, Abs, Buf) */
55         0xc0                            /* END_COLLECTION */
56 };
57
58 void fetch_serno(void)
59 {
60         if (!serno_read) {
61                 eeprom_read_block(serno, 0, 6);
62                 if (serno[0] == 0xff) {
63                         /* If the EEPROM is blank, return a default serial # */
64                         serno[0] = 'U';
65                         serno[1] = 'N';
66                         serno[2] = 'S';
67                         serno[3] = 'E';
68                         serno[4] = 'T';
69                         serno[5] = 0;
70                 }
71                 serno_read = 1;
72         }
73 }
74
75 void update_serno(uchar *buf, uchar len)
76 {
77         uchar i;
78
79         /*
80          * I have no idea why this gets stored 3 times, but the original
81          * firmware does it.
82          */
83         eeprom_write_block(buf, (void *) 0x00, len);
84         eeprom_write_block(buf, (void *) 0x40, len);
85         eeprom_write_block(buf, (void *) 0x80, len);
86
87         for (i = 0; i < 6; i++) {
88                 serno[i] = buf[i];
89         }
90 }
91
92 usbMsgLen_t usbFunctionSetup(uchar data[8])
93 {
94         usbRequest_t *rq = (void *) data;
95
96         if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
97                 if ((rq->bRequest == USBRQ_HID_GET_REPORT) ||
98                                 (rq->bRequest == USBRQ_HID_SET_REPORT)) {
99                         return 0xFF;
100                 }
101         }
102
103         return 0;
104 }
105
106 uchar usbFunctionRead(uchar *data, uchar len)
107 {
108         uchar i;
109
110         if (len != 0) {
111                 fetch_serno();
112                 for (i = 0; i < 6; i++) {
113                         data[i] = serno[i];
114                 }
115                 data[6] = data[7] = 0;
116                 if (PORTB & (1 << PB0)) {
117                         data[7] = 1;
118                 }
119                 return len;
120         }
121
122         return 0;
123 }
124
125 uchar usbFunctionWrite(uchar *data, uchar len)
126 {
127         if (data[0] == CMD_ALL_ON) {
128                 cmd = ENER_ADDR | 0xd;
129                 wait = 200;
130                 repeat = 5;
131         } else if (data[0] == CMD_ALL_OFF) {
132                 cmd = ENER_ADDR | 0xc;
133                 wait = 10;
134                 repeat = 5;
135         } else if (data[0] == CMD_ON) {
136                 wait = 200;
137                 switch (data[1]) {
138                 case 1:
139                         cmd = ENER_ADDR | 0xf;
140                         repeat = 5;
141                         break;
142                 case 2:
143                         cmd = ENER_ADDR | 0x7;
144                         repeat = 5;
145                         break;
146                 case 3:
147                         cmd = ENER_ADDR | 0xb;
148                         repeat = 5;
149                         break;
150                 case 4:
151                         cmd = ENER_ADDR | 0x3;
152                         repeat = 5;
153                         break;
154                 default:
155                         break;
156                 }
157         } else if (data[0] == CMD_OFF) {
158                 wait = 200;
159                 switch (data[1]) {
160                 case 1:
161                         cmd = ENER_ADDR | 0xe;
162                         repeat = 5;
163                         break;
164                 case 2:
165                         cmd = ENER_ADDR | 0x6;
166                         repeat = 5;
167                         break;
168                 case 3:
169                         cmd = ENER_ADDR | 0xa;
170                         repeat = 5;
171                         break;
172                 case 4:
173                         cmd = ENER_ADDR | 0x2;
174                         repeat = 5;
175                         break;
176                 default:
177                         break;
178                 }
179         } else if (data[0] == CMD_SET_SERIAL) {
180                 update_serno(&data[1], 6);
181         }
182
183         return len;
184 }
185
186 void t433_transmit_bit(bool value)
187 {
188         PORTB |= 1 << PB0;
189         if (value)
190                 _delay_us(600);
191         else
192                 _delay_us(200);
193
194         PORTB &= ~(1 << PB0);
195         if (value)
196                 _delay_us(200);
197         else
198                 _delay_us(600);
199 }
200
201 void t433_send(unsigned long code, unsigned int length)
202 {
203         int i;
204
205         cli();
206         for (i = length - 1; i >= 0; i--) {
207                 if (code & (1L << i)) {
208                         t433_transmit_bit(true);
209                 } else {
210                         t433_transmit_bit(false);
211                 }
212         }
213         /* Send a sync bit */
214         PORTB |= 1 << PB0;
215         _delay_us(200);
216         PORTB &= ~(1 << PB0);
217         sei();
218         _delay_ms(30);
219 }
220
221 int __attribute__((noreturn)) main(void)
222 {
223         unsigned char i;
224
225         wdt_enable(WDTO_1S);
226
227         usbInit();
228         usbDeviceDisconnect();
229
230         i = 0;
231         while (--i) {
232                 wdt_reset();
233                 _delay_ms(1);
234         }
235
236         usbDeviceConnect();
237
238         /* Set the 433MHz transmitter bit to output mode */
239         DDRB |= (1 << PB0);
240         PORTB &= (1 << PB0);
241
242         sei(); /* We're ready to go; enable interrupts */
243
244         while (1) {
245                 wdt_reset();
246                 usbPoll();
247                 if (cmd) {
248                         if (wait) {
249                                 wait--;
250                         } else {
251                                 t433_send(cmd, 24);
252                                 if (--repeat == 0)
253                                         cmd = 0;
254                         }
255                 }
256         }
257 }