static int check_packet_count(int fd) { struct lux_packet response; struct lux_packet packet2 = { .destination = ADDRESS, .command = LUX_CMD_GET_PKTCNT, .index = 0, .payload_length = 0, }; int rc = lux_command(fd, &packet2, &response, LUX_RETRY); if (rc < 0) { PERROR("Packet check count failed"); return -1; } printf("Received packet for %#08x; cmd=%#02x; idx=%d; plen=%d; data=", response.destination, response.command, response.index, response.payload_length); uint32_t *x = (uint32_t *) response.payload; /* uint32_t good_packet; uint32_t malformed_packet; uint32_t packet_overrun; uint32_t bad_checksum; uint32_t rx_interrupted; */ printf("good:%d malfm:%d ovrun:%d badcrc:%d rxint:%d xaddr:%d\n", x[0], x[1], x[2], x[3], x[4], x[5]); return 0; } static int blink_led(int fd, uint32_t addr, int count) { struct lux_packet packet = { .destination = addr, .command = LUX_CMD_SET_LED, .index = 0, .payload_length = 1, }; struct lux_packet response; while (count--) { packet.payload[0] = 1; int rc = lux_command(fd, &packet, &response, LUX_ACK | LUX_RETRY); if (rc < 0) PERROR("Unable to write LED on msg"); usleep(100000); packet.payload[0] = 0; rc = lux_command(fd, &packet, &response, LUX_ACK | LUX_RETRY); if (rc < 0) PERROR("Unable to write LED off msg"); usleep(100000); } return 0; } int main(void) { //int fd = lux_uri_open("serial:///dev/ttyACM0"); int fd = lux_uri_open("udp://127.0.0.1:1365"); check_packet_count(fd); blink_led(fd, 0xFFFFFFFF, 10); return 0; }
static int lux_length(uint32_t lux_id) { struct lux_packet packet = { .destination = lux_id, .command = CMD_GET_LENGTH, .index = 0, .payload_length = 0, }; struct lux_packet response; int rc = lux_command(lux_fd, &packet, 1, &response); if (rc < 0 || response.payload_length != 2) { printf("No/invalid response to length query on %#X\n", lux_id); return -1; } uint16_t length; memcpy(&length, response.payload, sizeof length); return length; } static int lux_frame(uint32_t lux_id, unsigned char * data, size_t data_size) { struct lux_packet packet = { .destination = lux_id, .command = CMD_FRAME, .index = 0, .payload_length = data_size, }; memcpy(packet.payload, data, data_size); return lux_write(lux_fd, &packet); } int output_lux_init() { if (lux_fd >= 0) { // Already connected return -1; } lux_fd = lux_network_open(config.lux.address, config.lux.port); if (lux_fd < 0) { printf("ERROR connecting to socket\n"); return -1; } printf("Lux initialized\n"); return 0; } void output_lux_del() { if (lux_fd >= 0) { lux_close(lux_fd); } } int output_lux_enumerate(output_strip_t * strips, int n_strips) { int found = 0; for (int i = 0; i < n_strips; i++) { int length = lux_length(strips[i].id_int); if (length < 0) { strips[n_strips].bus &= ~OUTPUT_LUX; printf("Lux UDP device '%#X' not found\n", strips[i].id_int); continue; } strips[i].bus |= OUTPUT_LUX; strips[i].length = length; found++; printf("Lux UDP device '%#X' with length '%i'\n", strips[i].id_int, length); } return found; } int output_lux_push(output_strip_t * strip, unsigned char * frame, int length) { lux_frame(strip->id_int, frame, length); return 0; }