X-Git-Url: https://the.earth.li/gitweb/?a=blobdiff_plain;f=main.c;h=bcc4008fb06047bb719be722feeba1a856712947;hb=refs%2Fheads%2Fmain;hp=fff2ae8df7d32a5fc90c10a99a86c8d517e9c667;hpb=3c727aad6ec52918a14a6550349a2e2ca35d0645;p=temper-clone.git diff --git a/main.c b/main.c index fff2ae8..bcc4008 100644 --- a/main.c +++ b/main.c @@ -30,6 +30,9 @@ #include "usbdrv.h" #include "libs-device/osccal.h" +#include "timer.h" +#include "w1.h" + typedef struct { uint8_t modifier; uint8_t reserved; @@ -37,6 +40,10 @@ typedef struct { } keyboard_report_t; keyboard_report_t keyboard_report; +uint8_t temp_state = 0; +uint16_t last_temp = 0xFFFD; +#define TEMP_INTERVAL (10 * 1000) + uint8_t temp_report[8]; bool have_temp_int = false; @@ -190,6 +197,30 @@ PROGMEM const char usbDescriptorConfiguration[] = { USB_CFG_INTR_POLL_INTERVAL, /* in ms */ }; +inline char hexdigit(unsigned int i) +{ + return (i < 10) ? ('0' + i) : ('A' - 10 + i); +} + +/* Look for a 1-Wire device and use its ROMID to set the serial ID */ +void set_serial(void) +{ + uint8_t buf[8]; + uint8_t i; + + if (!w1_reset()) { + return; + } + + w1_write(0x33); /* READ ROM */ + w1_read(buf, 8); + + for (i = 0; i < 8; i++) { + serno_str[i * 2 + 1] = hexdigit(buf[i] >> 4); + serno_str[i * 2 + 2] = hexdigit(buf[i] & 0xF); + } +} + usbMsgLen_t usbFunctionDescriptor(usbRequest_t *rq) { if (rq->wValue.bytes[1] == USBDESCR_STRING && @@ -256,6 +287,7 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) usbMsgLen_t usbFunctionWrite(uint8_t * data, uchar len) { if (len == 1) { + PORTB |= 1 << PB1; // LED on if (data[0] != ledstate) { ledstate = data[0]; @@ -275,13 +307,12 @@ usbMsgLen_t usbFunctionWrite(uint8_t * data, uchar len) if (data[1] == 0x80 && data[2] == 0x33 && data[3] == 1) { /* Temperature query */ + memset(temp_report, 0, 8); have_temp_int = true; temp_report[0] = 0x80; temp_report[1] = 2; - /* Fake 15°C */ - temp_report[2] = 0xF; - temp_report[3] = 0x0; - + temp_report[2] = last_temp >> 8; + temp_report[3] = last_temp & 0xFF; } else if (data[1] == 0x82 && data[2] == 0x77 && data[3] == 1) { /* Initialisation Query #1 */ @@ -302,23 +333,35 @@ usbMsgLen_t usbFunctionWrite(uint8_t * data, uchar len) return 0; } +void hadUsbReset(void) +{ + /* Reset our state machine back to having nothing to send */ + temp_state = 0; + have_temp_int = false; +} + int main(void) { unsigned char i; + uint8_t buf[9]; + unsigned long last_temp_time = 0; wdt_enable(WDTO_1S); - usbInit(); - usbDeviceDisconnect(); + w1_setup(); + set_serial(); + timer_init(); + usbDeviceDisconnect(); i = 0; while (--i) { wdt_reset(); _delay_ms(1); } - usbDeviceConnect(); + usbInit(); + /* PB1 as output for LED */ DDRB |= 1 << PB1; @@ -342,5 +385,46 @@ int main(void) have_temp_int = false; } } + + if (temp_state == 1) { + if (w1_reset()) { + temp_state++; + } else { + temp_state = 0; + } + } else if (temp_state == 2) { + w1_write(0xCC); /* SKIP ROM */ + temp_state++; + } else if (temp_state == 3) { + w1_write(0x44); /* Convert T */ + temp_state++; + } else if (temp_state == 4) { + if (w1_read_bit()) + temp_state++; + } else if (temp_state == 5) { + if (w1_reset()) { + temp_state++; + } else { + temp_state = 0; + } + } else if (temp_state == 6) { + w1_write(0xCC); /* SKIP ROM */ + temp_state++; + } else if (temp_state == 7) { + w1_write(0xBE); /* Read Scratchpad */ + temp_state++; + } else if (temp_state > 7 && temp_state < 17) { + buf[temp_state - 8] = w1_read_byte(); + temp_state++; + } else if (temp_state == 17) { + last_temp = buf[1] << 12 | buf[0] << 4; + temp_state = 0; + last_temp_time = millis(); + } + + if (temp_state == 0 && + (millis() - last_temp_time) > TEMP_INTERVAL) { + temp_state = 1; + } } }