Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Problem: I2C volume control of max9744
#2
So, I don't really know what I'm doing, but I seem to have kludged something together that works Smile. I literally guessed the php syntax based on the nearby rows lol
Maybe someone can let me know if there's an error or something with this? Will it bite me later?

Also, what on earth does vol.sh do?? Same with rotvol.sh as I tried changing those and nothing happened!

It seems mpc volume is now pegged at 100, and changing the volume on the web interface now sets my max9744 amp gain to the "volume" shown on the Moode webpage.

I started by running this
Code:
grep -rnw '/var/' -e 'setvol'

from here to find playback.php having a "setvol" command (AI gave me the idea to try this) 
I then added the sysCmd('python3... as shown below, and changed the sendMpdCmd to set volume to 100 every time. The "SetAmpVolume.py" was written by AI and is posted below
Code:
if ($_SESSION['mpdmixer'] == 'hardware') {
                       sysCmd('amixer -M -c ' . $_SESSION['cardnum'] . ' sset "' . $_SESSION['amixname'] . '" ' . $_POST['volknob'] . '%' );
               } else {
                       sysCmd('python3 /home/pi/SetAmpVolume.py ' . $_POST['volknob']);
                       sendMpdCmd($sock, 'setvol 100');
                       //sendMpdCmd($sock, 'setvol ' . $_POST['volknob']);
                       $resp = readMpdResp($sock);
               }

Code:
import smbus
import sys

# Initialize I2C bus
bus = smbus.SMBus(1)
MAX9744_ADDR = 0x4B  # I2C address of MAX9744

def set_volume(volume):
   """Set volume level on MAX9744 (0-63)"""
   if volume < 0:
       volume = 0
   elif volume > 63:
       volume = 63
   try:
       bus.write_byte(MAX9744_ADDR, volume)
       return True
   except:
       print("Error communicating with MAX9744")
       return False

# Check if volume was provided as command line argument
if len(sys.argv) > 1:
   try:
       volume = int(sys.argv[1])
       if 0 <= volume <= 63:
           if set_volume(volume):
               print(f"Volume set to {volume}")
           else:
               print("Failed to set volume")
       else:
           print("Volume must be between 0 and 63")
   except ValueError:
       print("Please provide a valid number between 0 and 63")
else:
   # Get volume input from user if no command line argument
   while True:
       try:
           volume = input("Enter volume level (0-63) or 'x' to exit: ")
           if volume.lower() == 'x':
               sys.exit()
           volume = int(volume)
           if 0 <= volume <= 63:
               if set_volume(volume):
                   print(f"Volume set to {volume}")
                   break
               else:
                   print("Failed to set volume")
           else:
               print("Volume must be between 0 and 63")
       except ValueError:
           print("Please enter a valid number")
Reply


Messages In This Thread
I2C volume control of max9744 - by Mechoption - 11-28-2024, 12:37 AM
RE: I2C volume control of max9744 - by Mechoption - 12-03-2024, 04:36 AM
RE: I2C volume control of max9744 - by Mechoption - 12-03-2024, 11:29 PM

Forum Jump: