Last active
January 15, 2025 12:07
-
-
Save ProfAndreaPollini/706ee01791e6e0d454d3bdb16687c31a to your computer and use it in GitHub Desktop.
arduino serial to raylib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
auto x = random(1,100); | |
Serial.println(x); | |
delay(1000); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing as mp | |
import serial as sr | |
import raylib as rb | |
import random as rd | |
import time | |
data = 0 | |
def serial_task(q: mp.Queue): | |
while True: | |
time.sleep(1) | |
n = rd.randint(1,100) | |
q.put(n) | |
if __name__ == "__main__": | |
mp.freeze_support() | |
q = mp.Queue() # creo la coda | |
p = mp.Process(target=serial_task,args=(q,)) | |
p.start() | |
while True: | |
if not q.empty(): | |
data = q.get() | |
print(data,end=" ") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing as mp | |
import serial as sr | |
import random as rd | |
import time | |
data = 0 | |
def serial_task(q: mp.Queue): | |
arduino_serial = sr.Serial("COM6",9600) | |
while True: | |
from_serial = arduino_serial.readline() | |
from_serial = from_serial.decode("utf-8") | |
n = float(from_serial) | |
q.put(n) | |
from pyray import * | |
def app(): | |
global data | |
init_window(800, 450, "Hello") | |
while not window_should_close(): | |
if not q.empty(): | |
data = q.get() | |
begin_drawing() | |
clear_background(WHITE) | |
draw_text(f"data = {data}", 190, 200, 20, VIOLET) | |
end_drawing() | |
close_window() | |
if __name__ == "__main__": | |
mp.freeze_support() | |
q = mp.Queue() # creo la coda | |
p = mp.Process(target=serial_task,args=(q,)) | |
p.start() | |
app() | |
# while True: | |
# if not q.empty(): | |
# data = q.get() | |
# print(data,end=" ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment