Giter Site home page Giter Site logo

Comments (6)

jandelgado avatar jandelgado commented on May 25, 2024

Yes, you can define you own custom HAL class, which controls access to the GPIO and the timer. I have assembled an example, which will be included in the examples of JLed.
Is this what you are looking for?

// JLed custom HAL example. 
// Copyright 2019 by Jan Delgado. All rights reserved.
// https://github.com/jandelgado/jled

// we include jled_base.h instead of "jled.h" since we define our own JLed
// class using our custom HAL.
#include <jled_base.h>

// a custom HAL for the Arduino, inverting output and ticking with half
// the speed.
class CustomHal : jled::JLedHal {
    private:
        template <typename T, typename B>
            friend class jled::TJLed;
        CustomHal() {}

    public:
        explicit CustomHal(uint8_t pin) noexcept : pin_(pin) {}

        void analogWrite(uint8_t val) const {
            // some platforms, e.g. STM need lazy initialization
            if (!setup_) {
                ::pinMode(pin_, OUTPUT);
                setup_ = true;
            }
            ::analogWrite(pin_, 255-val);
        }

        uint32_t millis() const { return ::millis() >> 1; }

    private:
        mutable bool setup_ = false;
        uint8_t pin_;
};

class JLed : public jled::TJLed<CustomHal, JLed> {
    using jled::TJLed<CustomHal, JLed>::TJLed;
};

// uses above defined CustomHal
auto led = JLed(LED_BUILTIN).Blink(1000, 1000).Repeat(5);

void setup() {
}

void loop() {
    led.Update();
}

from jled.

sslupsky avatar sslupsky commented on May 25, 2024

Yes, I modified the arduino_hal.h to change the millis(). I was wondering if this could be part of the API where you call a method to override millis() with your own function?

from jled.

jandelgado avatar jandelgado commented on May 25, 2024

The official, non-invasive, way would be to implement a custom HAL as shown above. This way, no modification of the lib is necessary.

from jled.

sslupsky avatar sslupsky commented on May 25, 2024

Wouldn't you still need to change the reference to arduino_hal.h in the code snippet below from jled.h to the custom HAL ?

#ifdef ESP32
#include "esp32_hal.h"  // NOLINT
namespace jled {using JLedHalType = Esp32Hal;}
#elif ESP8266
#include "esp8266_hal.h"  // NOLINT
namespace jled {using JLedHalType = Esp8266Hal;}
#else
#include "arduino_hal.h"  // NOLINT
namespace jled {using JLedHalType = ArduinoHal;}
#endif

from jled.

jandelgado avatar jandelgado commented on May 25, 2024

No, because in the custom HAL example above, I included jled_base.h instead of jled.h and defined my own JLed class. This is basically what is usually done in jled.h, for convenience reasons.

from jled.

jandelgado avatar jandelgado commented on May 25, 2024

Hi, can we close the issue? The problem can be solved as described. In the meantime, I added an Custom-HAL example to the examples section.

br
Jan

from jled.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.