We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fb64fb9 commit d66a614Copy full SHA for d66a614
m5stack/libs/unit/hbridge.py
@@ -75,6 +75,19 @@ def set_16bit_pwm(self, duty=0):
75
"""
76
self.write_mem_list(PWM16BIT_REG, [(duty & 0xFF), ((duty >> 8) & 0xFF)])
77
78
+ def set_percentage_pwm(self, duty=0, res=8):
79
+ """
80
+ set 8bit or 16bit pwm dutycycle
81
+ duty : 0 to 100%
82
+ resolution: 8 or 16 bit
83
84
+ duty = max(min(duty, 100), 0)
85
+ if res == 8:
86
+ duty = self.map(duty, 0, 100, 0, 255)
87
+ else:
88
+ duty = self.map(duty, 0, 100, 0, 65535)
89
+ self.write_mem_list(PWM8BIT_REG, [duty])
90
+
91
def set_pwm_freq(self, freq=0):
92
93
set direction
@@ -127,6 +140,9 @@ def write_mem_list(self, reg, data):
127
140
def read_reg(self, reg, num):
128
141
return self.hbridge_i2c.readfrom_mem(self.i2c_addr, reg, num)
129
142
143
+ def map(self, x, in_min, in_max, out_min, out_max):
144
+ return round((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
145
130
146
def deinit(self):
131
147
pass
132
148
0 commit comments