I wrote a little python script which checks the checksum of a Bios.
In a previous post i used hexedit to play around changing a Bios dump.
Below posted python script calculates the checkum using the following:
Add all bytes in the file and do a modulo 256 on this number. The output should be 0.
My previous edit gave me a output of C2, so I changed an unused byte FF into (FF-C2) 3D. No more checksum errors when booting!
Next to do, get a Bios like Glabios or PcXtBios to start my own code from a secondary Eeprom.
import sys
# Bios sum modulo should be 0
# edit last or unused byte to fix
# python bios-checksum-test.py MYROM.edit.checksum
# 0
f = open(sys.argv[1],'rb')
m = f.read()
print '%x' % ( ( sum(ord(c) for c in m) & 0xFFFFFFFF ) % 256 )
Python3
import sys
# Bios sum modulo should be 0
# edit last or unused byte to fix
# python bios-checksum-test.py MYROM.edit.checksum
# 0
f = open(sys.argv[1],'rb')
m = f.read()
checksum = ( ( sum((c) for c in m) & 0xFFFFFFFF ) % 256 )
print(checksum, 'in hex =', hex(checksum))
PlatformIO tested ( install StackArray.h in lib/StackArray/StackArray.h )
Reset button and dipswitches for below options
Visited pathmode ( dip switch 1)
Preview maze generation ( dip switch 3)
Hard mode – starts at first position when hitting a wall ( dip 2 )
Longest path mode (longest stackarray before stack.pop (dip 4 )
Prints serial info, like a drawn maze
Pixel remapping for other displays ( Below mapping i had to use, see source sketch )
How is the maze generated?
It is generated using recursive backtracking:
Start at 0,0 mark visited, select a random direction from unvisited neighbours, push current on the stack, goto new location, and mark this visited. Repeat until no possible directions. pop from the stack a previous location, another direction possible? Take that path and repeat. Repeat popping from stack, until stack empty .. now all cells are visited
Make an initial cell the current cell
mark it as visited
While there are unvisited cells
If the current cell has any neighbours
which have not been visited
Choose randomly one of the unvisited neighbours
Push the current cell to the stack
Mark wall hole
Make the chosen cell the current cell
mark it as visited
Else if stack is not empty
Pop a cell from the stack
Make it the current cell
This is my implementation of backtracking
The displaymatrix function is a implementation of different led mappings
Still have to decide where to place endpoint … At 8,8 or at first stack pop? Maybe both?
Code
#include <WEMOS_Matrix_LED.h>
#include <StackArray.h>
int directions[4]{};
int notalldone = 1;
int tmpx=0;
int tmpy=0;
int x = 1;
int y = 1;
MLED mled(5); //set intensity=5
int maze[8][8] = {
};
int displaymatrix[8][8] = {
{ 0,1,2,3,4,5,6,7 },
{ 8,9,10,11,12,13,14,15 },
{16,17,18,19,20,21,22,23},
{24,25,26,27,28,29,30,31},
{32,33,34,35,36,37,38,39},
{40,41,42,43,44,45,46,47},
{48,49,50,51,52,53,54,55},
{56,57,58,59,60,61,62,63}
};
int visitmatrix[10][10] = {
1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1
};
void setup() {
Serial.begin(115200);
randomSeed(analogRead(0));
mazegen();
drawmaze();
}
void mazegen(){
visitmatrix[x][y]=1;
StackArray <int> rowStack;
StackArray <int> colStack;
rowStack.push(x);
colStack.push(y);
while(notalldone == 1){
visitmatrix[x][y]=1;
while(!rowStack.isEmpty()) {
int count=0;
//up
if ( visitmatrix[x-1][y] == 0 ){
directions[count]=1;
count++;
}
//right
if ( visitmatrix[x][y+1] == 0 ){
directions[count]=2;
count++;
}
//down
if ( visitmatrix[x+1][y] == 0 ){
directions[count]=4;
count++;
}
//left
if ( visitmatrix[x][y-1] == 0 ){
directions[count]=8;
count++;
}
// no dir found
if (count == 0 ) {
mled.dot(x-1,y-1);
mled.display();
x = rowStack.pop();
y = colStack.pop();
Serial.println("popping ");
} else {
// count random direction
int dir = directions[random(count)];
Serial.println("push ");
rowStack.push(x);
colStack.push(y);
Serial.print("nr dir : ");
Serial.println(count);
//delay(100);
Serial.println(dir);
// move 1,1 to 0,0
mled.dot(x-1,y-1);
mled.display();
// set direction in maze, dit moet bit set worden
int mybits = maze[x-1][y-1];
int storedir = mybits | dir;
maze[x-1][y-1] = storedir;
if ( dir == 1){
int getup = maze[x-2][y-1];
int storedir = getup | 4;
maze[x-2][y-1] = storedir;
}
if ( dir == 2){
int getup = maze[x-1][y];
int storedir = getup | 8;
maze[x-1][y] = storedir;
}
if ( dir == 4){
int getup = maze[x][y-1];
int storedir = getup | 1;
maze[x][y-1] = storedir;
}
if ( dir == 8){
int getup = maze[x-1][y-2];
int storedir = getup | 2;
maze[x-1][y-2] = storedir;
}
// maze[x-1][y-1] = dir;
//set new square
if (dir == 1){ x--; }
if (dir == 2){ y++; }
if (dir == 4){ x++; }
if (dir == 8){ y--; }
visitmatrix[x][y]=1;
drawmaze();
}
}
notalldone = 0; //#2
// if found 0 in 10x10 matrix visited, do
for(int checkx=0;checkx<10;checkx++){
for(int checky=0;checky<10;checky++){
if ( visitmatrix[checkx][checky] == 0 ){
tmpx=x;
tmpy=y;
notalldone = 1;
}
}
}
}
rowStack.push(tmpx);
colStack.push(tmpy);
}
void drawmaze(){
Serial.println("Generating done - Drawing");
for(int ledx=0;ledx<8;ledx++)
{
for(int ledy=0;ledy<8;ledy++){
Serial.print(maze[ledx][ledy]);
if ( maze[ledx][ledy] != 0 ) {
mled.dot(ledx,ledy); // draw dot
mled.display();
// delay(50);
}
}
Serial.println("");
}
Serial.println("");
delay(100);
}
void loop() {
}
When you need a large digital joystick, but only got an analog one. You can use below code to make the joystick act as a digital one.
I’ve played with analog joysticks on digital pins also, it can be done. But it can be buggy, and needs extra code.
Note: The joystick pins are marked with 5V, but when you use a Arduino which can only read till 3.3V using its ADC (Analog Digital Convertors), you can get some weird readings. When moving down and left is reads okay, but up and right react as being connected together! Just try it with 3.3V or use a resistor.
Above shows a ESP32, but below code has Arduino Nano pin names, change accordingly.
CODE
The code gives you a direction only once, you will need to move the stick to the middle position first and then move again.
Below gave me readings between 0 and 1024 (10 bits) Hence the between 350 and 650 for the middle position.
Most will give you a reading between 0 and 4096.
Want to set the resolution yourself?
analogReadResolution(10); // 10 bits
int val1 =0;
int val2 =0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
if (sensorValue1 > 650){
if (val1 == 0){
Serial.print("DOWN");
Serial.println(" ");
val1=1;
}
}
else if (sensorValue1 < 350){
if (val1 == 0){
Serial.print("UP");
Serial.println(" ");
val1=1;
}
}
else if (sensorValue2 > 350 && sensorValue2 < 650){
val1=0;
}
if (sensorValue2 > 650){
if (val2 == 0){
Serial.print("LEFT");
Serial.println(" ");
val2=1;
}
}
else if (sensorValue2 < 350){
if (val2 == 0){
Serial.print("RIGHT");
Serial.println(" ");
val2=1;
}
}
else if (sensorValue2 > 350 && sensorValue2 < 650){
val2=0;
}
delay(100);
}
elcome to Crack the Code!
The code has 4 digits. Each digit is between 0 and 9.
You have 10 tries to guess the code.
After each guess, you will be told how many digits you got correct and how many were misplaced.
Good luck!
Enter your guess: 1234
Guess 1: 1234 - 0 correct, 0 misplaced
Enter your guess: 5678
Guess 2: 5678 - 2 correct, 1 misplaced
Enter your guess: 5689
Guess 3: 5689 - 1 correct, 2 misplaced
Enter your guess: 9688
Guess 4: 9688 - 1 correct, 2 misplaced
....
Sorry, you did not crack the code. The code was 5568.
Generated python code
import random
def generate_code():
code = ""
for i in range(4):
code += str(random.randint(0, 9))
return code
def check_guess(code, guess):
correct_digits = 0
misplaced_digits = 0
for i in range(4):
if guess[i] == code[i]:
correct_digits += 1
elif guess[i] in code:
misplaced_digits += 1
return (correct_digits, misplaced_digits)
def play_game():
print("Welcome to Crack the Code!")
print("The code has 4 digits. Each digit is between 0 and 9.")
print("You have 10 tries to guess the code.")
print("After each guess, you will be told how many digits you got correct and how many were misplaced.")
print("Good luck!")
code = generate_code()
num_guesses = 0
while num_guesses < 10:
guess = input("Enter your guess: ")
if len(guess) != 4 or not guess.isdigit():
print("Invalid input. Please enter a 4-digit number.")
continue
num_guesses += 1
correct, misplaced = check_guess(code, guess)
print(f"Guess {num_guesses}: {guess} - {correct} correct, {misplaced} misplaced")
if correct == 4:
print("Congratulations! You cracked the code!")
return
print(f"Sorry, you did not crack the code. The code was {code}.")
play_game()
After this i let the AI made some changes, which kinda worked. Scary nevertheless
These are last weeks findings, I will add to this page when I discover other useful things.
Platformio
always include “Arduino.h”
Order of functions matter! (Not with Arduino IDE)
setup serial monitor speed in platformio.ini monitor_speed = 115200
Arduino IDE
Build error “panic: runtime error: index out of range [3] with length 3” or length 4. Code probably correct, build with another board and build+upload with correct board as workaround.
Generic
Using a SSD1306 with other pins? For example with Adafruit_SSD1306.h in setup(){ place Wire.begin(5,4);
"If something is worth doing, it's worth overdoing."