Vibrator HAT
Support the following products:
Micropython Example:
1import os, sys, io 2import M5 3from M5 import * 4from hat import VibratorHat 5from hardware import * 6 7 8title0 = None 9freq_label = None 10duty_label = None 11label0 = None 12label1 = None 13label2 = None 14line0 = None 15line1 = None 16line2 = None 17hat_vibrator_0 = None 18 19 20import math 21 22width = None 23duty_w = None 24duty = None 25freq = None 26 27 28# Describe this function... 29def draw_pwm(): 30 global \ 31 width, \ 32 duty_w, \ 33 duty, \ 34 freq, \ 35 title0, \ 36 freq_label, \ 37 duty_label, \ 38 label0, \ 39 label1, \ 40 label2, \ 41 line0, \ 42 line1, \ 43 line2, \ 44 hat_vibrator_0 45 if duty == 0: 46 width = 55 - (freq - 10) 47 print(width) 48 line0.setPoints(x0=(67 - width), y0=160, x1=67, y1=160) 49 line2.setPoints(x0=67, y0=160, x1=67, y1=160) 50 line1.setPoints(x0=67, y0=160, x1=(67 + width), y1=160) 51 else: 52 width = 55 - (freq - 10) 53 duty_w = math.ceil((width + width) * (duty / 100)) 54 line0.setPoints(x0=12, y0=130, x1=(12 + duty_w), y1=130) 55 line2.setPoints(x0=(12 + duty_w), y0=130, x1=(12 + duty_w), y1=160) 56 line1.setPoints( 57 x0=(12 + duty_w), y0=160, x1=((12 + duty_w) + ((width + width) - duty_w)), y1=160 58 ) 59 60 61def setup(): 62 global \ 63 title0, \ 64 freq_label, \ 65 duty_label, \ 66 label0, \ 67 label1, \ 68 label2, \ 69 line0, \ 70 line1, \ 71 line2, \ 72 hat_vibrator_0, \ 73 width, \ 74 duty_w, \ 75 duty, \ 76 freq 77 78 M5.begin() 79 Widgets.fillScreen(0xFFFFFF) 80 title0 = Widgets.Title("VIBRATOR", 18, 0xFFFFFF, 0xF5A41D, Widgets.FONTS.DejaVu18) 81 freq_label = Widgets.Label("Freq: 00", 2, 32, 1.0, 0xF5A41D, 0xFFFFFF, Widgets.FONTS.DejaVu18) 82 duty_label = Widgets.Label("Duty: 00", 2, 64, 1.0, 0xF5A41D, 0xFFFFFF, Widgets.FONTS.DejaVu18) 83 label0 = Widgets.Label( 84 "Long Press Turn OFF", 18, 192, 1.0, 0xF5A41D, 0xFFFFFF, Widgets.FONTS.DejaVu9 85 ) 86 label1 = Widgets.Label("Duty", 84, 100, 1.0, 0xF5A41D, 0xFFFFFF, Widgets.FONTS.DejaVu18) 87 label2 = Widgets.Label("Freq", 47, 213, 1.0, 0xF5A41D, 0xFFFFFF, Widgets.FONTS.DejaVu18) 88 line0 = Widgets.Line(12, 130, 67, 130, 0xF5A41D) 89 line1 = Widgets.Line(67, 160, 122, 160, 0xF5A41D) 90 line2 = Widgets.Line(67, 130, 67, 160, 0xF5A41D) 91 92 hat_vibrator_0 = VibratorHat(port=(26, 0)) 93 freq = 10 94 duty = 0 95 freq_label.setText(str((str("Freq: ") + str(freq)))) 96 duty_label.setText(str((str("Duty: ") + str(duty)))) 97 draw_pwm() 98 hat_vibrator_0.once(freq=10, duty=50, duration=50) 99 100 101def loop(): 102 global \ 103 title0, \ 104 freq_label, \ 105 duty_label, \ 106 label0, \ 107 label1, \ 108 label2, \ 109 line0, \ 110 line1, \ 111 line2, \ 112 hat_vibrator_0, \ 113 width, \ 114 duty_w, \ 115 duty, \ 116 freq 117 M5.update() 118 if BtnA.wasClicked(): 119 freq = freq + 1 120 if freq > 55: 121 freq = 10 122 freq_label.setText(str((str("Freq: ") + str(freq)))) 123 draw_pwm() 124 hat_vibrator_0.set_freq(freq) 125 if BtnB.wasClicked(): 126 duty = duty + 1 127 duty = duty % 100 128 duty_label.setText(str((str("Duty: ") + str(duty)))) 129 draw_pwm() 130 hat_vibrator_0.set_duty(duty) 131 if BtnA.isHolding(): 132 duty = 0 133 draw_pwm() 134 hat_vibrator_0.turn_off() 135 136 137if __name__ == "__main__": 138 try: 139 setup() 140 while True: 141 loop() 142 except (Exception, KeyboardInterrupt) as e: 143 try: 144 from utility import print_error_msg 145 146 print_error_msg(e) 147 except ImportError: 148 print("please update to latest firmware")
UIFLOW2 Example:
class VibratorHAT
Constructors
- class VibratorHAT
Create an VibratorHAT object.
UIFLOW2:
Methods
- VibratorHAT.once(freq=10, duty=50, duration=50) None
Play the haptic effect once on the motor.
- Parameters:
UIFLOW2:
- VibratorHAT.set_freq(freq)
Set the vibration frequency.
- Parameters:
freq (int) – The frequency of vibration ranges from 10-55Hz.
UIFLOW2:
