torchknob.py

Created by steveg1cmz

Created on March 14, 2024

1.96 KB

Torch/beacon. The torchknob version is for platforms with no buttons, like the Numworks on Android. Ask for inputs or use defaults.


"""Torchknob (Torch-no butttons)
This version is suitable for devices with limited io
such as Numworks (so cannot use keydown buttons to change color or brightness).
Asks for blink, color and then runs (or defaults).
(Use blinkmax to minimise Numworks reinstalls).
"""
__version__="0.1"

#try:
from ion import *
from kandinsky import *
from math import *
from time import *
#except:
#  print("may be useful in a powercut")

#screensize (320*216 common, 320*240 some)
w=320
h=216 
#h=240

#customise
ask=True #ask for info 1st (else use defaults)
#blinking
blinking=False
blinkmax=20 #in absence of off-switch io 
blink1=1 #each blink sleep
#remember to set blinkmax to a useful value
#remembering its difficult to off the torch

dimred=[128,0,0] #preserves night vision
colors=["white","red","green","blue",dimred]
color="red" #default until asked
#end custom

crid="Torchknob\n© 2024 SteveG1CMZ"

def about():
  print("Use can have medical or legal consequences")
  print("Liability disclaimed")
  print("Steady Off: Backarrow") #then re-execute
  print("Blink Off: [x]") #then reload script!!!

 
if __name__=="__main__" or True:
  print(crid)
  if ask:
    #ask for blinkmax and color (else use defaults)
    for n,c in enumerate(colors):
      print (n,c)
    num=len(colors)-1
    blinkmax=eval(input("Blinkmax?[0=steady]"))
    if blinkmax>0:
      blinking=True
    else:
      blinking=False
    
    n=eval(input("Color?[0.."+str(num)+"] or [rgb]"))    
    try:
      color=colors[n] #integer from list
    except:
      color=n #tuple [r,g,b]
  fill_rect(0,0,w,h,color)
  
  if blinking:
    i=blinkmax
    t0=monotonic()
    while i>0:
      i=i-1
      sleep(blink1)
      fill_rect(0,0,w,h,"black")
      t=int(monotonic()-t0)
      mins=floor(t/60)
      hrs=floor(mins/60) #dont use h
      st=str(t)+" s\n"+str(mins)+"\n"+str(hrs)+" h"
      draw_string(st,0,0)
      #print(st)
      sleep(blink1)
      fill_rect(0,0,w,h,color)
    draw_string("longer than: \n"+st,0,0)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.