<filename>main.py<gh_stars>0
# Batsignal
# Use CircuitPython on an Adafruit Trinket M0 or other similar board to turn a lamp on and off
#---------------------------------------------------------------------------------------------
# Copyright: 2019 - <NAME> (<EMAIL>)
# Released under the MIT license. 
# https://github.com/drakoswraith/batsignal
#---------------------------------------------------------------------------------------------
# Usage
# 1. Build the circuit...
# 2. Copy main.py to the Trinket M0
# 3. Copy signalon.txt and signaloff.txt to the Trinket M0
# 4. Save to the on/off files to control the light
#
# Basic circuit consists of:
#   a momentary push button
#       - pulled high
#       - on pin 3, and connected to ground
#
#   an HBridge (eg L293D)
#       - wire Vcc1 and Vcc2 both to 5V USB
#       - wire the enable to 3.3V with a 10k resister (on the L293D this is 1,2 EN)
#       - The PWM input (on the L293D this is 1A) on the chip is wired to pin 2 of the trinket
#       - H-Bridge output (on the L293D this is 1Y) goes to the positive lead of the LED/motor/etc..
#       - Ground to Ground.. connect all the grounds for heat sinking
# 
#   Ensure the LED or motor has the correct current limiting resistance
# 
# Lamp (or motor, or whatever...) can be toggled via either a button on D3
# Or by updating the modified time on either /signalon.txt or /signaloff.txt 
# on the root of the filesystem of the trinket
#
# By using the mechanism of saving to signalon.txt and signaloff.txt, it is easy to integrate this into software running
# on the host computer, without having to write complex USB commands, etc...
# This sytem works because the computer is able to write tothe USB storage, and the trinket
# can read from the storage, but not write to it
# As time marches ever forward, tracking the modified timestamp of the files gives a clear
# method of toggling the given command.
# The contents of the file do not matter for this method, although it would be trivial to add
# additional info that way (maybe set dotstar color, etc...)
#---------------------------------------------------------------------------------------------

import board
import pulseio
import digitalio
import adafruit_dotstar as dotstar
import time
import os
import supervisor


def getOnMTime():
    files = os.listdir()
    for f in files:
        if f.lower() == 'signalon.txt':
            # See the python 3.0 os.stat docs for the structure description
            #7 = access time, 8 = modified time, 9 = creation or metadata time
            return os.stat(f)[8]     
    return 0   

def getOffMTime():
    files = os.listdir()
    for f in files:
        if f.lower() == 'signaloff.txt':
            return os.stat(f)[8]     
    return 0  

# we need to disable auto-reload of the scripts to avoid the script
# restarting and losing our state when signalon.txt and signaloff.txt are modified
supervisor.disable_autoreload() 

dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.05)

pwmLamp = pulseio.PWMOut(board.D2)
pwmLamp.duty_cycle = 0 

btnToggle = digitalio.DigitalInOut(board.D3)
btnToggle.direction = digitalio.Direction.INPUT
btnToggle.pull = digitalio.Pull.UP

signalState = False
fileOnModTime = getOnMTime()
fileOffModTime = getOffMTime()
    
while True:
    if not signalState:
        if getOnMTime() > fileOnModTime:
            signalState = True
            fileOnModTime = getOnMTime()
            fileOffModTime = fileOnModTime

    if signalState:
        if getOffMTime() > fileOffModTime:
            signalState = False
            fileOffModTime = getOffMTime()
            fileOnModTime = fileOffModTime

    toggled = False
    #pullup flips the logic
    if not btnToggle.value:
        print("Button toggled")
        signalState = not signalState
        fileOnModTime = getOnMTime()
        fileOffModTime = getOffMTime()
        toggled = True

    if signalState:
        dot[0] = [0, 50, 100]
        pwmLamp.duty_cycle = 65535      #Lower this value to reduce brightness/current draw
    else:
        dot[0] = [0, 0, 0]
        pwmLamp.duty_cycle = 0
        

    #wait till button released to avoid flipping on and off
    if toggled:
        while not btnToggle.value:
            time.sleep(0.05)
    time.sleep(0.1)


<filename>main.py<gh_stars>0
# Batsignal
# Use CircuitPython on an Adafruit Trinket M0 or other similar board to turn a lamp on and off
#---------------------------------------------------------------------------------------------
# Copyright: 2019 - <NAME> (<EMAIL>)
# Released under the MIT license. 
# https://github.com/drakoswraith/batsignal
#---------------------------------------------------------------------------------------------
# Usage
# 1. Build the circuit...
# 2. Copy main.py to the Trinket M0
# 3. Copy signalon.txt and signaloff.txt to the Trinket M0
# 4. Save to the on/off files to control the light
#
# Basic circuit consists of:
#   a momentary push button
#       - pulled high
#       - on pin 3, and connected to ground
#
#   an HBridge (eg L293D)
#       - wire Vcc1 and Vcc2 both to 5V USB
#       - wire the enable to 3.3V with a 10k resister (on the L293D this is 1,2 EN)
#       - The PWM input (on the L293D this is 1A) on the chip is wired to pin 2 of the trinket
#       - H-Bridge output (on the L293D this is 1Y) goes to the positive lead of the LED/motor/etc..
#       - Ground to Ground.. connect all the grounds for heat sinking
# 
#   Ensure the LED or motor has the correct current limiting resistance
# 
# Lamp (or motor, or whatever...) can be toggled via either a button on D3
# Or by updating the modified time on either /signalon.txt or /signaloff.txt 
# on the root of the filesystem of the trinket
#
# By using the mechanism of saving to signalon.txt and signaloff.txt, it is easy to integrate this into software running
# on the host computer, without having to write complex USB commands, etc...
# This sytem works because the computer is able to write tothe USB storage, and the trinket
# can read from the storage, but not write to it
# As time marches ever forward, tracking the modified timestamp of the files gives a clear
# method of toggling the given command.
# The contents of the file do not matter for this method, although it would be trivial to add
# additional info that way (maybe set dotstar color, etc...)
#---------------------------------------------------------------------------------------------

import board
import pulseio
import digitalio
import adafruit_dotstar as dotstar
import time
import os
import supervisor


def getOnMTime():
    files = os.listdir()
    for f in files:
        if f.lower() == 'signalon.txt':
            # See the python 3.0 os.stat docs for the structure description
            #7 = access time, 8 = modified time, 9 = creation or metadata time
            return os.stat(f)[8]     
    return 0   

def getOffMTime():
    files = os.listdir()
    for f in files:
        if f.lower() == 'signaloff.txt':
            return os.stat(f)[8]     
    return 0  

# we need to disable auto-reload of the scripts to avoid the script
# restarting and losing our state when signalon.txt and signaloff.txt are modified
supervisor.disable_autoreload() 

dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.05)

pwmLamp = pulseio.PWMOut(board.D2)
pwmLamp.duty_cycle = 0 

btnToggle = digitalio.DigitalInOut(board.D3)
btnToggle.direction = digitalio.Direction.INPUT
btnToggle.pull = digitalio.Pull.UP

signalState = False
fileOnModTime = getOnMTime()
fileOffModTime = getOffMTime()
    
while True:
    if not signalState:
        if getOnMTime() > fileOnModTime:
            signalState = True
            fileOnModTime = getOnMTime()
            fileOffModTime = fileOnModTime

    if signalState:
        if getOffMTime() > fileOffModTime:
            signalState = False
            fileOffModTime = getOffMTime()
            fileOnModTime = fileOffModTime

    toggled = False
    #pullup flips the logic
    if not btnToggle.value:
        print("Button toggled")
        signalState = not signalState
        fileOnModTime = getOnMTime()
        fileOffModTime = getOffMTime()
        toggled = True

    if signalState:
        dot[0] = [0, 50, 100]
        pwmLamp.duty_cycle = 65535      #Lower this value to reduce brightness/current draw
    else:
        dot[0] = [0, 0, 0]
        pwmLamp.duty_cycle = 0
        

    #wait till button released to avoid flipping on and off
    if toggled:
        while not btnToggle.value:
            time.sleep(0.05)
    time.sleep(0.1)


# Batsignal # Use CircuitPython on an Adafruit Trinket M0 or other similar board to turn a lamp on and off #--------------------------------------------------------------------------------------------- # Copyright: 2019 - <NAME> (<EMAIL>) # Released under the MIT license. # https://github.com/drakoswraith/batsignal #--------------------------------------------------------------------------------------------- # Usage # 1. Build the circuit... # 2. Copy main.py to the Trinket M0 # 3. Copy signalon.txt and signaloff.txt to the Trinket M0 # 4. Save to the on/off files to control the light # # Basic circuit consists of: #   a momentary push button #       - pulled high #       - on pin 3, and connected to ground # #   an HBridge (eg L293D) #       - wire Vcc1 and Vcc2 both to 5V USB #       - wire the enable to 3.3V with a 10k resister (on the L293D this is 1,2 EN) #       - The PWM input (on the L293D this is 1A) on the chip is wired to pin 2 of the trinket #       - H-Bridge output (on the L293D this is 1Y) goes to the positive lead of the LED/motor/etc.. #       - Ground to Ground.. connect all the grounds for heat sinking # #   Ensure the LED or motor has the correct current limiting resistance # # Lamp (or motor, or whatever...) can be toggled via either a button on D3 # Or by updating the modified time on either /signalon.txt or /signaloff.txt # on the root of the filesystem of the trinket # # By using the mechanism of saving to signalon.txt and signaloff.txt, it is easy to integrate this into software running # on the host computer, without having to write complex USB commands, etc... # This sytem works because the computer is able to write tothe USB storage, and the trinket # can read from the storage, but not write to it # As time marches ever forward, tracking the modified timestamp of the files gives a clear # method of toggling the given command. # The contents of the file do not matter for this method, although it would be trivial to add # additional info that way (maybe set dotstar color, etc...) #--------------------------------------------------------------------------------------------- # See the python 3.0 os.stat docs for the structure description #7 = access time, 8 = modified time, 9 = creation or metadata time # we need to disable auto-reload of the scripts to avoid the script # restarting and losing our state when signalon.txt and signaloff.txt are modified #pullup flips the logic #Lower this value to reduce brightness/current draw #wait till button released to avoid flipping on and off