Converting InputPort code to Agent/Device

Working with a project to control some LEDs. We have been working with a Hannah and are now attempting to work with an April and also use Agents.

The code we have uses classes that interface with the old style httpin through an input port.
I’m getting caught with trying to convert this code to disregard the input port and receive that data through an agent.on request.

Any ideas?

`// April controlling RGB LED on pins 7-9

/* Pin Assignments

  • Pin 9 = PWM for Red
  • Pin 8 = PWM for Green
  • Pin 7 = PWM for Blue
    */

// Configure Hardware

hardware.pin9.configure(PWM_OUT, 1.0/500.0, 1.0); // Red
hardware.pin8.configure(PWM_OUT, 1.0/500.0, 1.0); // Green
hardware.pin7.configure(PWM_OUT, 1.0/500.0, 1.0); // Blue

server.log(“Hardware Configured”);

// LED drivers

class ledBrightness extends InputPort
{
name = "LED Brightness"
pin = null
type = “float”

// define a constructor so that we can construct seperate instances for Red, Green, and Blue LEDs
constructor(name, pin) {
    // call through to the base (InputPort) constructor with the provided name
    base.constructor(name)
    this.pin = pin
    // no need to configure the pins as we've already done it at global scope
}

function set(value) {
    // output pin is configured for PWM, we can set the duty cycle with pin.write()
    // write a floating point number between 0.0 and 1.0, where 1.0 = 100% duty cycle
    server.log(format("%s set to %.2f", name, value));
    this.pin.write(1.0-value);
}

}

// RGB LED Class
class RgbLed
{
// IO Pin assignments
pinR = null;
pinG = null;
pinB = null;

constructor(r, g, b)
{

    // Save pin assignments
    pinR = r;
    pinG = g;
    pinB = b;
    
    // Initialise as inactive
    setLevels(0, 0, 0);

// setPin(pinR, 0);
// setPin(pinG, 0);
// setPin(pinB, 0);
}

// Set red, green and blue intensity levels

function setLevels(r, g, b)
{
            // output pin is configured for PWM, we can set the duty cycle with pin.write()
    // write a floating point number between 0.0 and 1.0, where 1.0 = 100% duty cycle
    server.log(format("RGB led set to %.2f, %.2f, %.2f", r, g, b));
    this.pinR.write(r/255.0);
    this.pinG.write(g/255.0);
    this.pinB.write(b/255.0);
    
}

}
// Web input in the form of hex string

// Convert hex string to an integer
function hexToInteger(hex)
{
local result = 0;
local shift = hex.len() * 4;

// For each digit..
for(local d=0; d<hex.len(); d++)
{
    local digit;

    // Convert from ASCII Hex to integer
    if(hex[d] >= 0x61)
        digit = hex[d] - 0x57;
    else if(hex[d] >= 0x41)
         digit = hex[d] - 0x37;
    else
         digit = hex[d] - 0x30;

    // Accumulate digit
    shift -= 4;
    result += digit << shift;
}

return result;

}

// Input port to accept color values
class LedInput extends InputPort
{
name = “Color”;
type = “string”;

led = null;

// Construct an LED and enable it
constructor()
{
    base.constructor();
    led = RgbLed(hardware.pin9, hardware.pin8, hardware.pin7);
    led.setLevels(255.0, 255.0, 255.0);
}

// Use received colors in HTML Hex format to set the LED
function set(col)
{
    server.log(typeof col);
    server.log(col);
    local r = hexToInteger(col.slice(0,2));
    local g = hexToInteger(col.slice(2,4));
    local b = hexToInteger(col.slice(4,6));
    led.setLevels(r, g, b);
    //server.show(col);
}

}

// Register with the server
imp.configure(“April RGB Webcolor”, [ LedInput() ], []);/code>