Quellcode durchsuchen

picadae/app/picadae_api.py, picadae_shell.py : Updated PWM settters and getters.

master
kevin.larke vor 4 Jahren
Ursprung
Commit
08b1671d74
2 geänderte Dateien mit 24 neuen und 9 gelöschten Zeilen
  1. 21
    8
      control/app/picadae_api.py
  2. 3
    1
      control/app/picadae_shell.py

+ 21
- 8
control/app/picadae_api.py Datei anzeigen

@@ -174,14 +174,14 @@ class Picadae:
174 174
 
175 175
         return result
176 176
         
177
-    def write( self, i2c_addr, reg_addr, byteL ):
177
+    def write_tiny_reg( self, i2c_addr, reg_addr, byteL ):
178 178
         return self._send( 'w', i2c_addr, reg_addr, [ len(byteL) ] + byteL )
179 179
 
180 180
     def call_op( self, midi_pitch, op_code, argL ):
181
-        return self.write( self._pitch_to_i2c_addr( midi_pitch ), op_code, argL )                          
181
+        return self.write_tiny_reg( self._pitch_to_i2c_addr( midi_pitch ), op_code, argL )                          
182 182
 
183 183
     def set_read_addr( self, i2c_addr, mem_id, addr ):
184
-        return self. write(i2c_addr, TinyOp.setReadAddr.value,[ mem_id, addr ])
184
+        return self.write_tiny_reg(i2c_addr, TinyOp.setReadAddr.value,[ mem_id, addr ])
185 185
                 
186 186
     def read_request( self, i2c_addr, reg_addr, byteOutN ):
187 187
         return self._send( 'r', i2c_addr, reg_addr,[ byteOutN ] )
@@ -253,12 +253,23 @@ class Picadae:
253 253
         byteOutN = 2
254 254
         return self.block_on_picadae_read( midi_pitch, TinyConst.kRdTableSrcId.value, midi_vel*2, byteOutN, time_out_ms )
255 255
 
256
-    def set_pwm( self, midi_pitch, duty_cycle_pct ):
257
-        return self.call_op( midi_pitch, TinyOp.setPwmOp.value, [ int( duty_cycle_pct * 255.0 /100.0 )])
256
+    def set_pwm_duty( self, midi_pitch, duty_cycle_pct ):
257
+        if 0 <= duty_cycle_pct and duty_cycle_pct <= 100:
258
+            duty_cycle_pct = 100.0 - duty_cycle_pct
259
+            return self.call_op( midi_pitch, TinyOp.setPwmOp.value, [ int( duty_cycle_pct * 255.0 /100.0 )])
260
+        else:
261
+            return Result(msg="Duty cycle (%f) out of range 0-100." % (duty_cycle_pct))
258 262
 
259
-    def get_pwm( self, midi_pitch, time_out_ms=250 ):
263
+    def get_pwm_duty( self, midi_pitch, time_out_ms=250 ):
260 264
         return self.block_on_picadae_read_reg( midi_pitch, TinyRegAddr.kPwmDutyAddr.value, time_out_ms=time_out_ms )
261 265
     
266
+    def set_pwm_freq( self, midi_pitch, freq ):
267
+        res = self.get_pwm_duty( midi_pitch )
268
+        if res:
269
+            print("duty",int(res.value[0]))
270
+            res = self.call_op( midi_pitch, TinyOp.setPwmOp.value, [ int(res.value[0]), int(freq) ])
271
+        return res
272
+    
262 273
     def get_pwm_freq( self, midi_pitch, time_out_ms=250 ):
263 274
         return self.block_on_picadae_read_reg( midi_pitch, TinyRegAddr.kPwmFreqAddr.value, time_out_ms=time_out_ms )
264 275
 
@@ -296,12 +307,14 @@ class Picadae:
296 307
         coarse_usec = self.prescaler_usec*255 # usec's in one coarse tick
297 308
         
298 309
         coarse = int( usec / coarse_usec )
299
-        fine   = int((usec - coarse*coarse_usec) / self.prescaler_usec)
310
+        fine   = int(round((usec - coarse*coarse_usec) / self.prescaler_usec))
300 311
 
301 312
         assert( coarse <= 255 )
302 313
         assert( fine <= 255)
303 314
 
304
-        print("C:%i F:%i : %i " % (coarse,fine, coarse*coarse_usec + fine*self.prescaler_usec ))
315
+        x = coarse*coarse_usec + fine*self.prescaler_usec
316
+        
317
+        print("C:%i F:%i : %i %i (%i)" % (coarse,fine, x, usec, usec-x ))
305 318
 
306 319
         return coarse,fine
307 320
 

+ 3
- 1
control/app/picadae_shell.py Datei anzeigen

@@ -16,9 +16,11 @@ class PicadaeShell:
16 16
             'o':{ "func":"note_off",     "minN":1,  "maxN":1, "help":"note-off <pitch>"},
17 17
             'T':{ "func":"set_vel_map",  "minN":3,  "maxN":3, "help":"table <pitch> <vel> <usec>"},
18 18
             't':{ "func":"get_vel_map",  "minN":2,  "maxN":2, "help":"table <pitch> <vel>"},
19
-            'D':{ "func":"set_pwm",      "minN":2,  "maxN":4, "help":"duty <pitch> <percent> {<hz> {<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" },
19
+            'D':{ "func":"set_pwm_duty", "minN":2,  "maxN":4, "help":"duty <pitch> <percent> {<hz> {<div>}} " },
20 20
             'd':{ "func":"get_pwm_duty", "minN":1,  "maxN":1, "help":"duty <pitch>"},
21
+            'F':{ "func":"set_pwm_freq", "minN":2,  "maxN":2, "help":"freq <pitch> <hz> 254=~123Hz"},
21 22
             'f':{ "func":"get_pwm_freq", "minN":1,  "maxN":1, "help":"freq <pitch>"},
23
+            'I':{ "func":"set_pwm_div",  "minN":2,  "maxN":2, "help":"div <pitch> <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"},
22 24
             'i':{ "func":"get_pwm_div",  "minN":1,  "maxN":1, "help":"div <pitch>"},
23 25
             'W':{ "func":"write_table",  "minN":1,  "maxN":1, "help":"write_table <pitch>"},
24 26
             'N':{ "func":"make_note",    "minN":3,  "maxN":3, "help":"note <pitch> <atkUs> <durMs>"},

Laden…
Abbrechen
Speichern