You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# SPDX-FileCopyrightText: 2020 Richard Albritton for Adafruit Industries## SPDX-License-Identifier: MITimporttimeimportboardimportmicrocontrollerimportdisplayioimportbusiofromanalogioimportAnalogInimportneopixelimportadafruit_adt7410fromadafruit_bitmap_fontimportbitmap_fontfromadafruit_display_text.labelimportLabelfromadafruit_buttonimportButtonimportadafruit_touchscreenfromadafruit_pyportalimportPyPortal# ------------- Constants ------------- ## Sound EffectssoundDemo="/sounds/sound.wav"soundBeep="/sounds/beep.wav"soundTab="/sounds/tab.wav"# Hex ColorsWHITE=0xFFFFFFRED=0xFF0000YELLOW=0xFFFF00GREEN=0x00FF00BLUE=0x0000FFPURPLE=0xFF00FFBLACK=0x000000# Default Label stylingTABS_X=0TABS_Y=15# Default button styling:BUTTON_HEIGHT=40BUTTON_WIDTH=80# Default Stateview_live=1icon=1icon_name="Ruby"button_mode=1switch_state=0# ------------- Functions ------------- ## Backlight function# Value between 0 and 1 where 0 is OFF, 0.5 is 50% and 1 is 100% brightness.defset_backlight(val):
val=max(0, min(1.0, val))
try:
board.DISPLAY.auto_brightness=FalseexceptAttributeError:
passboard.DISPLAY.brightness=val# Helper for cycling through a number set of 1 to x.defnumberUP(num, max_val):
num+=1ifnum<=max_val:
returnnumelse:
return1# Set visibility of layerdeflayerVisibility(state, layer, target):
try:
ifstate=="show":
time.sleep(0.1)
layer.append(target)
elifstate=="hide":
layer.remove(target)
exceptValueError:
pass# This will handle switching Images and Iconsdefset_image(group, filename):
"""Set the image file for a given goup for display. This is most useful for Icons or image slideshows. :param group: The chosen group :param filename: The filename of the chosen image """print("Set image to ", filename)
ifgroup:
group.pop()
ifnotfilename:
return# we're done, no icon desired# CircuitPython 6 & 7 compatibleimage_file=open(filename, "rb")
image=displayio.OnDiskBitmap(image_file)
image_sprite=displayio.TileGrid(
image, pixel_shader=getattr(image, "pixel_shader", displayio.ColorConverter())
)
# # CircuitPython 7+ compatible# image = displayio.OnDiskBitmap(filename)# image_sprite = displayio.TileGrid(image, pixel_shader=image.pixel_shader)group.append(image_sprite)
# return a reformatted string with word wrapping using PyPortal.wrap_nicelydeftext_box(target, top, string, max_chars):
text=pyportal.wrap_nicely(string, max_chars)
new_text=""test=""forwintext:
new_text+="\n"+wtest+="M\n"text_height=Label(font, text="M", color=0x03AD31)
text_height.text=test# Odd things happen without thisglyph_box=text_height.bounding_boxtarget.text=""# Odd things happen without thistarget.y=int(glyph_box[3] /2) +toptarget.text=new_textdefget_Temperature(source):
ifsource: # Only if we have the temperature sensorcelsius=source.temperatureelse: # No temperature sensorcelsius=microcontroller.cpu.temperaturereturn (celsius*1.8) +32# ------------- Inputs and Outputs Setup ------------- #light_sensor=AnalogIn(board.LIGHT)
try:
# attempt to init. the temperature sensori2c_bus=busio.I2C(board.SCL, board.SDA)
adt=adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
adt.high_resolution=TrueexceptValueError:
# Did not find ADT7410. Probably running on Titano or Pyntadt=None# ------------- Screen Setup ------------- #pyportal=PyPortal()
pyportal.set_background("/images/loading.bmp") # Display an image until the loop startspixel=neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1)
# Touchscreen setup [ Rotate 270 ]display=board.DISPLAYdisplay.rotation=270ifboard.board_id=="pyportal_titano":
screen_width=320screen_height=480set_backlight(
1
) # 0.3 brightness does not cause the display to be visible on the Titanoelse:
screen_width=240screen_height=320set_backlight(0.3)
# We want three buttons across the top of the screenTAB_BUTTON_Y=0TAB_BUTTON_HEIGHT=40TAB_BUTTON_WIDTH=int(screen_width/3)
# We want two big buttons at the bottom of the screenBIG_BUTTON_HEIGHT=int(screen_height/3.2)
BIG_BUTTON_WIDTH=int(screen_width/2)
BIG_BUTTON_Y=int(screen_height-BIG_BUTTON_HEIGHT)
# Initializes the display touch screen areats=adafruit_touchscreen.Touchscreen(
board.TOUCH_YD,
board.TOUCH_YU,
board.TOUCH_XR,
board.TOUCH_XL,
calibration=((5200, 59000), (5800, 57000)),
size=(screen_width, screen_height),
)
# ------------- Display Groups ------------- #splash=displayio.Group() # The Main Display Groupview1=displayio.Group() # Group for View 1 objectsview2=displayio.Group() # Group for View 2 objectsview3=displayio.Group() # Group for View 3 objects# ------------- Setup for Images ------------- #bg_group=displayio.Group()
splash.append(bg_group)
set_image(bg_group, "/images/BGimage.bmp")
icon_group=displayio.Group()
icon_group.x=180icon_group.y=120icon_group.scale=1view2.append(icon_group)
# ---------- Text Boxes ------------- ## Set the font and preload lettersfont=bitmap_font.load_font("/fonts/Helvetica-Bold-16.bdf")
font.load_glyphs(b"abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ()")
# Text Label Objectsfeed1_label=Label(font, text="Text Window 1", color=0xE39300)
feed1_label.x=TABS_Xfeed1_label.y=TABS_Yview1.append(feed1_label)
feed2_label=Label(font, text="Text Window 2", color=0xFFFFFF)
feed2_label.x=TABS_Xfeed2_label.y=TABS_Yview2.append(feed2_label)
sensors_label=Label(font, text="Data View", color=0x03AD31)
sensors_label.x=TABS_Xsensors_label.y=TABS_Yview3.append(sensors_label)
sensor_data=Label(font, text="Data View", color=0x03AD31)
sensor_data.x=TABS_X+16# Indents the text layoutsensor_data.y=150view3.append(sensor_data)
# ---------- Display Buttons ------------- ## This group will make it easy for us to read a button press later.buttons= []
# Main User Interface Buttonsbutton_view1=Button(
x=0, # Start at furthest lefty=0, # Start at topwidth=TAB_BUTTON_WIDTH, # Calculated widthheight=TAB_BUTTON_HEIGHT, # Static heightlabel="View 1",
label_font=font,
label_color=0xFF7E00,
fill_color=0x5C5B5C,
outline_color=0x767676,
selected_fill=0x1A1A1A,
selected_outline=0x2E2E2E,
selected_label=0x525252,
)
buttons.append(button_view1) # adding this button to the buttons groupbutton_view2=Button(
x=TAB_BUTTON_WIDTH, # Start after width of a buttony=0,
width=TAB_BUTTON_WIDTH,
height=TAB_BUTTON_HEIGHT,
label="View 2",
label_font=font,
label_color=0xFF7E00,
fill_color=0x5C5B5C,
outline_color=0x767676,
selected_fill=0x1A1A1A,
selected_outline=0x2E2E2E,
selected_label=0x525252,
)
buttons.append(button_view2) # adding this button to the buttons groupbutton_view3=Button(
x=TAB_BUTTON_WIDTH*2, # Start after width of 2 buttonsy=0,
width=TAB_BUTTON_WIDTH,
height=TAB_BUTTON_HEIGHT,
label="View 3",
label_font=font,
label_color=0xFF7E00,
fill_color=0x5C5B5C,
outline_color=0x767676,
selected_fill=0x1A1A1A,
selected_outline=0x2E2E2E,
selected_label=0x525252,
)
buttons.append(button_view3) # adding this button to the buttons groupbutton_switch=Button(
x=0, # Start at furthest lefty=BIG_BUTTON_Y,
width=BIG_BUTTON_WIDTH,
height=BIG_BUTTON_HEIGHT,
label="Light Switch",
label_font=font,
label_color=0xFF7E00,
fill_color=0x5C5B5C,
outline_color=0x767676,
selected_fill=0x1A1A1A,
selected_outline=0x2E2E2E,
selected_label=0x525252,
)
buttons.append(button_switch) # adding this button to the buttons groupbutton_2=Button(
x=BIG_BUTTON_WIDTH, # Starts just after button 1 widthy=BIG_BUTTON_Y,
width=BIG_BUTTON_WIDTH,
height=BIG_BUTTON_HEIGHT,
label="Light Color",
label_font=font,
label_color=0xFF7E00,
fill_color=0x5C5B5C,
outline_color=0x767676,
selected_fill=0x1A1A1A,
selected_outline=0x2E2E2E,
selected_label=0x525252,
)
buttons.append(button_2) # adding this button to the buttons group# Add all of the main buttons to the splash Groupforbinbuttons:
splash.append(b)
# Make a button to change the icon image on view2button_icon=Button(
x=150,
y=60,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="Icon",
label_font=font,
label_color=0xFFFFFF,
fill_color=0x8900FF,
outline_color=0xBC55FD,
selected_fill=0x5A5A5A,
selected_outline=0xFF6600,
selected_label=0x525252,
style=Button.ROUNDRECT,
)
buttons.append(button_icon) # adding this button to the buttons group# Add this button to view2 Groupview2.append(button_icon)
# Make a button to play a sound on view2button_sound=Button(
x=150,
y=170,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="Sound",
label_font=font,
label_color=0xFFFFFF,
fill_color=0x8900FF,
outline_color=0xBC55FD,
selected_fill=0x5A5A5A,
selected_outline=0xFF6600,
selected_label=0x525252,
style=Button.ROUNDRECT,
)
buttons.append(button_sound) # adding this button to the buttons group# Add this button to view2 Groupview3.append(button_sound)
# pylint: disable=global-statementdefswitch_view(what_view):
globalview_liveifwhat_view==1:
button_view1.selected=Falsebutton_view2.selected=Truebutton_view3.selected=TruelayerVisibility("hide", splash, view2)
layerVisibility("hide", splash, view3)
layerVisibility("show", splash, view1)
elifwhat_view==2:
# global iconbutton_view1.selected=Truebutton_view2.selected=Falsebutton_view3.selected=TruelayerVisibility("hide", splash, view1)
layerVisibility("hide", splash, view3)
layerVisibility("show", splash, view2)
else:
button_view1.selected=Truebutton_view2.selected=Truebutton_view3.selected=FalselayerVisibility("hide", splash, view1)
layerVisibility("hide", splash, view2)
layerVisibility("show", splash, view3)
# Set global button stateview_live=what_viewprint("View {view_num:.0f} On".format(view_num=what_view))
# pylint: enable=global-statement# Set veriables and startup statesbutton_view1.selected=Falsebutton_view2.selected=Truebutton_view3.selected=Truebutton_switch.label="OFF"button_switch.selected=TruelayerVisibility("show", splash, view1)
layerVisibility("hide", splash, view2)
layerVisibility("hide", splash, view3)
# Update out Labels with display text.text_box(
feed1_label,
TABS_Y,
"The text on this screen is wrapped so that all of it fits nicely into a ""text box that is {} x {}.".format(
feed1_label.bounding_box[2], feed1_label.bounding_box[3] *2
),
30,
)
text_box(feed2_label, TABS_Y, "Tap on the Icon button to meet a new friend.", 18)
text_box(
sensors_label,
TABS_Y,
"This screen can display sensor readings and tap Sound to play a WAV file.",
28,
)
board.DISPLAY.show(splash)
# ------------- Code Loop ------------- #whileTrue:
touch=ts.touch_pointlight=light_sensor.valuesensor_data.text="Touch: {}\nLight: {}\nTemp: {:.0f}°F".format(
touch, light, get_Temperature(adt)
)
# Will also cause screen to dim when hand is blocking sensor to touch screen# # Adjust backlight# if light < 1500:# set_backlight(0.1)# elif light < 3000:# set_backlight(0.5)# else:# set_backlight(1)# ------------- Handle Button Press Detection ------------- #iftouch: # Only do this if the screen is touched# loop with buttons using enumerate() to number each button group as ifori, binenumerate(buttons):
ifb.contains(touch): # Test each button to see if it was pressedprint("button{} pressed".format(i))
ifi==0andview_live!=1: # only if view1 is visiblepyportal.play_file(soundTab)
switch_view(1)
whilets.touch_point:
passifi==1andview_live!=2: # only if view2 is visiblepyportal.play_file(soundTab)
switch_view(2)
whilets.touch_point:
passifi==2andview_live!=3: # only if view3 is visiblepyportal.play_file(soundTab)
switch_view(3)
whilets.touch_point:
passifi==3:
pyportal.play_file(soundBeep)
# Toggle switch button typeifswitch_state==0:
switch_state=1b.label="ON"b.selected=Falsepixel.fill(WHITE)
print("Switch ON")
else:
switch_state=0b.label="OFF"b.selected=Truepixel.fill(BLACK)
print("Switch OFF")
# for debouncewhilets.touch_point:
passprint("Switch Pressed")
ifi==4:
pyportal.play_file(soundBeep)
# Momentary button typeb.selected=Trueprint("Button Pressed")
button_mode=numberUP(button_mode, 5)
ifbutton_mode==1:
pixel.fill(RED)
elifbutton_mode==2:
pixel.fill(YELLOW)
elifbutton_mode==3:
pixel.fill(GREEN)
elifbutton_mode==4:
pixel.fill(BLUE)
elifbutton_mode==5:
pixel.fill(PURPLE)
switch_state=1button_switch.label="ON"button_switch.selected=False# for debouncewhilets.touch_point:
passprint("Button released")
b.selected=Falseifi==5andview_live==2: # only if view2 is visiblepyportal.play_file(soundBeep)
b.selected=Truewhilets.touch_point:
passprint("Icon Button Pressed")
icon=numberUP(icon, 3)
ificon==1:
icon_name="Ruby"elificon==2:
icon_name="Gus"elificon==3:
icon_name="Billie"b.selected=Falsetext_box(
feed2_label,
TABS_Y,
"Every time you tap the Icon button the icon image will ""change. Say hi to {}!".format(icon_name),
18,
)
set_image(icon_group, "/images/"+icon_name+".bmp")
ifi==6andview_live==3: # only if view3 is visibleb.selected=Truewhilets.touch_point:
passprint("Sound Button Pressed")
pyportal.play_file(soundDemo)
b.selected=False
Behavior
soft reboot
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Traceback (most recent call last):
File "code.py", line 148, in
File "adafruit_pyportal/init.py", line 128, in init
File "adafruit_pyportal/graphics.py", line 44, in init
File "adafruit_portalbase/graphics.py", line 60, in init
AttributeError: .show(x) removed. Use .root_group = x
Code done running.
Press any key to enter the REPL. Use CTRL-D to reload.
Description
No response
Additional information
No response
The text was updated successfully, but these errors were encountered:
CircuitPython version
Code/REPL
Behavior
soft reboot
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Traceback (most recent call last):
File "code.py", line 148, in
File "adafruit_pyportal/init.py", line 128, in init
File "adafruit_pyportal/graphics.py", line 44, in init
File "adafruit_portalbase/graphics.py", line 60, in init
AttributeError: .show(x) removed. Use .root_group = x
Code done running.
Press any key to enter the REPL. Use CTRL-D to reload.
Description
No response
Additional information
No response
The text was updated successfully, but these errors were encountered: