code.py
1 # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries 2 # SPDX-License-Identifier: MIT 3 """ 4 CircuitPython Essentials Storage CP Filesystem code.py file 5 6 For use with boards that have a built-in NeoPixel or NeoPixels, but no little red LED. 7 8 It will use only one pixel as an indicator, even if there is more than one NeoPixel. 9 """ 10 import time 11 import board 12 import microcontroller 13 import neopixel 14 15 pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) 16 17 try: 18 with open("/temperature.txt", "a") as temp_log: 19 while True: 20 # The microcontroller temperature in Celsius. Include the 21 # math to do the C to F conversion here, if desired. 22 temperature = microcontroller.cpu.temperature 23 24 # Write the temperature to the temperature.txt file every 10 seconds. 25 temp_log.write('{0:.2f}\n'.format(temperature)) 26 temp_log.flush() 27 28 # Blink the NeoPixel on every write... 29 pixel.fill((255, 0, 0)) 30 time.sleep(1) # ...for one second. 31 pixel.fill((0, 0, 0)) # Then turn it off... 32 time.sleep(9) # ...for the other 9 seconds. 33 34 except OSError as e: # When the filesystem is NOT writable by CircuitPython... 35 delay = 0.5 # ...blink the NeoPixel every half second. 36 if e.args[0] == 28: # If the file system is full... 37 delay = 0.15 # ...blink the NeoPixel every 0.15 seconds! 38 while True: 39 pixel.fill((255, 0, 0)) 40 time.sleep(delay) 41 pixel.fill((0, 0, 0)) 42 time.sleep(delay)