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;
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 */
58 inline char hexdigit(int i)
60 return (i < 10) ? ('0' + i) : ('A' - 10 + i);
63 inline int digithex(char i)
65 if (i >= '0' && i <= '9')
68 if (i >= 'A' && i <= 'F')
71 if (i >= 'a' && i <= 'f')
77 void fetch_serno(void)
79 eeprom_read_block(&serno, 0, 4);
80 if (serno == 0xffffffff) {
81 /* If the EEPROM is blank, return a default serial # */
88 serno_str[1] = hexdigit((serno >> 20) & 0xF);
89 serno_str[2] = hexdigit((serno >> 16) & 0xF);
90 serno_str[3] = hexdigit((serno >> 12) & 0xF);
91 serno_str[4] = hexdigit((serno >> 8) & 0xF);
92 serno_str[5] = hexdigit((serno >> 4) & 0xF);
96 void update_serno(uchar *buf, uchar len)
101 for (i = 0; i < 5; i++) {
102 serno |= digithex(buf[i]);
107 * I have no idea why this gets stored 3 times, but the original
110 eeprom_write_block(&serno, (void *) 0x00, 4);
111 eeprom_write_block(&serno, (void *) 0x40, 4);
112 eeprom_write_block(&serno, (void *) 0x80, 4);
114 for (i = 0; i < 5; i++) {
115 serno_str[i + 1] = buf[i];
119 usbMsgLen_t usbFunctionSetup(uchar data[8])
121 usbRequest_t *rq = (void *) data;
123 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
124 if ((rq->bRequest == USBRQ_HID_GET_REPORT) ||
125 (rq->bRequest == USBRQ_HID_SET_REPORT)) {
133 usbMsgLen_t usbFunctionDescriptor(usbRequest_t *rq)
135 if (rq->wValue.bytes[1] == USBDESCR_STRING &&
136 rq->wValue.bytes[0] == 3) {
137 usbMsgPtr = (usbMsgPtr_t) serno_str;
138 return sizeof(serno_str);
143 uchar usbFunctionRead(uchar *data, uchar len)
148 for (i = 0; i < 5; i++) {
149 data[i] = serno_str[i + 1];
151 data[5] = data[6] = data[7] = 0;
152 if (PORTB & (1 << PB0)) {
161 uchar usbFunctionWrite(uchar *data, uchar len)
163 if (data[0] == CMD_ALL_ON) {
167 } else if (data[0] == CMD_ALL_OFF) {
171 } else if (data[0] == CMD_ON) {
193 } else if (data[0] == CMD_OFF) {
215 } else if (data[0] == CMD_SET_SERIAL) {
216 update_serno(&data[1], 6);
222 void t433_transmit_bit(bool value)
230 PORTB &= ~(1 << PB0);
237 void t433_send(unsigned long code, unsigned int length)
242 for (i = length - 1; i >= 0; i--) {
243 if (code & (1L << i)) {
244 t433_transmit_bit(true);
246 t433_transmit_bit(false);
249 /* Send a sync bit */
252 PORTB &= ~(1 << PB0);
257 int __attribute__((noreturn)) main(void)
266 usbDeviceDisconnect();
276 /* Set the 433MHz transmitter bit to output mode */
280 sei(); /* We're ready to go; enable interrupts */