From 08b1671d7421d474b48b1ced59d8a999afe9c557 Mon Sep 17 00:00:00 2001 From: "kevin.larke" Date: Mon, 18 Nov 2019 09:37:45 -0500 Subject: [PATCH] picadae/app/picadae_api.py, picadae_shell.py : Updated PWM settters and getters. --- control/app/picadae_api.py | 29 +++++++++++++++++++++-------- control/app/picadae_shell.py | 4 +++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/control/app/picadae_api.py b/control/app/picadae_api.py index e6620dd..756c4fb 100644 --- a/control/app/picadae_api.py +++ b/control/app/picadae_api.py @@ -174,14 +174,14 @@ class Picadae: return result - def write( self, i2c_addr, reg_addr, byteL ): + def write_tiny_reg( self, i2c_addr, reg_addr, byteL ): return self._send( 'w', i2c_addr, reg_addr, [ len(byteL) ] + byteL ) def call_op( self, midi_pitch, op_code, argL ): - return self.write( self._pitch_to_i2c_addr( midi_pitch ), op_code, argL ) + return self.write_tiny_reg( self._pitch_to_i2c_addr( midi_pitch ), op_code, argL ) def set_read_addr( self, i2c_addr, mem_id, addr ): - return self. write(i2c_addr, TinyOp.setReadAddr.value,[ mem_id, addr ]) + return self.write_tiny_reg(i2c_addr, TinyOp.setReadAddr.value,[ mem_id, addr ]) def read_request( self, i2c_addr, reg_addr, byteOutN ): return self._send( 'r', i2c_addr, reg_addr,[ byteOutN ] ) @@ -253,12 +253,23 @@ class Picadae: byteOutN = 2 return self.block_on_picadae_read( midi_pitch, TinyConst.kRdTableSrcId.value, midi_vel*2, byteOutN, time_out_ms ) - def set_pwm( self, midi_pitch, duty_cycle_pct ): - return self.call_op( midi_pitch, TinyOp.setPwmOp.value, [ int( duty_cycle_pct * 255.0 /100.0 )]) + def set_pwm_duty( self, midi_pitch, duty_cycle_pct ): + if 0 <= duty_cycle_pct and duty_cycle_pct <= 100: + duty_cycle_pct = 100.0 - duty_cycle_pct + return self.call_op( midi_pitch, TinyOp.setPwmOp.value, [ int( duty_cycle_pct * 255.0 /100.0 )]) + else: + return Result(msg="Duty cycle (%f) out of range 0-100." % (duty_cycle_pct)) - def get_pwm( self, midi_pitch, time_out_ms=250 ): + def get_pwm_duty( self, midi_pitch, time_out_ms=250 ): return self.block_on_picadae_read_reg( midi_pitch, TinyRegAddr.kPwmDutyAddr.value, time_out_ms=time_out_ms ) + def set_pwm_freq( self, midi_pitch, freq ): + res = self.get_pwm_duty( midi_pitch ) + if res: + print("duty",int(res.value[0])) + res = self.call_op( midi_pitch, TinyOp.setPwmOp.value, [ int(res.value[0]), int(freq) ]) + return res + def get_pwm_freq( self, midi_pitch, time_out_ms=250 ): return self.block_on_picadae_read_reg( midi_pitch, TinyRegAddr.kPwmFreqAddr.value, time_out_ms=time_out_ms ) @@ -296,12 +307,14 @@ class Picadae: coarse_usec = self.prescaler_usec*255 # usec's in one coarse tick coarse = int( usec / coarse_usec ) - fine = int((usec - coarse*coarse_usec) / self.prescaler_usec) + fine = int(round((usec - coarse*coarse_usec) / self.prescaler_usec)) assert( coarse <= 255 ) assert( fine <= 255) - print("C:%i F:%i : %i " % (coarse,fine, coarse*coarse_usec + fine*self.prescaler_usec )) + x = coarse*coarse_usec + fine*self.prescaler_usec + + print("C:%i F:%i : %i %i (%i)" % (coarse,fine, x, usec, usec-x )) return coarse,fine diff --git a/control/app/picadae_shell.py b/control/app/picadae_shell.py index 1ea4ebc..0202031 100644 --- a/control/app/picadae_shell.py +++ b/control/app/picadae_shell.py @@ -16,9 +16,11 @@ class PicadaeShell: 'o':{ "func":"note_off", "minN":1, "maxN":1, "help":"note-off "}, 'T':{ "func":"set_vel_map", "minN":3, "maxN":3, "help":"table "}, 't':{ "func":"get_vel_map", "minN":2, "maxN":2, "help":"table "}, - 'D':{ "func":"set_pwm", "minN":2, "maxN":4, "help":"duty { {
}} div:2=2,3=4,4=8,5=16,6=32,7=64,8=128,9=256,(10)=512 32us, 11=1024,12=2048,13=4096,14=8192,15=16384" }, + 'D':{ "func":"set_pwm_duty", "minN":2, "maxN":4, "help":"duty { {
}} " }, 'd':{ "func":"get_pwm_duty", "minN":1, "maxN":1, "help":"duty "}, + 'F':{ "func":"set_pwm_freq", "minN":2, "maxN":2, "help":"freq 254=~123Hz"}, 'f':{ "func":"get_pwm_freq", "minN":1, "maxN":1, "help":"freq "}, + 'I':{ "func":"set_pwm_div", "minN":2, "maxN":2, "help":"div
div:2=2,3=4,4=8,5=16,6=32,7=64,8=128,9=256,(10)=512 32us, 11=1024,12=2048,13=4096,14=8192,15=16384"}, 'i':{ "func":"get_pwm_div", "minN":1, "maxN":1, "help":"div "}, 'W':{ "func":"write_table", "minN":1, "maxN":1, "help":"write_table "}, 'N':{ "func":"make_note", "minN":3, "maxN":3, "help":"note "},