PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LED.cpp 1.5KB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * File Purpose
  3. * Implementing LED.h functions
  4. */
  5. #include "Arduino.h"
  6. #include "LED.h"
  7. // Initial constructor. Sets the pin, pinmode, and sets initial state as false
  8. LED::LED(uint8_t pin)
  9. {
  10. this->pin = pin;
  11. state = false;
  12. pinMode(pin, OUTPUT);
  13. this->duration = 0;
  14. this->period = 0;
  15. this->duty = 0;
  16. }
  17. LED::~LED(){}
  18. // Turns on the LED and changes state to true. Volatile so it can be run in thread
  19. void LED::turnOn() volatile
  20. {
  21. digitalWrite(pin, HIGH);
  22. state = true;
  23. }
  24. // Turns off the LED and changes state to false. Volatile so it can be run in thread
  25. void LED::turnOff() volatile
  26. {
  27. digitalWrite(pin, LOW);
  28. state = false;
  29. }
  30. // This function emulates a PWM signal. But used not for controlling voltage but blinking.
  31. void LED::runTarget(void *arg)
  32. {
  33. // Casting the derived object so it can be used within the static runnable functions
  34. LED* thisobj = static_cast<LED*>(arg);
  35. float elapsedDuration = duration;
  36. do
  37. {
  38. thisobj->turnOn();
  39. threads.delay(period * (duty / 100));
  40. thisobj->turnOff();
  41. threads.delay((period * (1 - (duty / 100))));
  42. // For quick example, just assuming that thread delay will equal to exact execution time
  43. elapsedDuration -= period;
  44. } while(elapsedDuration > 0);
  45. }
  46. // Start thread that will last for duration duration, period period, and duty duty
  47. void LED::startBlinking(float duration, float period, float duty)
  48. {
  49. this->duration = duration;
  50. this->period = period;
  51. this->duty = duty;
  52. // Start thread
  53. blinkThread = new std::thread(&Runnable::runThread, this);
  54. blinkThread->detach();
  55. }