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 2
Yoo @Cunning07, this project of yours seems very cool! Just recently i’ve been getting into this tech stuff, and most of the things i already built are very simple. Do you have any tips on how to get started on coding and stuff?
@Cacique First, I want to thank you for your kind words.
Second, while it may sound like simple advice, the best way to get better at coding is to actually code. I found that the fastest way to grow was by challenging myself with difficult projects. Whenever I got stuck, I searched the web for documentation, examples, or projects tackling similar problems. And if I couldn’t find anything useful, I’d use AI tools like Chat to point me in the right direction.
Here are a few resources that I think provide a great starting point:
• https://github.com/codecrafters-io/build-your-own-x — A collection of tutorials that walk you through building real projects from scratch.
• https://www.boot.dev — A fun, gamified way to learn backend development and programming fundamentals.
• https://leetcode.com — Great for improving problem-solving skills and learning common algorithms and data structures.
If you ever have questions, need help with a project, or just want to bounce around ideas, feel free to reach out on Slack. I’ll do my best to help however I can.
Slack: ic499614
Best of luck on your coding journey
Sign in to join the conversation.