HomeSpan - How do I not use the "new" keyword so I can refer to the classes?

HomeSpan - Led Strip
Hey, I know that this is the correct way of doing this:

SPAN_ACCESSORY("2RGB strip 1");
    new EffectClass(13, 120); 

...but I need to pass the class to another one. What I'm trying to achieve is this:
Neopixel_RGB get the HSV values from the HomeKit App.
That is passed to EffectClass that sends the info to the RGB strip with effects (EffectClass gets the HSV values, but I care only about brightness, since that defines the effect - 100% solid, 80% flashing, ...).

This is my current implemetation. Can I get help please? When I use this code it is displayed in the app as "not supported".

main.ino

#if defined(CONFIG_IDF_TARGET_ESP32)

  #define NEOPIXEL_RGB_PIN       13
  #define NEOPIXEL_RGBW_PIN      32
  #define DOTSTAR_DATA_PIN       33
  #define DOTSTAR_CLOCK_PIN      27
  #define DEVICE_SUFFIX          ""

#elif defined(CONFIG_IDF_TARGET_ESP32S2)

  #define NEOPIXEL_RGB_PIN       17
  #define NEOPIXEL_RGBW_PIN      38
  #define DOTSTAR_DATA_PIN       3
  #define DOTSTAR_CLOCK_PIN      7
  #define DEVICE_SUFFIX          "-S2"

#elif defined(CONFIG_IDF_TARGET_ESP32C3)

  #define NEOPIXEL_RGB_PIN       0
  #define NEOPIXEL_RGBW_PIN      3
  #define DOTSTAR_DATA_PIN       7
  #define DOTSTAR_CLOCK_PIN      2

  #define DEVICE_SUFFIX          "-C3"

#endif
 
#include "HomeSpan.h"
#include "LED.h"
#include "Effect.h"
#include <FastLED.h>

// EffectClass effect;
// NeoPixel_RGB led;

void setup() {
  
  Serial.begin(115200);
 
  homeSpan.begin(Category::Lighting,"RGB strips" DEVICE_SUFFIX);

  SPAN_ACCESSORY();                                             // create Bridge (note this sketch uses the SPAN_ACCESSORY() macro, introduced in v1.5.1 --- see the HomeSpan API Reference for details on this convenience macro)

  SPAN_ACCESSORY("2RGB strip 1");
    EffectClass effect(13, 120);                       // create 8-LED NeoPixel RGB Strand with full color control

  SPAN_ACCESSORY("2Effect Setter 1");
    NeoPixel_RGB led(led, 13, 120);                       // create 8-LED NeoPixel RGB Strand with full color control

}

void loop() {
  homeSpan.poll();
}

///////////////////////////////

LED.h

#ifndef NEOPIXEL_RGB_H
#define NEOPIXEL_RGB_H

#include <HomeSpan.h>
#include <FastLED.h>

class NeoPixel_RGB : public Service::LightBulb {
public:
    Characteristic::On power{0, true};
    Characteristic::Hue H{0, true};
    Characteristic::Saturation S{0, true};
    Characteristic::Brightness V{100, true};

    NeoPixel_RGB(uint8_t pin, int nPixels); // Constructor declaration

    boolean update() override;

private:
    int nPixels;
    Pixel *pixel;
};

NeoPixel_RGB::NeoPixel_RGB(uint8_t pin, int nPixels) : Service::LightBulb() {
    this->nPixels = nPixels;
    V.setRange(0, 100, 1);
    pixel = new Pixel(pin);
    update();
}

boolean NeoPixel_RGB::update() {
    int p = power.getNewVal();
    float h = H.getNewVal<float>();
    float s = S.getNewVal<float>();
    float v = V.getNewVal<float>();

    Pixel::Color color;
    pixel->set(color.HSV(h * p, s * p, v * p), nPixels);

    return true;
}

#endif // NEOPIXEL_RGB_H

///////////////////////////////

Effect.h

#ifndef EFFECTCLASS_H
#define EFFECTCLASS_H

#include <HomeSpan.h>
#include <FastLED.h>
#include "LED.h"

struct EffectClass : Service::LightBulb {
    Characteristic::On pwr{0,true};
    Characteristic::Hue Hu{0,true};
    Characteristic::Saturation Sa{0,true};
    Characteristic::Brightness Br{100,true};
    NeoPixel_RGB &neopixel;

    EffectClass(NeoPixel_RGB &neopixel, int pin, int nPixels) : neopixel(neopixel), Service::LightBulb() {
        Br.setRange(0, 100, 1);
        update();
    }

    boolean update() override {
        int pw = pwr.getNewVal();
        float hu = Hu.getNewVal<float>();
        float sa = Sa.getNewVal<float>();
        float br = Br.getNewVal<float>();

        Pixel::Color clr;
        neopixel.power.setVal(pw);
        neopixel.H.setVal(hu);
        neopixel.S.setVal(sa);
        neopixel.V.setVal(br);

        return true;
    }
};

#endif // EFFECTCLASS_H

Well I'm using HomeSpan (sorry for not saying that in the first place - edited my post.
In HomeSpan that is the correct way of creating an accessory (see the HomeSpan Pixel example).

What I'm trying to achieve is this (since HomeKit has limitations):
Main LED strip controller (LED.h) that does this:
Sets brightness, hue, saturation (HSV) and sends that info to EffectClass

EffectClass sets the LED effect based on set brightness (20 - strobing, 40 - static, ...) via FastLED or some other library.

in other words I need to access the variables from LED.h to Effect.h (since there can be multiple LED strips it must work in that case as well

EffectClass* myObject;  // makes a pointer..
 myObject = new EffectClass(13, 120); // Creates the class and now you have a handle to it.

Now you can pass the pointer to the class anywhere you want to.

-jim lee

1 Like

You say that like everyone knows what it is / means. They don't.

Same with "HomeKit".

Word Salad.

ROFL!!

-jimlee

The "CustomService.ino" example mentions the "Not Supported" message. Perhaps looking at that example will be helpful:

// This brief sketch demonstrates how you can use HomeSpan's Custom Service and Custom Characteristic features
// to create a Pressure Sensor Accessory that will be recognized by the Eve for HomeKit App. Note that the
// Apple Home App will show this as a "Not Supported" Accessory Tile indicating it cannot be used in the Home App.
// However, this does not create any problems or errors in the Home App.

Thinking of this :wink:

@VirtualCZ, sorry about this, could not resists :wink:

Well, this is very specific to the Apple HomeKit ecosystem. If you are not familiar with homekit then the library and the example won't make much sense to you.

If you are, this is actually quite a great library to build your own HomeKit integrable devices. Works really well.

there is a solid documentation to get started (HomeSpan/docs/GettingStarted.md at master · HomeSpan/HomeSpan · GitHub)

I'd recommend following the Tutorials

one thing that is not obvious at first (and author tried to highlight that through indentation) is that when you do

  new SpanAccessory();                              // Begin by creating a new Accessory using SpanAccessory(), no arguments needed
    new Service::AccessoryInformation();            // HAP requires every Accessory to implement an AccessoryInformation Service, with the required Identify Characteristic
      new Characteristic::Identify();               // Create the required Identify  

the service and Characteristic are automatically attached to the most recently created element (so here Identify s attached to AccessoryInformation which is attached to our new SpanAccessory)


@VirtualCZ I'm unclear on what you want to achieve

Thanks, but not interested. I have no involvement with the Apple ecosystem. I was only interested from a C++ coding technique point of view.

Yes, I picked that up by looking at the library's source code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.