pca9632: Update to use LEDHelper class

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-03-22 23:29:57 -04:00
parent 0c425cf014
commit 271c2ac95b
1 changed files with 34 additions and 59 deletions

View File

@ -3,7 +3,9 @@
# Copyright (C) 2022 Ricardo Alcantara <ricardo@vulcanolabs.com> # Copyright (C) 2022 Ricardo Alcantara <ricardo@vulcanolabs.com>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
from .mcp4018 import SoftwareI2C from . import mcp4018
BACKGROUND_PRIORITY_CLOCK = 0x7fffffff00000000
# Register addresses # Register addresses
PCA9632_MODE1 = 0x00 PCA9632_MODE1 = 0x00
@ -22,70 +24,43 @@ PCA9632_WHT = 0x06
class PCA9632: class PCA9632:
def __init__(self, config): def __init__(self, config):
self.printer = config.get_printer() self.printer = printer = config.get_printer()
self.i2c = mcp4018.SoftwareI2C(config, 98)
self.i2c = SoftwareI2C(config, 98) self.prev_regs = {}
#i2c_addr = self.i2c.get_i2c_address() pled = printer.load_object(config, "led")
self.led_helper = pled.setup_helper(config, self.update_leds, 1, True)
name = config.get_name().split()[1] printer.register_event_handler("klippy:connect", self.handle_connect)
self.gcode = self.printer.lookup_object('gcode') def reg_write(self, reg, val, minclock=0):
self.gcode.register_mux_command("SET_LED","LED",name,self.set_led, if self.prev_regs.get(reg) == val:
desc="Set the color of an LED") return
self.prev_regs[reg] = val
self.led_red = config.getfloat("initial_RED",0.,0.,1.) self.i2c.i2c_write([reg, val], minclock=minclock,
self.led_green = config.getfloat("initial_GREEN",0.,0.,1.) reqclock=BACKGROUND_PRIORITY_CLOCK)
self.led_blue = config.getfloat("initial_BLUE",0.,0.,1.)
self.led_white = config.getfloat("initial_WHITE",0.,0.,1.)
config.get_printer().register_event_handler("klippy:connect",
self.handle_connect)
def handle_connect(self): def handle_connect(self):
self._init_PCA9632()
def _init_PCA9632(self):
#Configure MODE1 #Configure MODE1
self.i2c.i2c_write([PCA9632_MODE1,0x00]) self.reg_write(PCA9632_MODE1, 0x00)
#Configure MODE2 (DIMMING, INVERT, CHANGE ON STOP,TOTEM) #Configure MODE2 (DIMMING, INVERT, CHANGE ON STOP,TOTEM)
self.i2c.i2c_write([PCA9632_MODE2,0x15]) self.reg_write(PCA9632_MODE2, 0x15)
self.led_red = int(self.led_red * 255.) self.update_leds(self.led_helper.get_status()['color_data'], None)
self.led_blue = int(self.led_blue * 255.) def update_leds(self, led_state, print_time):
self.led_green = int(self.led_green * 255.) minclock = 0
self.led_white = int(self.led_white * 255.) if print_time is not None:
minclock = self.i2c.get_mcu().print_time_to_clock(print_time)
self.i2c.i2c_write([PCA9632_PWM0,self.led_red]) red, green, blue, white = [int(v * 255. + .5) for v in led_state[0]]
self.i2c.i2c_write([PCA9632_PWM1,self.led_blue]) self.reg_write(PCA9632_PWM0, red, minclock=minclock)
self.i2c.i2c_write([PCA9632_PWM2,self.led_green]) self.reg_write(PCA9632_PWM1, blue, minclock=minclock)
self.i2c.i2c_write([PCA9632_PWM3,self.led_white]) self.reg_write(PCA9632_PWM2, green, minclock=minclock)
self.reg_write(PCA9632_PWM3, white, minclock=minclock)
LEDOUT = (LED_PWM << PCA9632_RED if self.led_red else 0) LEDOUT = (LED_PWM << PCA9632_RED if red else 0)
LEDOUT |= (LED_PWM << PCA9632_GRN if self.led_green else 0) LEDOUT |= (LED_PWM << PCA9632_GRN if green else 0)
LEDOUT |= (LED_PWM << PCA9632_BLU if self.led_blue else 0) LEDOUT |= (LED_PWM << PCA9632_BLU if blue else 0)
LEDOUT |= (LED_PWM << PCA9632_WHT if self.led_white else 0) LEDOUT |= (LED_PWM << PCA9632_WHT if white else 0)
self.reg_write(PCA9632_LEDOUT, LEDOUT, minclock=minclock)
self.i2c.i2c_write([PCA9632_LEDOUT,LEDOUT]) def get_status(self, eventtime):
def set_led(self, gcmd): return self.led_helper.get_status(eventtime)
self.led_red = gcmd.get_float("RED",self.led_red,0.,1.)
self.led_green = gcmd.get_float("GREEN",self.led_green,0.,1.)
self.led_blue = gcmd.get_float("BLUE",self.led_blue,0.,1.)
self.led_white = gcmd.get_float("WHITE",self.led_white,0.,1.)
self.led_red = int(self.led_red * 255.)
self.led_blue = int(self.led_blue * 255.)
self.led_green = int(self.led_green * 255.)
self.led_white = int(self.led_white * 255.)
self.i2c.i2c_write([PCA9632_PWM0,self.led_red])
self.i2c.i2c_write([PCA9632_PWM1,self.led_blue])
self.i2c.i2c_write([PCA9632_PWM2,self.led_green])
self.i2c.i2c_write([PCA9632_PWM3,self.led_white])
LEDOUT = (LED_PWM << PCA9632_RED if self.led_red else 0)
LEDOUT |= (LED_PWM << PCA9632_GRN if self.led_green else 0)
LEDOUT |= (LED_PWM << PCA9632_BLU if self.led_blue else 0)
LEDOUT |= (LED_PWM << PCA9632_WHT if self.led_white else 0)
self.i2c.i2c_write([PCA9632_LEDOUT,LEDOUT])
def load_config_prefix(config): def load_config_prefix(config):
return PCA9632(config) return PCA9632(config)