Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // AMX Color Controller
- //I2C Addresses
- const i2c_ioexp = 0x7C;
- const i2c_temp = 0x98;
- const i2c_als = 0xE8;
- const i2c_accel = 0x38;
- //----------------------------------------
- //-- Configure I2C
- //----------------------------------------
- hardware.configure(I2C_89);
- local i2c = hardware.i2c89;
- local led_r = 0;
- local led_g = 0;
- local led_b = 0;
- local potLevel = 0.0;
- //----------------------------------------
- //-- IO Expander Functions
- //----------------------------------------
- local function ioexp_read(addr) {
- local result = i2c.read(i2c_ioexp, format("%c", addr), 1);
- if (result == null) {
- server.log("i2c read fail");
- return -1;
- } else return result[0];
- }
- local function ioexp_write(addr, data) {
- i2c.write(i2c_ioexp, format("%c%c",addr, data));
- }
- local function ioexp_writebit(addr, bitn, level) {
- // read modify write
- local reg = ioexp_read(addr);
- reg = (level==0)?(reg&~(1<<bitn)) : (reg | (1<<bitn));
- ioexp_write(addr, reg)
- }
- local function ioexp_modify_write(addr, data, mask) {
- local reg = ioexp_read(addr);
- reg = (reg & ~mask) | (data & mask);
- ioexp_write(addr, reg);
- }
- local function ioexp_setpin(gpio, level) {
- ioexp_writebit(gpio>=8?0x10:0x11, gpio&7, level?1:0);
- }
- local function ioexp_setdir(gpio, output) {
- ioexp_writebit(gpio>=8?0x0e:0x0f, gpio&7, output?0:1);
- }
- local function ioexp_setpullup(gpio, enable) {
- ioexp_writebit(gpio>=8?0x06:0x07, gpio&7, enable);
- }
- local function ioexp_setirqmask(gpio, enable) {
- ioexp_writebit(gpio>=8?0x12:0x13, gpio&7, enable);
- }
- local function ioexp_setirqedge(gpio, rising, falling) {
- local addr = 0x17 - (gpio>>2);
- local mask = 0x03 << ((gpio&3)<<1);
- local data = (2*falling + rising) << ((gpio&3)<<1);
- ioexp_modify_write(addr, data, mask);
- }
- local function ioexp_clearirq(gpio) {
- ioexp_writebit(gpio>=8?0x18:0x19, gpio&7, 1);
- }
- local function ioexp_readpin(gpio) {
- return (ioexp_read(gpio>=8?0x10:0x11)&(1<<(gpio&7)))?1:0;
- }
- local function ioexp_setled(gpio, led) {
- ioexp_writebit(gpio>=8?0x20:0x21, gpio&7, led);
- }
- local function ioexp_update_leds(r,g,b) {
- if(r != null)
- led_r = r;
- if(g != null)
- led_g = g;
- if(b != null)
- led_b = b;
- ioexp_write(0x3b, led_g);
- ioexp_write(0x40, led_b);
- ioexp_write(0x45, led_r);
- }
- //----------------------------------------
- //-- Custom Classes
- //----------------------------------------
- // Custom color class
- class color extends InputPort
- {
- function set(v)
- {
- // Update the color at 33%
- if(v.tostring().toupper().find("HEX-") > -1)
- {
- local rgb = Hex2RGB(v.slice(4));
- ioexp_update_leds(rgb[0] * potLevel, rgb[1] * potLevel, rgb[2] * potLevel);
- }
- else if(v.tostring().toupper().find("RGB-") > -1)
- {
- local vString = v.tostring().toupper();
- local rgb = split(vString.slice(4),",");
- ioexp_update_leds(rgb[0].tointeger() * potLevel, rgb[1].tointeger() * potLevel, rgb[2].tointeger() * potLevel);
- }
- }
- }
- //----------------------------------------
- //-- Custom Functions
- //----------------------------------------
- function Hex2RGB(v)
- {
- local vString = v.tostring().toupper();
- local hexChars = "0123456789ABCDEF";
- // Make sure it's 6 digits long
- if(vString.len() == 6)
- {
- // Return the new colors
- return [ (hexChars.find(vString[0].tochar())*16) + hexChars.find(vString[1].tochar()),
- (hexChars.find(vString[2].tochar())*16) + hexChars.find(vString[3].tochar()),
- (hexChars.find(vString[4].tochar())*16) + hexChars.find(vString[5].tochar()) ];
- }
- // Just return the existing LED colors
- return [led_r, led_g, led_b];
- }
- function RGB2Hex(r,g,b)
- {
- return format(r,"%02X") + format(g,"%02X") + format(b,"%02X");
- }
- function ReadPot()
- {
- imp.wakeup(0.2,ReadPot);
- // Read the pot, scale to 0.0 - 1.0
- local pot = hardware.pin2.read() / 65520.0;
- if(math.fabs(pot - potLevel) > 0.005)
- {
- // Update soft copy
- potLevel = pot;
- ioexp_update_leds(led_r * potLevel, led_g * potLevel, led_b * potLevel);
- }
- }
- //LED Driver Enable
- ioexp_modify_write(0x01, 0xE0, 0xFF);
- ioexp_modify_write(0x0f, 0xE0, 0x00);
- ioexp_modify_write(0x0b, 0xE0, 0xFF);
- ioexp_modify_write(0x21, 0xE0, 0xFF);
- ioexp_write(0x1e, 0x50);
- ioexp_write(0x1f, 0x10);
- ioexp_update_leds(0,0,0);
- ioexp_setpin(5, 0);
- ioexp_setpin(6, 0);
- ioexp_setpin(7, 0);
- // Enable Rotary
- ioexp_setpin(8, 0);
- ioexp_setdir(8, 1);
- hardware.pin2.configure(ANALOG_IN);
- // Turn off the LED
- ioexp_update_leds(0,0,0);
- // Initiate
- imp.configure("Hannah / AMX Color", [color("Color","string")], []);
- potLevel = 1;
- server.show("");
- //ReadPot();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement