Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Some tips and tricks for CircuitPython, using a QT Py board

NotificationsYou must be signed in to change notification settings

todbot/qtpy-tricks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 

Repository files navigation

Some things to do on a stockAdafruit QT PyrunningCircuitPython 6.This list is mostly to help me remmeber how to do things in CircuitPython.Fore more generalCircuitPython tricks, check out myCircuitPython Tricks page.

These code snippets will also work on a Trinket M0 and really just aboutCircuitPython-compatible board, but you may need to adjust some of theboard pins.

Notes:

Table of Contents

Print "time" on OLED display

[Note: as of CircuitPython 7, this only works on QTPy RP2040, not QTPy M0 or QTPY M0 Haxpress]

importtimeimportboardimportadafruit_ssd1306# requires: adafruit_bus_device and adafruit_framebufi2c=board.I2C()oled=adafruit_ssd1306.SSD1306_I2C(width=128,height=32,i2c=i2c)whileTrue:oled.fill(0)oled.text("hello world",0,0,1)# requires 'font5x8.bin'oled.text("time:"+str(time.monotonic()),0,8,1)oled.show()time.sleep(1.0)

Disco Party on built-in Neopixel

importtimeimportboardimportneopixelfromrandomimportrandintpixel=neopixel.NeoPixel(board.NEOPIXEL,1,brightness=0.2)whileTrue:pixel.fill( (randint(0,255),randint(0,255),randint(0,255) ))time.sleep(0.1)

Output Farty Noises to DAC

importtimeimportboardimportanalogioimportrandomdac=analogio.AnalogOut(board.A0)i=0di=40000lasttime=0whileTrue:dac.value=ii= (i+di)&0xffffiftime.monotonic()-lasttime>1:lasttime=time.monotonic()di=random.randrange(40000,80000)

(hear it in action)

Capsense Touch to Colors on Built-in LED

importboardfromtouchioimportTouchInimportneopixelpixel=neopixel.NeoPixel(board.NEOPIXEL,1,brightness=0.2)touchA=TouchIn(board.A1)touchB=TouchIn(board.A2)print("hello")whileTrue:pixel[0]= (int(touchA.value*255),0,int(touchB.value*255))

Rotary Encoder to Built-in LED

importboardimporttimeimportneopixelimportrotaryiopixel=neopixel.NeoPixel(board.NEOPIXEL,1,brightness=1.0)encoder=rotaryio.IncrementalEncoder(board.MOSI,board.MISO )# any two pinswhileTrue:b= (encoder.position%32)*8print(encoder.position,b)pixel.fill((0,0,b))time.sleep(0.1)

Fire Simulation on External Neopixel Strip

Uses Python array operations and list comprehensions for conciseness.

importboardimporttimeimportneopixelfromrandomimportrandint# External Neopixel strip, can be on any pinleds=neopixel.NeoPixel(board.RX,8,brightness=0.2,auto_write=False)whileTrue:# reduce brightness of all pixels by (30,30,30)leds[:]= [[max(i-30,0)foriinl]forlinleds]# shift LED values up by one (0->1, 1->2, etc)leds[1:]=leds[0:-1]# '-1' means len-1 here# pick new random fire color for LED 0leds[0]= (randint(150,255),randint(50,100),0)leds.show()time.sleep(0.1)

If you want it "flipped", so the fire goes from the top LED down to LED 0:

importtime,board,neopixelfromrandomimportrandintleds=neopixel.NeoPixel(board.RX,8,brightness=0.2,auto_write=False)whileTrue:leds[:]= [[max(i-30,0)foriinl]forlinleds]# reduce brightness of all pixels by (30,30,30)leds[0:-1]=leds[1:]# shift LED values down by oneleds[-1]= (randint(150,255),randint(50,100),0)# pick new random fire color for LED Nleds.show()time.sleep(0.1)

Two servos with Python Class for Easing / Sequencing

importboardimporttimefrompulseioimportPWMOutfromadafruit_motorimportservofromrandomimportrandomservoA=servo.Servo(PWMOut(board.RX,duty_cycle=2**15,frequency=50))servoB=servo.Servo(PWMOut(board.TX,duty_cycle=2**15,frequency=50))classAngleSequence:def__init__(self,angles,speed):self.angles=anglesself.aindex=0self.angle=angles[0]self.speed=speeddefnext_angle(self):self.aindex= (self.aindex+1)%len(self.angles)defupdate(self):new_angle=self.angles[self.aindex]self.angle+= (new_angle-self.angle)*self.speed# simple easingreturnself.angleseqA=AngleSequence( [90,70,90,90,90,90,60,80,100],0.1)# set speed to tasteseqB=AngleSequence( [90,90,90,80,70,90,120],0.1)# set angles for desired animationlasttime=time.monotonic()whileTrue:iftime.monotonic()-lasttime> (0.2+random()*4.0  ):# creepy random timinglasttime=time.monotonic()seqA.next_angle()# go to next angle in listseqB.next_angle()servoA.angle=seqA.update()# get updated (eased) servo angleservoB.angle=seqB.update()time.sleep(0.02)# wait a bit, note this affects 'speed'

Spooky Eyes with Dual SSD1306 OLED displays

Displays are at same address, so code can be much simpler

[Note: as of CircuitPython 7, this only works on QTPy RP2040, not QTPy M0 or QTPY M0 Haxpress]

importtimeimportboardimportrandomimportadafruit_ssd1306# requires: adafruit_bus_device, adafruit_framebufi2c=board.I2C()oled=adafruit_ssd1306.SSD1306_I2C(width=128,height=64,i2c=i2c)i=0;inc=30whileTrue:oled.fill(0)fordin (31,30,14,12,10,8):# various diametersoled.circle(64+i,32,d,1)i=random.randint(-30,30)oled.show()time.sleep(0.1+random.random() )

Use Capsense as Proximity Detector to Make Spooopy Ghost

Computing the difference between current touch raw value anda baseline minimumprovides a kind of proximity detector, if your antenna is big enough.

importtimeimportboardimportdigitalioimporttouchiofrompulseioimportPWMOutfromadafruit_motorimportservotouchA=touchio.TouchIn(board.A2)servoA=servo.Servo(PWMOut(board.RX,duty_cycle=2**15,frequency=50))touch_min=touchA.raw_value# baseline for proximityservo_pos_last=160whileTrue:# get proximity value, set within servo bounds (30-160)proximity_val= (touchA.raw_value-touch_min)servo_pos=160-min(160,max(30,proximity_val))servo_pos_last+= (servo_pos-servo_pos_last)*0.01# easing/smoothingservoA.angle=servo_pos_last

Get Size of Device's Flash Disk

seeos.statvfs() docs

importosprint("\nHello World!")fs_stat=os.statvfs('/')print("Disk size in MB",fs_stat[0]*fs_stat[2]/1024/1024)print("Free space in MB",fs_stat[0]*fs_stat[3]/1024/1024)whileTrue:pass

Capsense Touch Sensor to USB keyboard

importtimeimportboardimportneopixelimporttouchioimportusb_hidfromadafruit_hid.keyboardimportKeyboardfromadafruit_hid.keycodeimportKeycodetouchA=touchio.TouchIn(board.A0)pixel=neopixel.NeoPixel(board.NEOPIXEL,1,brightness=0.2,auto_write=False)pixel[0]= (0,0,0)time.sleep(1)# Sleep for a bit to avoid a race condition on some systemskbd=Keyboard(usb_hid.devices)whileTrue:iftouchA.value:print("A press")pixel[0]= (255,0,0)kbd.send(Keycode.RIGHT_ARROW)pixel.show()pixel[0]= (0,0,0)time.sleep(0.2)

Really Helpful CircuitPython Links

About

Some tips and tricks for CircuitPython, using a QT Py board

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp