from ssd1306 import SSD1306_I2C from machine import Pin, I2C from time import sleep import time i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=200000)#Grove - OLED oled 0.96" (SSD1315) oled = SSD1306_I2C(128, 64, i2c) def test(): oled.fill(0)#clear oled.text("Hello,World!",0,0) oled.show() sleep(2) oled.fill(0) oled.fill_rect(0, 0, 32, 32, 1) oled.fill_rect(2, 2, 28, 28, 0) oled.vline(9, 8, 22, 1) oled.vline(16, 2, 22, 1) oled.vline(23, 8, 22, 1) oled.fill_rect(26, 24, 2, 4, 1) oled.text('MicroPython', 40, 0, 1) oled.text('SSD1306', 40, 12, 1) oled.text('OLED 128x64', 40, 24, 1) oled.show() sleep(3) oled.fill(0) def draw_smiley_mouth(oled, start_x, start_y, width, height): # Calculate the center and the radius of the half-circle center_x = start_x + width // 2 center_y = start_y + height // 2 radius = width // 2 # Draw the half-circle pixel by pixel for x in range(start_x, start_x + width): # Calculate the y coordinate based on the half-circle equation y = int(math.sqrt(radius**2 - (x - center_x)**2) + center_y) # Draw the pixel oled.pixel(x, y, 1) #chatGPT improved version def write_to_screen(message): global lines_on_screen # Calculate how many lines on the screen this message will take up lines_needed = (len(message) + 13) // 14 # Adjust calculation to consider partial lines # Reset the screen if there's not enough space if lines_on_screen + lines_needed > 8: oled.fill(0) lines_on_screen = 0 oled.show() time.sleep(0.5) # Process the message to fit into the screen lines while len(message) > 0: # Remove the first char if it is a space if message[0] == " ": message = message[1:] continue # Determine the message part to display now if len(message) > 16: # Check if we are cutting off a word and if a hyphen should be added split_point = 16 if message[15] != " " and message[16] != " " and message[14] != " ": split_point = 15 # Adjust to add a hyphen display_message = message[:split_point] + "-" else: # Find the last space to avoid cutting words if possible split_point = message.rfind(" ", 0, 16) if split_point == -1: split_point = 16 # No space found; use the whole space available display_message = message[:split_point] remaining_message = message[split_point:].lstrip() # Remove leading spaces from the remaining part else: display_message = message remaining_message = "" # Display the current part of the message oled.text(display_message, 0, lines_on_screen * 8) lines_on_screen += 1 oled.show() time.sleep(0.75) # Prepare for the next iteration, if there is any remaining message message = remaining_message # Check and reset the screen if needed (in case of very long messages) if lines_on_screen == 8: oled.fill(0) lines_on_screen = 0 oled.show() time.sleep(0.5) def write_to_screen_old(message): global lines_on_screen #calculate how many lines on the screen this message will take up lines_needed = len(message) // 14 if lines_on_screen == 8 or ((lines_on_screen + lines_needed) > 8): oled.fill(0) lines_on_screen = 0 oled.show() time.sleep(0.5) # remove the first char if it is a space if message[0] == " ": message = message[1:] remaining_message = None if len(message) > 16: # if we are cutting off a word add a hyphen if (message[15] != " ") and (message[16] != " ") and (message[14] != " "): first_line = message[:15] + "-" remaining_message = message[15:] message = first_line else: first_line = message[:16] remaining_message = message[16:] message = first_line oled.text(message, 0, lines_on_screen * 8) lines_on_screen += 1 oled.show() time.sleep(0.75) if remaining_message: write_to_screen(remaining_message) # Clear the display oled.fill(0) # Draw the smiley mouth draw_smiley_mouth(oled, 16, 0, 90, 20)