If you feel like the canvas method is too much effort - especially if it part of a larger project or you prefer M5GFX - then here is a working switch in M5GFX:
import os, sys, io
import M5
from M5 import *
import time
Switch1 = None
Switch2 = None
Switch3 = None
Knob = None
state = None
touch = None
# Describe this function...
def Off():
global state, touch, Switch1, Switch2, Switch3, Knob
state = 0
Knob.setCursor(x=145, y=119)
Switch1.setColor(color=0x999999, fill_c=0x999999)
Switch2.setColor(color=0x999999, fill_c=0x999999)
Knob.setColor(color=0xffffff, fill_c=0xffffff)
# Describe this function...
def On():
global state, touch, Switch1, Switch2, Switch3, Knob
state = 1
Knob.setCursor(x=174, y=119)
Switch2.setColor(color=0x33ccff, fill_c=0x33ccff)
Switch3.setColor(color=0x33ccff, fill_c=0x33ccff)
Knob.setColor(color=0xffffff, fill_c=0xffffff)
def setup():
global Switch1, Switch2, Switch3, Knob, state, touch
M5.begin()
Widgets.setRotation(1)
Widgets.fillScreen(0x222222)
Switch1 = Widgets.Circle(174, 119, 14, 0xa4a4a4, 0xa4a4a4)
Switch2 = Widgets.Rectangle(145, 105, 28, 28, 0xa4a4a4, 0xa4a4a4)
Switch3 = Widgets.Circle(145, 119, 14, 0xa4a4a4, 0xa4a4a4)
Knob = Widgets.Circle(145, 119, 14, 0xffffff, 0xffffff)
Off()
def loop():
global Switch1, Switch2, Switch3, Knob, state, touch
M5.update()
touch = M5.Touch.getCount()
if touch != 0:
if (M5.Touch.getX()) >= 131 and (M5.Touch.getX()) <= 188 and (M5.Touch.getY()) >= 105 and (M5.Touch.getY()) <= 133:
if state == 0:
On()
else:
Off()
time.sleep_ms(500)
if __name__ == '__main__':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware")