Python Bindings

The Python bindings allow to modify the computer lights by using other programs signals. Some applications can be to modify the colors when receiving an email, when monitoring things like the weather or the CPU temperature.

Examples

Testing all the commands

This is the example that I use to test the bindings. It should be clear enough to explain all the commands !


#!/usr/bin/python3
#

import time
from AKBL.Bindings import Bindings
 
AKBLConnection=Bindings()
 
lights_test=True
profiles_test=True
colors_test=True
speed_test=True
colors_multiple_test=True


if not AKBLConnection.ping():
    print("The connection with the daemon is off")
    exit()

  
 """
     Each command is called as:
     
         print( <command_name>, <command> )
         
     To check if the commands succeed. You don't 
     really need to do this in your code!
 """

 if lights_test:
     print('lights off', AKBLConnection.set_lights(False))
     time.sleep(2)
     print('lights on', AKBLConnection.set_lights(True))
     time.sleep(2)
     print('switch lights', AKBLConnection.switch_lights())


 if profiles_test:
     for profile_name in AKBLConnection.get_profile_names():
         print('set profile:', profile_name, AKBLConnection.set_profile(profile_name))
         time.sleep(5)


 color1='#F7F200'
 color2='#0018FF'

 if colors_test:
     print('set_colors blink', AKBLConnection.set_colors('blink', 100, color2))
     time.sleep(5)
     print('set_colors fixed', AKBLConnection.set_colors('fixed', 100, color1))
     time.sleep(5)
     print('set_colors morph', AKBLConnection.set_colors('morph', 100, color1, color2))

      
 if speed_test:
     print('set_colors blink', AKBLConnection.set_colors('blink', 1, color2))
     time.sleep(5)
     print('set_colors blink', AKBLConnection.set_colors('blink', 100, color2))
     time.sleep(5)
     print('set_colors blink', AKBLConnection.set_colors('blink', 256, color2))
     time.sleep(5)

  
 if colors_multiple_test:
     colors1='#0600FF' 
     colors2='#FF00E5'
      
     print('set_colors multiple blink', AKBLConnection.set_colors('blink', 100, colors2))
     time.sleep(5)
     print('set_colors multiple morph', AKBLConnection.set_colors('morph', 100, colors1, colors2))
     time.sleep(5)
     print('set_colors mutlple fixed', AKBLConnection.set_colors('fixed', 100, colors1))
     
		

Changing the keyboard colors by checking the CPU Temperature

The following script will change the keyboard colors by checking the CPU Temperature. Before using the script, you should check in a terminal if you have the command sensors


#!/usr/bin/python3
#

import os
import time 
from AKBL.Bindings import Bindings

def get_max_temp():
    """
        Get the maximum temperature of the CPU by 
        using the bash commands "sensors"
    """
    output=os.popen('''sensors''')
    lines= output.readlines()
    
    max_temperature=0
    
    for line in lines:
        if '°C' in line:
            temp = line.split('+')[1]
            temp = temp.split('°')[0]
            temp = float(temp)
            
            if temp > max_temperature:
                max_temperature=temp
            
    return max_temperature      


if __name__ == '__main__':

    akbl=Bindings()

    if not akbl.ping():
        print("The akbl daemon is off.")
    else:
        while True:
            
            max_temperature=get_max_temp()
            print("The maximum temperature is", max_temperature)
            
            if max_temperature <= 0:
                akbl.set_colors('fixed', 100, '#000000') # black
            elif max_temperature <= 20:
                akbl.set_colors('fixed', 100, '#02EDFF') # cyan
            elif max_temperature <= 55:
                akbl.set_colors('fixed', 100, '#0000FF') # blue
            elif max_temperature <= 70:
                akbl.set_colors('fixed', 100, '#FFE900') # yellow
            elif max_temperature <= 85:
                akbl.set_colors('fixed', 100, '#FF7800') # orange
            elif max_temperature <= 95:
                akbl.set_colors('fixed', 100, '#FF0014') # red
            else:
                akbl.set_colors('blink', 100, '#FF0014') # red

            time.sleep(5) # seconds
		

Note that if you want to test the code, you can just create a fake temperature:


    else:
        max_temperature=0
        while True:
            #max_temperature=get_max_temp()
                    .
                    .
                    .
            max_temperature+=10
            time.sleep(5)
		

Changing the keyboard colors by checking the weather

I was curious to find if there was command line weather program, and it seems that inxi works fine :)


#!/usr/bin/python3
#

import os
import time 
from AKBL.Bindings import Bindings 

def get_max_temp():
    """
        Get weather temperature and make a linear
        scale: 40°C <==> 100
        
        (The linear scale is only to adapt this function
         to the previous script)
    """
    
    # get the temperature
    output=os.popen('''inxi -w''')
    line=output.read()      
    try:
        temp_str=line.split(' C')[0].split('(')[1]
        max_temperature=float(temp_str)
    except Exception as e:
        print(e)
        max_temperature=0
        
    # adapt it
    max_temperature=(max_temperature*10)/4

    return max_temperature    


if __name__ == '__main__':

    akbl=Bindings()

    if not akbl.ping():
        print("The akbl daemon is off.")
    else:
        while True:
            
            max_temperature=get_max_temp()
            print("The maximum temperature is", max_temperature)
            
            if max_temperature <= 0:
                akbl.set_colors('fixed', 100, '#000000') # black
            elif max_temperature <= 20:
                akbl.set_colors('fixed', 100, '#02EDFF') # cyan
            elif max_temperature <= 55:
                akbl.set_colors('fixed', 100, '#0000FF') # blue
            elif max_temperature <= 70:
                akbl.set_colors('fixed', 100, '#FFE900') # yellow
            elif max_temperature <= 85:
                akbl.set_colors('fixed', 100, '#FF7800') # orange
            elif max_temperature <= 95:
                akbl.set_colors('fixed', 100, '#FF0014') # red
            else:
                akbl.set_colors('blink', 100, '#FF0014') # red

            time.sleep(5) # seconds		
		

API



class AlienwareKBL():
		
	def reload_address(self):
	    """
	        It tries to make a connection with the Daemon
		and it returns True or False.
	    """
	
	def ping(self):
	    """
	        It checks if the Daemon is connected
	        and it returns True or False.
	    """ 
	
	def get_address(self):
	    """
	        It returns the current URI of the Daemon.
	    """
	
	def get_profile_names(self):
	    """
	        It returns a list of the existing profile names.
	    """
	    
	def set_profile(self, profile):
	    """
	        Set a profile from the existing profiles.
	        
	        + 'profile' is the profile name.
	    """
	    
	def switch_lights(self):
           """
	        Toggle on/off the lights of the keyboard.
           """
	
	def set_lights(self, state):
	    """
	        Turn the lights on or off.
	        
	        + 'state' can be a boolean or a string
	    """
	    
	def set_colors(self, mode, speed, colors1, colors2=None):
	    """
	        Change the colors and the mode of the keyboard.
	        
                + The available modes are: 'fixed', 'morph' and 'blink',
                  'fixed' and 'blink' only take 'colors1'.
	            
	        + Speed must be an integer. 1 =< speed =< 256
	        
	        + colors1 and colors2 can be a single hex_color or a list
		  of hex_colors. If both arguments are used, they must
		  have the same number of items.
	    """