Compare commits

..

No commits in common. "74526fa07da473d4bbc0b1befc4c9d206e1f8ce1" and "3237a9bf6a6d634442089d00d800512a19791104" have entirely different histories.

2 changed files with 20 additions and 62 deletions

View File

@ -1,6 +1,5 @@
import serial
import time
import colorsys
channels = [
192, # pan
@ -19,52 +18,18 @@ start_addr = 10
with serial.Serial("/dev/ttyUSB0", 115200) as ser:
payload = bytearray(512)
FPS = 20
FRAME_TIME = 1 / FPS
t = 0
def sync():
# wait for sync
while True:
b = ser.readline()
if b.strip() == b"Sync.":
return
sync()
print("initial sync.")
while True:
loop_start = time.time()
r, g, b = colorsys.hls_to_rgb(t, 0.5, 1)
channels[3] = int(255 * r)
channels[4] = int(255 * g)
channels[5] = int(255 * b)
# payload.extend(channels)
payload[(start_addr - 1) : (start_addr - 1 + len(channels))] = channels
print(payload)
while True:
ser.write(payload)
ser.flush()
response = ser.readline()
if response.strip() != b"Ack.":
print(f"received bad response: {response!r}")
sync()
continue
time.sleep(1/50)
t += FRAME_TIME
t %= 1
print(".")
loop_time = time.time() - loop_start
if loop_time < FRAME_TIME:
time.sleep(FRAME_TIME - loop_time)
else:
print("loop took too long!")
print(f"loop time: {1000 * loop_time:0.2f}ms busy, {1000 * (time.time() - loop_start):0.2f}ms total")
# print(ser.read_all())
#print(ser.read_all())

View File

@ -1,7 +1,4 @@
unsigned long tic_loop = 0;
unsigned long time_since_last_sync;
const unsigned long SYNC_TIMEOUT = 1000;
const unsigned int FRAME_TIME = 20; // 20 ms -> 50 FPS
const size_t UNIVERSE_SIZE = 512;
@ -18,8 +15,7 @@ void setup()
// spin until serial is up
}
Serial.println();
Serial.println("Sync.");
time_since_last_sync = millis();
Serial.println("Init.");
Serial1.begin(250000, SERIAL_8N2); // DMX
}
@ -42,30 +38,19 @@ void send_packet()
bool update_buffer()
{
unsigned long now = millis();
size_t n = Serial.available();
if (!n)
{
// nothing available to read
if (now - time_since_last_sync > SYNC_TIMEOUT)
{
// re-sync
bytes_read = 0;
Serial.println("Sync.");
time_since_last_sync = now;
}
return false;
}
time_since_last_sync = now;
int bytes_received = Serial.read(channels_buffer + bytes_read, std::min(n, UNIVERSE_SIZE - bytes_read));
bytes_read += bytes_received;
// Serial.printf("rx n=%d rec=%d total=%d\n", n, bytes_received, bytes_read);
if (bytes_read == UNIVERSE_SIZE)
{
bytes_read = 0;
Serial.println("Ack.");
// Serial.println();
// Serial.println("Updated.");
return true;
}
else
@ -78,10 +63,18 @@ void send_dmx_header()
{
Serial1.flush();
Serial1.begin(90000, SERIAL_8N2);
/*
while (Serial1.available())
Serial1.read();
*/
// send the break as a "slow" byte
Serial1.write(0);
// switch back to the original baud rate
Serial1.flush();
Serial1.begin(250000, SERIAL_8N2);
/*
while (Serial1.available())
Serial1.read();
*/
Serial1.write(0); // Start-Byte
}