uchar usbFunctionWrite(uchar *data, uchar len) { memcpy(&msg[pos], data, len); pos+=len; if (pos != rq_len) return 0; /* Need moar data */ uint8_t ret = 0; uint8_t retries = 15; switch (last_rq) { case RQ_SET_REMOTE_ADDR: memcpy(remote_addr, msg, 5); break; case RQ_SET_LOCAL_ADDR: memcpy(local_addr, msg, 5); break; case RQ_WRITE: do { rf24_power_up(g_radio); rf24_open_writing_pipe(g_radio, remote_addr); ret = rf24_write(g_radio, msg, pos); PORTC ^= 1<<2; delay_ms(5); } while( ret && retries--); break; } strcpy(msg, ((ret==0) && retries) ? "OK" : "FAIL"); return 1; }
uchar usbFunctionSetup(uchar data[8]) { usbRequest_t *rq = (void *)data; switch (rq->bRequest) { case RQ_SET_CONFIG: rf24_init(g_radio); rf24_enable_dynamic_payloads(g_radio); rf24_set_channel(g_radio, rq->wValue.bytes[0]); rf24_set_data_rate(g_radio, rq->wValue.bytes[1]); rf24_set_pa_level(g_radio, rq->wIndex.bytes[0]); break; case RQ_OPEN_PIPES: break; case RQ_LISTEN: if (rq->wValue.bytes[0]) { rf24_open_reading_pipe(g_radio, 1, local_addr); rf24_start_listening(g_radio); } else { rf24_stop_listening(g_radio); rf24_open_writing_pipe(g_radio, remote_addr); } break; case RQ_READ: { uint8_t pipe; uint8_t retries=50; uint8_t len =0; do { if (rf24_available(g_radio, &pipe)) { PORTC ^= 1<<2; len = rf24_get_dynamic_payload_size(g_radio); have_moar = !rf24_read(g_radio, msg, len); usbMsgPtr = msg; return len; } delay_ms(10); } while (retries--); } return 0; break; case RQ_NOP: /* Shit out dbg buffer */ usbMsgPtr = msg; return strlen(msg)+1; break; } last_rq = rq->bRequest; rq_len = rq->wLength.word; pos = 0; return USB_NO_MSG; }
int writeMessage(void* message, uint8_t address[5], uint16_t length) { transmitLedOn(); _delay_ms(250); rf24_stop_listening(&r); rf24_open_writing_pipe(&r, address); int writeReturn = rf24_write(&r, &message, length*sizeof(uint8_t)); _delay_ms(250); rf24_start_listening(&r); transmitLedOff(); return writeReturn }
int sendConfigRequest(uint8_t address[5]) { message_type_t type = REQUESTCONFIG; ConfirmAddress msg; msg.messageId = type; msg.length = 3; PORTD ^= (1<<4); _delay_ms(250); rf24_stop_listening(&r); rf24_open_writing_pipe(&r, address); int writeReturn = rf24_write(&r, &msg, msg.length*sizeof(uint8_t)); _delay_ms(250); rf24_start_listening(&r); PORTD ^= (1<<4); printk("sent config request message\n"); return writeReturn; }
uint8_t sendConfigResponse(uint8_t address[5]) { message_type_t type = RESPONDCONFIG; RespondConfig msg; msg.messageId = type; msg.length = 8; msg.numberOfSensors = 3; msg.sensorTypes = sensorIds; PORTD ^= (1<<4); _delay_ms(250); rf24_stop_listening(&r); rf24_open_writing_pipe(&r, address); int writeReturn = rf24_write(&r, &msg, msg.length*sizeof(uint8_t)); _delay_ms(250); rf24_start_listening(&r); PORTD ^= (1<<4); printk("sent config message\n"); return writeReturn; }