Tufty2350 Code won't run as an App

With a lot of help from @BillyTPilgrim I finally have some code from my Tufty 2040 working on my Tufty 2350. It will run from Thonny, and as main.py, but won’t run as _init_.py from my app folder?
When run from the App menu the display goes blank / black and I hear a beep when it disconnects as a USB device. Then i here another beep when it reconnects and just goes back to the App Menu. When running apps that work it shows as USB drive with no disconnect. I only hear the beep on reset then.

import time
from breakout_bme280 import BreakoutBME280
from machine import I2C

badge.mode(HIRES)

temperature_sensor = BreakoutBME280(I2C())
last_ticks = badge.ticks

t = 0
p = 0
h = 0
C = 1

min_temperature = 100
max_temperature = -100

#screen.font = font.load("/system/assets/fonts/MonaSans-Medium.af")
screen.font = rom_font.ignore

screen.pen = color.white
h_color = screen.pen
p_color = screen.pen


def describe_humidity(h):
    global h_color
    if h < 30:
        description = "Low"
        h_color = color.red
    elif 30 <= h <= 60:
        description = "OK"
        h_color = color.green
    elif 60 < h < 80:
        description = "High"
        h_color = color.yellow
    elif h >= 80:
        description = "Very High"
        h_color = color.orange        
    return description


def describe_pressure(p):
    global p_color
    if p < 982:
        description = "Very Low"
        p_color = color.red
    elif 982 <= p < 1004:
        description = "Low"
        p_color = color.yellow
    elif 1004 <= p < 1026:
        description = "OK"
        p_color = color.green
    elif 1026 <= p < 1048:
        description = "High"
        p_color = color.blue
    elif p >= 1048:
        description = "Very High"
        p_color = color.orange
    return description


def draw_graph():
    if C == 1:
        scaled_temp = int(t * 10)
        if scaled_temp <= 9:
            scaled_temp =9
        if scaled_temp >= 310:
            scaled_temp =310

        screen.pen = color.yellow
        screen.rectangle(9,89,116,19)
        screen.pen = color.white
        screen.circle(9,98,9)
        screen.pen = color.green
        screen.rectangle(127,89,111,19)
        screen.pen = color.orange
        screen.rectangle(240,89,28,19)
        screen.pen = color.red
        screen.rectangle(270,89,44,19)
        screen.circle(310,98,9)    
    
    if C == 0:    
        scaled_temp = int((t * 5) + 160)
        if scaled_temp <= 9:
            scaled_temp =9
        if scaled_temp >= 310:
            scaled_temp =310
    
        screen.pen = color.blue
        screen.circle(9, 98, 9)
        screen.rectangle(12, 89, 92, 19)
        screen.pen = color.white
        screen.rectangle(106, 89, 53, 19)
        screen.pen = color.yellow
        screen.rectangle(161,89,58,19)
        screen.pen = color.green
        screen.rectangle(221,89,58,19)
        screen.pen = color.orange
        screen.rectangle(281,89,13,19)
        screen.pen = color.red
        screen.rectangle(296,89,15,19)
        screen.circle(310,98,9)
            
    scaled_humid = int(h * (320 / 100))
    if scaled_humid <= 9:
        scaled_humid =9
    if scaled_humid >= 310:
        scaled_humid =310

    humidity_text = describe_humidity(h)
    screen.pen = (h_color)
    screen.text(humidity_text,130,118)
    
    scaled_press = int((p - 960) * 3)
    if scaled_press <= 9:
        scaled_press =9
    if scaled_press >= 310:
        scaled_press =310

    pressure_text = describe_pressure(p)
    screen.pen = (p_color)
    screen.text(pressure_text,130,184) 
    
    screen.pen = color.red
    screen.circle(9, 230, 9)
    screen.circle(9, 164, 9)
    screen.rectangle(12,221,50,19)
    screen.rectangle(12,155,82,19)

    screen.pen = color.yellow
    screen.rectangle(64,221,62,19)
    screen.rectangle(192,155,62,19)

    screen.pen = color.green
    screen.rectangle(128,221,62,19)
    screen.rectangle(96,155,94,19)

    screen.pen = color.blue
    screen.rectangle(192,221,62,19)

    screen.pen = color.orange
    screen.rectangle(256,221,52,19)
    screen.circle(310, 230, 9)
    screen.rectangle(256,155,55,19)
    screen.circle(310,164,9)

    screen.pen = color.black
    screen.line(127,221,127,240)
    screen.circle((scaled_temp),98,9)
    screen.circle((scaled_humid),164,9)
    screen.circle((scaled_press),230,9)

    screen.pen = color.white
    screen.circle((scaled_temp),98,5)
    screen.circle((scaled_humid),164,5)
    screen.circle((scaled_press),230,5)


def describe_month(month):
    if month == 1:
        description = "Jan"
    elif month == 2:
        description = "Feb"  
    elif month == 3:
        description = "Mar"
    elif month == 4:
        description = "Apr"              
    elif month == 5:
        description = "May"              
    elif month == 6:
        description = "Jun"              
    elif month == 7:
        description = "Jul"              
    elif month == 8:
        description = "Aug"              
    elif month == 9:
        description = "Sep"
    elif month == 10:
        description = "Oct"             
    elif month == 11:
        description = "Nov"              
    elif month == 12:
        description = "Dec"            
    return description


def describe_day(day):
    if day == 1:
        description = "st"
    elif day == 2:
        description = "nd"     
    elif day == 3:
        description = "rd" 
    elif day == 21:
        description = "st"      
    elif day == 22:
        description = "nd"  
    elif day == 23:
        description = "rd"
    elif day == 31:
        description = "st"
    else:
        description = "th"
    return description

def get_reading():
    global t, p, h
    temperature, pressure, humidity = temperature_sensor.read()
    pressuremb = pressure / 100
    t = round(temperature)
    h = round(humidity)
    p = round(pressuremb)
    
get_reading()
time.sleep(0.5)

def update():
    global last_ticks

    screen.pen = color.black
    screen.clear()

    if badge.ticks - last_ticks >= 2000:
        last_ticks = badge.ticks
        get_reading()
    
    year, month, day, hour, minute, second, weekday = rtc.datetime()
    #print(f"{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}")
    
    screen.pen = color.white
    
    if weekday == 0:
        weekday_text = "Mon"
    if weekday == 1:
        weekday_text = "Tue"
    if weekday == 2:
        weekday_text = "Wed"
    if weekday == 3:
        weekday_text = "Thu"
    if weekday == 4:
        weekday_text = "Fri"
    if weekday == 5:
        weekday_text = "Sat"
    if weekday == 6:
        weekday_text = "Sun"                      

    if hour == 0:
        time_text = f"{12}:{minute:02} AM"
    elif 0 < hour < 10:
        time_text = f"{hour:1}:{minute:02} AM"         
    elif 10 <= hour < 12:
        time_text = f"{hour:2}:{minute:02} AM"
    elif hour == 12:
        time_text = f"{hour:2}:{minute:02} PM"   
    elif hour >12:
        hour = hour - 12
        if hour <10:
            time_text = f"{hour:1}:{minute:02} PM"
        elif 10 <= hour < 12:
            time_text = f"{hour:2}:{minute:02} PM"
        elif hour == 12:
            time_text = f"{hour:2}:{minute:02} AM"

    screen.text(f"{weekday_text}   {describe_month(month)} {day}{describe_day(day)}   {time_text}",0,9)
    
    if t <= 0:
        C = 0

    if t >= 12:
        C = 1

    screen.text("{:0.0f}°C" .format(t), 5, 52)
    screen.text("{:0.0f}%".format(h), 5, 118)
    screen.text("{:0.0f}mb".format(p), 5, 184)
   
    draw_graph()
    
run(update)
![Capture|561x500](upload://xy6oriNJPHvhJH2C4G7wEPemIXd.jpeg)

I just figured it out, I was really confused because I pasted your code there into a fresh __init__.py on a freshly flashed Tufty and it worked without any problems at all - then I realised. It’s two underscores either side of init in the filename, not one, but there’s a lot of times that’s really difficult to see, like in your File Manager screenshot there.

Changed mine to _init_.py, and got exactly what you’re reporting, an invalid app. Dollars to doughnuts that’s your issue.

What is the correct way to edit a file in an IDE?

I can rename it in USB disk mode no problem. But when I eject it I get an error?


I can go back in disk mode and it does show, __init__.py.
Still doesn’t work though?
Should I just press reset on the Tufty instead of Windows eject media?

In USB Disk mode Thonny doesn’t see it? Not without clicking Stop, or selecting in the interpreter. And then after an edit when I click save I get this.

It looks like I made some edits that didn’t get saved to the file in my app folder?

After opening my backup file in Thonny. I saved it to the backup folder on my PC as __init__.py. Then put the Tufty in disk mode and copied it over. Now it’s working.

Is it not possible to save from an IDE to the Tufty?

Personally, if I’m working straight on the device (which is risky of course, no filesystem is perfect, so back up regularly), I’ll have the relevant files open in VS Code, that way I can double press reset, edit files, Ctrl-S, reset, test, rinse and repeat. Quickest way I’ve found to do it.

My current plan of attach is to save __init__.py to my PC. Then do all my tinkering editing and test running on the Tufty from there. All my edits get saved no problem if I’m editing the file stored on my PC. Once I’m all done in that session, I put the Tufty in USB Disk Mode and copy that file over. It’s a little inconvenient, but it works. I think my edits not being saved was messing me up. Anyway, thanks again for the help. =)