# print("Hello, ESP32!")
import framebuf
from math import *
buffer = bytearray((128*64)//8) # 128x64 pixels, 1 bit per pixel
def bytearray_to_binary_visual_list(byte_arr):
# Convert each byte in the bytearray to a binary string with 8 bits, keeping the '0b' prefix
binary_list = []
for byte in byte_arr:
# Use bitwise operations to manually convert to 8-bit binary string
binary_string = '0b' + ''.join('1' if byte & (1 << (7 - bit)) else '0' for bit in range(8))
binary_list.append(binary_string)
return binary_list
# print(bytearray_to_binary_visual_list(buffer))
fb = framebuf.FrameBuffer(buffer, 128, 64, framebuf.MONO_VLSB)
# fb.pixel(0,0,1)
# print(bytearray_to_binary_visual_list(buffer))
x_past=0
y_past=0
def plot_function(fb, func, x_min, x_max, y_min, y_max, width, height):
global x_past, y_past
# Scale factors to map function values to the display's pixel coordinates
x_scale = (x_max - x_min) /(width)
y_scale = (y_max - y_min) / (height)
# Loop through each x pixel
for x_pixel in range(width):
# Convert the pixel x-coordinate to the mathematical x-value
x_value = x_min + (x_pixel) * x_scale
# Calculate the corresponding y-value for the function
y_value = func(x_value)
# Scale the y-value to the display height and invert it (because displays usually have y=0 at the top)
y_pixel = int(height - (y_value - y_min) / y_scale)
if func(x_value)>y_max or func(x_value)<y_min:
print(" pixel= ",x_pixel," ", y_pixel," value= ",x_value," ", y_value ," past= ",x_past," ",y_past)
x_past=0
y_past=0
# fb.vline(x_pixel,y_pixel, 100,1)
# Ensure y_pixel is within the display range
if 0 <= y_pixel < height:
fb.pixel(x_pixel-1, y_pixel-1, 1)
# if x_past!=0 and y_past!=0:
if x_past*y_past!=0:
fb.line(x_past, y_past, x_pixel-1, y_pixel-1, 1)
x_past=x_pixel-1
y_past=y_pixel-1
def polynom(x):
y=sin(x)
return y
plot_function(fb, polynom, -2, 2, -1, 1, 128, 64)
print(bytearray_to_binary_visual_list(buffer))
# # Define the content of the new Python file
# new_file_content = """
# def greet():
# print("Hello from the newly created Python file!")
# greet()
# """
# # Define the name of the new Python file
# new_file_name = "new_script.py"
# # Create and write the content to the new Python file
# with open(new_file_name, "w") as new_file:
# new_file.write(new_file_content)
# # Read the content of the new file and execute it
# with open(new_file_name, "r") as new_file:
# code = new_file.read()
# exec(code)