2022-10-31 15:11:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <libserialport.h>
|
2025-01-02 15:57:13 -05:00
|
|
|
#include "../src/arduino/shiftlights/shiftlights.h"
|
2022-10-31 15:11:02 +00:00
|
|
|
|
|
|
|
|
/* Helper function for error handling. */
|
|
|
|
|
int check(enum sp_return result);
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
char* port_name = "/dev/ttyACM0";
|
|
|
|
|
|
|
|
|
|
/* The ports we will use. */
|
|
|
|
|
struct sp_port* port;
|
|
|
|
|
|
|
|
|
|
/* Open and configure each port. */
|
|
|
|
|
printf("Looking for port %s.\n", port_name);
|
|
|
|
|
check(sp_get_port_by_name(port_name, &port));
|
|
|
|
|
|
|
|
|
|
printf("Opening port.\n");
|
|
|
|
|
check(sp_open(port, SP_MODE_READ_WRITE));
|
|
|
|
|
|
2025-01-02 15:57:13 -05:00
|
|
|
printf("Setting port to 115200 8N1, no flow control.\n");
|
|
|
|
|
check(sp_set_baudrate(port, 115200));
|
2022-10-31 15:11:02 +00:00
|
|
|
check(sp_set_bits(port, 8));
|
|
|
|
|
check(sp_set_parity(port, SP_PARITY_NONE));
|
|
|
|
|
check(sp_set_stopbits(port, 1));
|
|
|
|
|
check(sp_set_flowcontrol(port, SP_FLOWCONTROL_NONE));
|
|
|
|
|
|
2025-01-02 15:57:13 -05:00
|
|
|
ShiftLightsData sd;
|
|
|
|
|
|
2025-02-12 15:03:11 -05:00
|
|
|
int numlights = 7;
|
|
|
|
|
size_t bufsize = (numlights * 3) + 9;
|
|
|
|
|
char bytes[bufsize];
|
|
|
|
|
|
|
|
|
|
bytes[0] = 0xff;
|
|
|
|
|
bytes[1] = 0xff;
|
|
|
|
|
bytes[2] = 0xff;
|
|
|
|
|
bytes[3] = 0xff;
|
|
|
|
|
bytes[4] = 0xff;
|
|
|
|
|
bytes[5] = 0xff;
|
|
|
|
|
bytes[bufsize-1] = 0xfd;
|
|
|
|
|
bytes[bufsize-2] = 0xfe;
|
|
|
|
|
bytes[bufsize-3] = 0xff;
|
2025-01-02 15:57:13 -05:00
|
|
|
|
|
|
|
|
int result;
|
|
|
|
|
unsigned int timeout = 2000;
|
2025-02-12 15:03:11 -05:00
|
|
|
for( int i = 0; i < 7; i++)
|
2022-10-31 15:11:02 +00:00
|
|
|
{
|
2025-02-12 15:03:11 -05:00
|
|
|
bytes[(i * 3) + 6 + 1] = 0xff;
|
2022-10-31 15:11:02 +00:00
|
|
|
|
|
|
|
|
/* Send data. */
|
2025-02-12 15:03:11 -05:00
|
|
|
result = check(sp_blocking_write(port, &bytes, bufsize, timeout));
|
2022-10-31 15:11:02 +00:00
|
|
|
|
|
|
|
|
/* Check whether we sent all of the data. */
|
2025-02-12 15:03:11 -05:00
|
|
|
if (result == bufsize)
|
2022-10-31 15:11:02 +00:00
|
|
|
{
|
2025-02-12 15:03:11 -05:00
|
|
|
printf("Sent %d bytes successfully, %i lit leds.\n", bufsize, numlights);
|
2022-10-31 15:11:02 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-02-12 15:03:11 -05:00
|
|
|
printf("Timed out, %d/%d bytes sent.\n", result, bufsize);
|
2022-10-31 15:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-01-02 15:57:13 -05:00
|
|
|
sleep(2);
|
2022-10-31 15:11:02 +00:00
|
|
|
}
|
2025-01-02 15:57:13 -05:00
|
|
|
sd.litleds = 0;
|
2025-02-12 15:03:11 -05:00
|
|
|
result = check(sp_blocking_write(port, &bytes, bufsize, timeout));
|
2022-10-31 15:11:02 +00:00
|
|
|
|
|
|
|
|
check(sp_close(port));
|
|
|
|
|
sp_free_port(port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Helper function for error handling. */
|
|
|
|
|
int check(enum sp_return result)
|
|
|
|
|
{
|
|
|
|
|
/* For this example we'll just exit on any error by calling abort(). */
|
|
|
|
|
char* error_message;
|
|
|
|
|
|
|
|
|
|
switch (result)
|
|
|
|
|
{
|
|
|
|
|
case SP_ERR_ARG:
|
|
|
|
|
printf("Error: Invalid argument.\n");
|
|
|
|
|
abort();
|
|
|
|
|
case SP_ERR_FAIL:
|
|
|
|
|
error_message = sp_last_error_message();
|
|
|
|
|
printf("Error: Failed: %s\n", error_message);
|
|
|
|
|
sp_free_error_message(error_message);
|
|
|
|
|
abort();
|
|
|
|
|
case SP_ERR_SUPP:
|
|
|
|
|
printf("Error: Not supported.\n");
|
|
|
|
|
abort();
|
|
|
|
|
case SP_ERR_MEM:
|
|
|
|
|
printf("Error: Couldn't allocate memory.\n");
|
|
|
|
|
abort();
|
|
|
|
|
case SP_OK:
|
|
|
|
|
default:
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|