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
5 * Copyright 2018 Jonathan McDowell <noodles@earth.li>
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.
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.
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/>.
21 #include <avr/eeprom.h>
22 #include <avr/interrupt.h>
25 #include <util/delay.h>
27 #include <avr/pgmspace.h>
29 #include "libs-device/osccal.h"
31 #define REPEAT_COUNT 10
33 #define CMD_ALL_ON 0xfe
34 #define CMD_ALL_OFF 0xfc
37 #define CMD_SET_SERIAL 0xfa
40 USB_STRING_DESCRIPTOR_HEADER(5),
41 '1', '2', '3', '4', '5',
44 unsigned long cmd = 0;
45 int repeat = 0, wait = 0;
48 PROGMEM const char usbHidReportDescriptor[22] = {
49 0x06, 0x00, 0xff, /* USAGE PAGE (Generic Desktop) */
50 0x09, 0x01, /* USAGE (Vendor Usage 1) */
51 0xa1, 0x01, /* COLLECTION (Application) */
52 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
53 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
54 0x75, 0x08, /* REPORT_SIZE (8) */
55 0x95, 0x08, /* REPORT_COUNT (8) */
56 0x09, 0x00, /* USAGE (Undefined) */
57 0xb2, 0x02, 0x01, /* FEATURE (Data, Var, Abs, Buf) */
58 0xc0 /* END_COLLECTION */
61 inline char hexdigit(int i)
63 return (i < 10) ? ('0' + i) : ('A' - 10 + i);
66 inline int digithex(char i)
68 if (i >= '0' && i <= '9')
71 if (i >= 'A' && i <= 'F')
74 if (i >= 'a' && i <= 'f')
80 void fetch_serno(void)
82 eeprom_read_block(&serno, 0, 4);
83 if (serno == 0xffffffff) {
84 /* If the EEPROM is blank, return a default serial # */
91 serno_str[1] = hexdigit((serno >> 20) & 0xF);
92 serno_str[2] = hexdigit((serno >> 16) & 0xF);
93 serno_str[3] = hexdigit((serno >> 12) & 0xF);
94 serno_str[4] = hexdigit((serno >> 8) & 0xF);
95 serno_str[5] = hexdigit((serno >> 4) & 0xF);
99 void update_serno(uchar *buf, uchar len)
104 for (i = 0; i < 5; i++) {
105 serno |= digithex(buf[i]);
110 * I have no idea why this gets stored 3 times, but the original
113 eeprom_write_block(&serno, (void *) 0x00, 4);
114 eeprom_write_block(&serno, (void *) 0x40, 4);
115 eeprom_write_block(&serno, (void *) 0x80, 4);
117 for (i = 0; i < 5; i++) {
118 serno_str[i + 1] = buf[i];
122 usbMsgLen_t usbFunctionSetup(uchar data[8])
124 usbRequest_t *rq = (void *) data;
126 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
127 if ((rq->bRequest == USBRQ_HID_GET_REPORT) ||
128 (rq->bRequest == USBRQ_HID_SET_REPORT)) {
136 usbMsgLen_t usbFunctionDescriptor(usbRequest_t *rq)
138 if (rq->wValue.bytes[1] == USBDESCR_STRING &&
139 rq->wValue.bytes[0] == 3) {
140 usbMsgPtr = (usbMsgPtr_t) serno_str;
141 return sizeof(serno_str);
146 uchar usbFunctionRead(uchar *data, uchar len)
151 for (i = 0; i < 5; i++) {
152 data[i] = serno_str[i + 1];
154 data[5] = data[6] = 0;
162 uchar usbFunctionWrite(uchar *data, uchar len)
164 if (data[0] == CMD_ALL_ON) {
168 repeat = REPEAT_COUNT;
169 } else if (data[0] == CMD_ALL_OFF) {
173 repeat = REPEAT_COUNT;
174 } else if (data[0] == CMD_ON) {
192 repeat = REPEAT_COUNT;
193 state |= (1 << (data[1] - 1));
194 } else if (data[0] == CMD_OFF) {
212 repeat = REPEAT_COUNT;
213 state &= ~(1 << (data[1] - 1));
214 } else if (data[0] == CMD_SET_SERIAL) {
215 update_serno(&data[1], 6);
221 void t433_transmit_bit(bool value)
229 PORTB &= ~(1 << PB0);
236 void t433_send(unsigned long code, unsigned int length)
241 for (i = length - 1; i >= 0; i--) {
242 if (code & (1L << i)) {
243 t433_transmit_bit(true);
245 t433_transmit_bit(false);
248 /* Send a sync bit */
251 PORTB &= ~(1 << PB0);
256 int __attribute__((noreturn)) main(void)
265 usbDeviceDisconnect();
275 /* Set the 433MHz transmitter bit to output mode */
279 sei(); /* We're ready to go; enable interrupts */