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 CMD_ALL_ON 0xfe
32 #define CMD_ALL_OFF 0xfc
35 #define CMD_SET_SERIAL 0xfa
38 USB_STRING_DESCRIPTOR_HEADER(5),
39 '1', '2', '3', '4', '5',
42 unsigned long cmd = 0;
43 int repeat = 0, wait = 0;
46 PROGMEM const char usbHidReportDescriptor[22] = {
47 0x06, 0x00, 0xff, /* USAGE PAGE (Generic Desktop) */
48 0x09, 0x01, /* USAGE (Vendor Usage 1) */
49 0xa1, 0x01, /* COLLECTION (Application) */
50 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
51 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
52 0x75, 0x08, /* REPORT_SIZE (8) */
53 0x95, 0x08, /* REPORT_COUNT (8) */
54 0x09, 0x00, /* USAGE (Undefined) */
55 0xb2, 0x02, 0x01, /* FEATURE (Data, Var, Abs, Buf) */
56 0xc0 /* END_COLLECTION */
59 inline char hexdigit(int i)
61 return (i < 10) ? ('0' + i) : ('A' - 10 + i);
64 inline int digithex(char i)
66 if (i >= '0' && i <= '9')
69 if (i >= 'A' && i <= 'F')
72 if (i >= 'a' && i <= 'f')
78 void fetch_serno(void)
80 eeprom_read_block(&serno, 0, 4);
81 if (serno == 0xffffffff) {
82 /* If the EEPROM is blank, return a default serial # */
89 serno_str[1] = hexdigit((serno >> 20) & 0xF);
90 serno_str[2] = hexdigit((serno >> 16) & 0xF);
91 serno_str[3] = hexdigit((serno >> 12) & 0xF);
92 serno_str[4] = hexdigit((serno >> 8) & 0xF);
93 serno_str[5] = hexdigit((serno >> 4) & 0xF);
97 void update_serno(uchar *buf, uchar len)
102 for (i = 0; i < 5; i++) {
103 serno |= digithex(buf[i]);
108 * I have no idea why this gets stored 3 times, but the original
111 eeprom_write_block(&serno, (void *) 0x00, 4);
112 eeprom_write_block(&serno, (void *) 0x40, 4);
113 eeprom_write_block(&serno, (void *) 0x80, 4);
115 for (i = 0; i < 5; i++) {
116 serno_str[i + 1] = buf[i];
120 usbMsgLen_t usbFunctionSetup(uchar data[8])
122 usbRequest_t *rq = (void *) data;
124 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
125 if ((rq->bRequest == USBRQ_HID_GET_REPORT) ||
126 (rq->bRequest == USBRQ_HID_SET_REPORT)) {
134 usbMsgLen_t usbFunctionDescriptor(usbRequest_t *rq)
136 if (rq->wValue.bytes[1] == USBDESCR_STRING &&
137 rq->wValue.bytes[0] == 3) {
138 usbMsgPtr = (usbMsgPtr_t) serno_str;
139 return sizeof(serno_str);
144 uchar usbFunctionRead(uchar *data, uchar len)
149 for (i = 0; i < 5; i++) {
150 data[i] = serno_str[i + 1];
152 data[5] = data[6] = 0;
160 uchar usbFunctionWrite(uchar *data, uchar len)
162 if (data[0] == CMD_ALL_ON) {
167 } else if (data[0] == CMD_ALL_OFF) {
172 } else if (data[0] == CMD_ON) {
191 state |= (1 << (data[1] - 1));
192 } else if (data[0] == CMD_OFF) {
211 state &= ~(1 << (data[1] - 1));
212 } else if (data[0] == CMD_SET_SERIAL) {
213 update_serno(&data[1], 6);
219 void t433_transmit_bit(bool value)
227 PORTB &= ~(1 << PB0);
234 void t433_send(unsigned long code, unsigned int length)
239 for (i = length - 1; i >= 0; i--) {
240 if (code & (1L << i)) {
241 t433_transmit_bit(true);
243 t433_transmit_bit(false);
246 /* Send a sync bit */
249 PORTB &= ~(1 << PB0);
254 int __attribute__((noreturn)) main(void)
263 usbDeviceDisconnect();
273 /* Set the 433MHz transmitter bit to output mode */
277 sei(); /* We're ready to go; enable interrupts */