forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Labels
Milestone
Description
CircuitPython version and board name
Adafruit CircuitPython 9.2.8 on Pico W RP2040
Hard fault: memory access or instruction error.Code/REPL
# This script supports the Raspberry Pi Pico board and the Lilygo ESP32-S2 board
# Raspberry Pi Pico: http://educ8s.tv/part/RaspberryPiPico
# ESP32-S2 Board: http://educ8s.tv/part/esp32s2
import board, busio, displayio, os, fourwire
import adafruit_ili9341
import time, rtc
import wifi
import socketpool
import microcontroller
import adafruit_ntp
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
import terminalio #Just a font
from adafruit_display_text import label
from adafruit_display_text import bitmap_label
from adafruit_bitmap_font import bitmap_font
from adafruit_bitmap_font import bitmap_font
from adafruit_datetime import date, datetime
import rotaryio
import digitalio
#import adafruit_bh1750
displayio.release_displays()
board_type = os.uname().machine
print(f"Board -> {board_type}")
if 'Pico' in board_type:
cs_pin, reset_pin, dc_pin, mosi_pin, clk_pin = board.GP18, board.GP17, board.GP16, board.GP11, board.GP10
elif 'ESP32-S2' in board_type:
mosi_pin, clk_pin, reset_pin, cs_pin, dc_pin = board.IO35, board.IO36, board.IO38, board.IO34, board.IO37
else:
mosi_pin, clk_pin, reset_pin, cs_pin, dc_pin = board.GP11, board.GP10, board.GP17, board.GP18, board.GP16
print("This board is not supported. Change the pin definitions above.")
spi = busio.SPI(clock=clk_pin, MOSI=mosi_pin) # GP10 & GP11
display_bus = fourwire.FourWire(spi, command=dc_pin, chip_select=cs_pin, reset=reset_pin)
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240, rotation=0)
#display = adafruit_ili9341.ILI9341(display_bus, width=240, height=320, rotation=90)
encoder = rotaryio.IncrementalEncoder(board.GP5, board.GP7) # Default Pico I2C pins
#i2c = busio.I2C(board.GP5, board.GP4)
#sensor = adafruit_bh1750.BH1750(i2c)
font_file = "fonts/Arial_12.bdf"
font_arial = bitmap_font.load_font(font_file)
font_file = "fonts/amstrad_cpc_extended.bdf"
font = bitmap_font.load_font(font_file)
timezone = +2
# The time of the thing!
EVENT_YEAR = 2026
EVENT_MONTH = 7
EVENT_DAY = 7
EVENT_HOUR = 0
EVENT_MINUTE = 0
# we'll make a python-friendly structure
event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY, EVENT_HOUR, EVENT_MINUTE, 0, -1, -1, False))
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=timezone, cache_seconds=3600)
rtc.RTC().datetime = ntp.datetime
# Setup Display layout
group = displayio.Group()
# Set text, font, and color
text = "HELLO WORLD"
#font = terminalio.FONT
color = 0x0000FF
# Create the text label
text_area = label.Label(font, text=text, color=color)
text_area.x = 100
text_area.y = 180
scrolling_label = bitmap_label.Label(terminalio.FONT, text=" ", y=display.height - 13)
group.append(text_area)
group.append(scrolling_label)
display.root_group = group
display.auto_refresh = False
refresh_clock = ticks_ms()
refresh_timer = 3600 * 1000
clock_clock = ticks_ms()
clock_timer = 1000
scroll_clock = ticks_ms()
scroll_timer = 50
first_run = True
# Draw a label
text_group = displayio.Group(scale=1, x=100, y=140)
text = "My Displayer"
text_gebied = label.Label(font_arial, text=text, color=0xFFFF00)
#text_gebied2 = label.Label(font_arial, text="sec", color=0xFFFF00)
text_group.append(text_gebied) # Subgroup for text scaling
#text_group.append(text_gebied2) # Subgroup for text scaling
group.append(text_group)
flipper = False
button = digitalio.DigitalInOut(board.GP7)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
button_state = None
last_position = 0
while True:
current_position = encoder.position
position_change = current_position - last_position
if position_change > 0:
print("CW +", current_position)
scroll_timer = scroll_timer - position_change
if scroll_timer < 1:
scroll_timer = 1
elif position_change < 0:
print("CCW -", current_position)
scroll_timer = scroll_timer + (position_change*-1)
if scroll_timer > 100:
scroll_timer = 100
last_position = current_position
if not button.value and button_state is None:
button_state = "pressed"
if button.value and button_state == "pressed":
print("Button pressed.", scroll_timer)
button_state = None
# only query the online time once per hour (and on first run)
if ticks_diff(ticks_ms(), refresh_clock) >= refresh_timer or first_run:
try:
print("Getting time from internet!")
now = ntp.datetime
print(now)
total_seconds = time.mktime(now)
first_run = False
refresh_clock = ticks_add(refresh_clock, refresh_timer)
except Exception as e: # pylint: disable=broad-except
print("Some error occured, retrying! -", e)
time.sleep(2)
microcontroller.reset()
if ticks_diff(ticks_ms(), clock_clock) >= clock_timer:
remaining = time.mktime(event_time) - total_seconds
secs_remaining = remaining % 60
remaining //= 60
mins_remaining = remaining % 60
remaining //= 60
hours_remaining = remaining % 24
remaining //= 24
days_remaining = remaining
scrolling_label.text = (f"{days_remaining} DAYS, {hours_remaining} HOURS," +
f"{mins_remaining} MINUTES & {secs_remaining} SECONDS")
total_seconds += 1
clock_clock = ticks_add(clock_clock, clock_timer)
if secs_remaining % 5 == 0:
flipper = not flipper
if flipper:
try:
#print("Getting time from internet!")
nu = time.localtime()
#print("Now is {:2}-{:02}-{:4} {:02}:{:2}".format(nu.tm_mday, nu.tm_mon, nu.tm_year, nu.tm_hour, nu.tm_min))
text_area.text = "{:2}-{:02}-{:4} {:02}:{:2}".format(nu.tm_mday, nu.tm_mon, nu.tm_year, nu.tm_hour, nu.tm_min)
text_area.scale = 1
except Exception as e: # pylint: disable=broad-except
print("Some error occured, retrying! -", e)
else:
text_area.text = "TIJD"
text_area.scale = 2
if ticks_diff(ticks_ms(), scroll_clock) >= scroll_timer:
scrolling_label.x -= 1
if scrolling_label.x < -(scrolling_label.width + 5):
scrolling_label.x = display.width + 2
display.refresh()
scroll_clock = ticks_add(scroll_clock, scroll_timer)Behavior
You are in safe mode because:
CircuitPython core code is hard gecrashed. Ojee!
Hard fault: memory access or instruction error.
Please file an issue with your program at github.com/adafruit/circuitpython/issues.
Description
Error after trying to use i2c, but after commenting it out it won't start anymore
Additional information
No response