Its been a long time
Sorry for my absence i have been stuck in the hell that is queues.
In this devlog i will cover how to communicate through serial
I am by no means a good software dev but after many long nights of banging my head against the wall i think i have somewhat of a decent system and understanding of this topic
Speaking Serial
In my last devlog i mentioned the idea of running complex programs on toaster level components by using a wire and a computer, this wire is communicating through a serial bus, which is simply a pathway that communicates information or bits through a single channel, emphasize on the idea of the single channel.
So first we have to establish the serial and how its gonna operate. During this entire log all info will be in c++ and python.
Arduino Section(c++)
#include <Arduino.h>
#include <SPI.h>
void setup() {
Serial.begin(115200);
}
Pc Side(python)
from serial import Serial
import time
def connect(port):
try:
serial = Serial(port.device, baudrate=115200, timeout=1)
time.sleep(3)
So some things to note, set the baud rate for both connections the exact same. Baudrate is essentially the speed of transfer a higher baudrate means faster communication, which in turn needs a stronger wire/cpu to process more info faster. in addition ensure on the pc side after opening the serial connection to add a delay you need to let the micro controller reset and settle its connection.
Communication
From here you need a way to actually communicate, say you have a simple keyboard and its connected to your computer and a user presses a button which is gonna open steam, unfortunately its not smart to just send a serial message to open steam as each character/Letter depending on the encoding can contain multiple bits which each need to be streamed through the serial and received by the pc. So its best to setup a pallet of commands both sides know what each command is and the command is simply just a single bit.
Commands
Arduino
enum Command : uint8_t {
CMD_HANDSHAKE = 0x00,
CMD_HANDSHAKE_ACK = 0x01,
CMD_HEARTBEAT = 0x02,
CMD_DRAW_IMAGE = 0x03
};
To differentiate commands:
void resolveCMD(uint8_t cmd, uint8_t display) {
switch (cmd) {
case CMD_DRAW_IMAGE:
#code
case CMD_HANDSHAKE:
#code
}
}
Commands can be expanded up to 256 different types, with each one still taking one byte. Now in this scope the computer or arduino send the command through the serial and the other side just does it, heavy lifting stuff is typically done on the pc for example loading and storing the images i want to display (here’s the snippet for that):
from PIL import Image
img = Image.open(base_dir / folder / img_filename)
width, height = img.size
pixels - img.load()
while True:
#check for arduino ready state
packet = bytearray()
#append display, width, height to packet
self.manager.write(packet)
buffer = bytearray()
for y in range(height):
for x in (width):
r,g,b = pixels[x,y]
rgb565 = (((r & 0xF8)<<8|((g & 0xFC) <<3\(b >> 3))
buffer.extend(rgb565.to_bytes(2, "big")
#when buffer is above 512 len send to arduino
Unfortunately i am close to the character limit and i hate reading long stuff so i dont want to go on a yapathon. If this is the type of devlogs people like my next one will go heavy in to depth on running a good queue, which plays in turn with the serial, making it more efficient and allowing a more complex system. Check out my git hub (https://github.com/Cunning077/HackPad) as well, all the code for this project is there im terrible at documentation but if it can provide a example or help or if you want to help me i would be very grateful.
Enjoy Summer
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.