HI, I am new to Blynk and have had some success. But,
I need help with one last detail to perfect my first hardware controller.
• Hardware model - RPi 3b+ -> I2C -> DAC_IC
• Smartphone OS - iOS 14.2.1
• Local Blynk server
• Blynk Library version - Python 0.2.6
I have two ways to control the music volume coming from the DAC.
- a volume slider in Blynk - works fine
@blynk.handle_event('write V6') # volume def write_virtual_pin_handler(pin, value): global volume try: newvalue6 = (value[-1]) volume = int(newvalue6) print(newvalue6) bus.write_byte_data(0x48, 0x17, volume) # volume register except IOError as err: print(err)
- a rotary volume encoder on the front of the music player - here is the code to read the encoder though the details are not important. What is important is that both means of control send values to the same IC register (0x17). BUT, these bits of code operate in different threads for efficiency and the global variables don’t seem to cross the process boundary.
try: counter = 0 clkState = GPIO.input(clk) dtState = GPIO.input(dt) if clkState != clkLastState: if counter == 0: volume = int(bus.read_byte_data(0x48, 0x17)) #read volume from the DAC chip if clkState != dtState: counter += 2 else: counter -= 2 volume = volume + counter # raise or lower volume by the 'counter' amount if volume >= 126: volume = 127 if volume <= 1: volume = 0 clkLastState = clkState print(volume) bus.write_byte_data(0x48, 0x17, volume) # write the new value into DAC memory time.sleep(0.01) #### Insert volume synchronizer here to adjust the 'volume' slider (v.pin 6) #### except IOError as err: print("I/O error on chipvol =", err) except: print('chipvol error')
I need the Blynk server to adjust the slider when the front rotary encoder changes the volume (an integer between 0-127). Code in the second block (spot is marked) to write to virtual pin 6 and to synchronize the pin doesn’t have any effect. This is only bad when the slider position is much higher than the encoder. Then the volume jumps up when you touch the slider. That’s potentially dangerous if the source is loud.
Can you give me some ideas and especially any syntax to try?
Many thanks in advance!