Skip to content

Commit

Permalink
Set relay state from USB hid host input
Browse files Browse the repository at this point in the history
  • Loading branch information
thmahe committed Nov 9, 2022
1 parent 730316c commit 8bb571b
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

/* Change RELAY_PORT and RELAY_BIT according to your targeted hardware */
/* Given example below, first relay is mapped on pin PC0 (atmega8) */
uint8_t *OUPTUT_REG[RELAY_COUNT] = {&DDRC, &DDRC, &DDRC, &DDRC};
uint8_t *RELAY_PORT[RELAY_COUNT] = {&PORTC, &PORTC, &PORTC, &PORTC};
int RELAY_BIT[RELAY_COUNT] = {0, 1, 2, 3};

Expand Down Expand Up @@ -157,7 +158,27 @@ uchar usbFunctionRead(uchar *data, uchar len) {

/* Called when the host sends a chunk of data to the device.*/
uchar usbFunctionWrite(uchar *data, uchar len) {
if (data[0] == CMD_SET_SERIAL) {
/* data[0] : command code received from host
* data[1] : relay number to operate or new serial number
* When data[1] > relay number, command is applied to all relays
*/
if (data[0] == CMD_ON) {
if (data[1] > RELAY_COUNT) {
for (int j = 0; j < RELAY_COUNT; j++) {
*RELAY_PORT[j] |= _BV(RELAY_BIT[j]);
}
return len;
}
*RELAY_PORT[data[1] - 1] |= _BV(RELAY_BIT[data[1] - 1]);
} else if (data[0] == CMD_OFF) {
if (data[1] > RELAY_COUNT) {
for (int j = 0; j < RELAY_COUNT; j++) {
*RELAY_PORT[j] &= ~_BV(RELAY_BIT[j]);
}
return len;
}
*RELAY_PORT[data[1] - 1] &= ~_BV(RELAY_BIT[data[1] - 1]);
} else if (data[0] == CMD_SET_SERIAL) {
update_serial_number(SERIAL_NUMBER, &data[1], SERIAL_NUMBER_LEN);
}
return len;
Expand All @@ -181,6 +202,11 @@ int main(void) {
}
usbDeviceConnect();

// set relay bit as output
for (uint8_t j = 0; j < RELAY_COUNT; j++) {
*OUPTUT_REG[j] |= _BV(RELAY_BIT[j]);
}

// enable interrupts
sei();

Expand Down

0 comments on commit 8bb571b

Please sign in to comment.