Skip to content

Instantly share code, notes, and snippets.

@Lukelectro
Last active April 1, 2024 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lukelectro/6d687f4f74dca086226c00c0d87e2848 to your computer and use it in GitHub Desktop.
Save Lukelectro/6d687f4f74dca086226c00c0d87e2848 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
lokale tijd omzetten in hh:mm:ss en dat als pulsbreedte encoded op het scherm laten flitsen
om zo een horloge met lichtsensor gelijk te zetten / experiment met optische data-overdracht
Stuurt voor neon-horloge uur, minuut, seconde en CRC-8
Stuurt voor LED-horloge Uur, Minuut, Seconde, weekdag (0-6), dag van maand, maand,
jaar(eerste 2 cijfers, hondertallen), jaar (2e paar cijfers, tien en eentallen), CRC-8
Om te kunnen compenseren voor de duur van het versturen, is de tijd per bit vast en wordt dit bij de verstuurde tijd opgeteld
Uitgaande van een monitor die met 60 Hz ververst wordt is de minimale tijd dat het venstertje wit of zwart kan zijn, 16,67 ms. Afgerond 20 (50Hz)
zo lang er niets verstuurd wordt, is het venstertje wit
om een 1 te sturen, is het venstertje 40 ms zwart, dan 80 ms wit
om een 0 te sturen, is het venstertje 80 ms zwart, dan 40 ms wit
een bit duurt dus altijd 120 ms om te versturen, een byte dus 960 ms
Er worden voor het led-horloge 9 bytes verstuurd, dit duurt dus 8,64 seconden.
Voor het neonhorloge: 4 bytes, 3,84 seconde.
"""
"""
to convert to executable:
>>> import PyInstaller.__main__
>>> PyInstaller.__main__.run(['time_pwm_fixedbittime.py', '--onefile', '--windowed'])
"""
from time import localtime, time
import tkinter as tk
import crc8 #https://pypi.org/project/crc8/ version 0.2.0
#tijd uitsturen door achtergrondkleur van windowtje te laten knipperen
def blink(thesebits):
blinked_data = " "
for i in range (8): #0...7
if(thesebits & (1 << i) ):
label.config(bg='black');
blinked_data+="1"
window.after(40, window.update())
label.config(bg='white');
window.after(80, window.update())
else:
label.config(bg='black');
blinked_data+="0"
window.after(80, window.update())
label.config(bg='white');
window.after(40, window.update())
print(blinked_data, thesebits)
now = localtime()
window = tk.Tk()
window.columnconfigure(0, minsize=200)
window.rowconfigure(2, minsize=200)
button_LED = tk.Button(
master=window,
text="Blink for LED watch"
)
button_NEON = tk.Button(
master=window,
text="Blink for neon watch"
)
label = tk.Label(
master=window,
text=f"{now.tm_hour:02d}:{now.tm_min:02d}:{now.tm_sec:02d}",
bg="white",
fg="black",
)
button_LED.grid(row=0, column=0, sticky="nsew")
button_NEON.grid(row=1, column=0, sticky="nsew")
label.grid(row=2, column=0, sticky="nsew")
def handle_click_LED(event):
print("debug display is big endian MSB first hour,min,sec, 0-6 mon-sun, day, month, year, CRC8. Transmission is little endian!")
#bij klikken huidige tijd opnieuw lezen en laten uitsturen
now = localtime(time()+8.64)
label.config(text=(f"{now.tm_hour:02d}:{now.tm_min:02d}:{now.tm_sec:02d}"))
#conversion - there must be a better way?
blinkthis = [now.tm_hour.to_bytes(1,"big"), now.tm_min.to_bytes(1,"big"), now.tm_sec.to_bytes(1,"big"),
now.tm_wday.to_bytes(1,"big"), now.tm_mday.to_bytes(1,"big"), now.tm_mon.to_bytes(1,"big"),
int(now.tm_year/100).to_bytes(1,"big"),int(now.tm_year%100).to_bytes(1,"big")]
#hour,min,sec, 0-6 mon-sun, day, month, year. Year split in 2 bytes in this clumsy way because it is easy-er down the line
#(and frankly I just don't know the magic 'line noise' python needs to format it justs right the pythonic way - let me know if you do!)
crc = crc8.crc8()
for i in range(len(blinkthis)):
crc.update(blinkthis[i])
for data in blinkthis:
blink(int.from_bytes(data,"big"))
blink(int.from_bytes(crc.digest(),"big")) #crc8 works with bytes, blink with ints. There's probably a nicer way to do this, let me know!
window.after(400, window.update()) #wait 400 ms
label.config(bg="white");
def handle_click_NEON(event):
print("debug display is big endian MSB first hour,min,sec, CRC8. Transmission is little endian!")
#bij klikken huidige tijd opnieuw lezen en laten uitsturen
now = localtime(time()+3.84)
label.config(text=(f"{now.tm_hour:02d}:{now.tm_min:02d}:{now.tm_sec:02d}"))
#conversion - there must be a better way?
blinkthis = [now.tm_hour.to_bytes(1,"big"), now.tm_min.to_bytes(1,"big"), now.tm_sec.to_bytes(1,"big")]
#or to test hour change:
#blinkthis = [now.tm_hour.to_bytes(1,"big"), int(58).to_bytes(1,"big"), now.tm_sec.to_bytes(1,"big")]
crc = crc8.crc8()
for i in range(len(blinkthis)):
crc.update(blinkthis[i])
for data in blinkthis:
blink(int.from_bytes(data,"big"))
blink(int.from_bytes(crc.digest(),"big")) #crc8 works with bytes, blink with ints. There's probably a nicer way to do this, I'm clumsy at python
window.after(400, window.update()) #wait 400 ms
label.config(bg="white");
button_LED.bind("<Button-1>", handle_click_LED)
button_NEON.bind("<Button-1>", handle_click_NEON)
window.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment