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
37 /* Energenie 24 bit address , last 4 all 0 for code */
38 #define ENER_ADDR 0x123450
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 void fetch_serno(void)
61 eeprom_read_block(serno, 0, 6);
62 if (serno[0] == 0xff) {
63 /* If the EEPROM is blank, return a default serial # */
75 void update_serno(uchar *buf, uchar len)
80 * I have no idea why this gets stored 3 times, but the original
83 eeprom_write_block(buf, (void *) 0x00, len);
84 eeprom_write_block(buf, (void *) 0x40, len);
85 eeprom_write_block(buf, (void *) 0x80, len);
87 for (i = 0; i < 6; i++) {
92 usbMsgLen_t usbFunctionSetup(uchar data[8])
94 usbRequest_t *rq = (void *) data;
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)) {
106 uchar usbFunctionRead(uchar *data, uchar len)
112 for (i = 0; i < 6; i++) {
115 data[6] = data[7] = 0;
116 if (PORTB & (1 << PB0)) {
125 uchar usbFunctionWrite(uchar *data, uchar len)
127 if (data[0] == CMD_ALL_ON) {
128 cmd = ENER_ADDR | 0xd;
131 } else if (data[0] == CMD_ALL_OFF) {
132 cmd = ENER_ADDR | 0xc;
135 } else if (data[0] == CMD_ON) {
139 cmd = ENER_ADDR | 0xf;
143 cmd = ENER_ADDR | 0x7;
147 cmd = ENER_ADDR | 0xb;
151 cmd = ENER_ADDR | 0x3;
157 } else if (data[0] == CMD_OFF) {
161 cmd = ENER_ADDR | 0xe;
165 cmd = ENER_ADDR | 0x6;
169 cmd = ENER_ADDR | 0xa;
173 cmd = ENER_ADDR | 0x2;
179 } else if (data[0] == CMD_SET_SERIAL) {
180 update_serno(&data[1], 6);
186 void t433_transmit_bit(bool value)
194 PORTB &= ~(1 << PB0);
201 void t433_send(unsigned long code, unsigned int length)
206 for (i = length - 1; i >= 0; i--) {
207 if (code & (1L << i)) {
208 t433_transmit_bit(true);
210 t433_transmit_bit(false);
213 /* Send a sync bit */
216 PORTB &= ~(1 << PB0);
221 int __attribute__((noreturn)) main(void)
228 usbDeviceDisconnect();
238 /* Set the 433MHz transmitter bit to output mode */
242 sei(); /* We're ready to go; enable interrupts */