Set program to run on boot

Continuing the discussion from Run program on boot:

Is there a way to set the program to run on boot without clicking Run in XRPCode?

MicroPython first looks for a file called boot.py and then looks for main.py. XRPCode hides main.py for a better user experience, and modifies it when you hit run. Our main.py does a number of things including setting up the bluetooth to connect remotely and resets the XRP to a known state when a program ends or gets an exception.
Depending on what you are trying to do.

  • One way to start a program but have it wait is to put a wait_for_button() at the beginning of the program. There is a block for this as well.
  • You can use another editor like Thonny that will allow you to edit the main.py, you can replace it with your own code, or you will see that we compile and then run a specific program and you can modify the program that it runs.
  • I have not tried playing with boot.py, but you could also do that.

XRP Code doesn’t use boot.py, right? It’s just an option that MicroPython would support, if it existed. When I list files via the MicroPython REPL I don’t see that file:

>>> import os
>>> os.listdir()
['XRPExamples', 'lib', 'main.py']

Here’s what main.py looks like for me:

import os
import sys
import time
FILE_PATH = '/lib/ble/isrunning'
doNothing = False
x = os.dupterm(None, 0)
if(x == None):
   import ble.blerepl
else:
   os.dupterm(x,0)
try:
   with open(FILE_PATH, 'r+b') as file:
      byte = file.read(1)
      if byte == b'\x01':
         file.seek(0)
         file.write(b'\x00')
         doNothing = True
      else:
         file.seek(0)
         file.write(b'\x01')
   if(not doNothing):
       with open('/XRPExamples/installation_verification.py', mode='r') as exfile:
           code = exfile.read()
       execCode = compile(code, 'XRPExamples/installation_verification.py', 'exec')
       exec(execCode)
       with open(FILE_PATH, 'r+b') as file:
           file.write(b'\x00')
except Exception as e:
   import sys
   sys.print_exception(e)
   with open(FILE_PATH, 'r+b') as file:
      file.write(b'\x00')
finally:
   import gc
   gc.collect()
   if 'XRPLib.resetbot' in sys.modules:
      del sys.modules['XRPLib.resetbot']
   import XRPLib.resetbot

A fun hack might be to set up a small display and a couple buttons that let you choose what program to run at runtime. E.g. you press button A and the display shows the name of one of your scripts. Press button A again and it shows a different script. If you press button B then it executes whatever script is currently being displayed on the screen.