discoLight
Loading...
Searching...
No Matches
LightController Class Reference

#include <LightController.h>

Inheritance diagram for LightController:
Collaboration diagram for LightController:

Public Member Functions

 LightController (int gpioOutPin, int gpioInPin, SWE4211RPi::CommandQueue &queue, std::string threadName, uint32_t period)
 
virtual ~LightController ()
 
void taskMethod ()
 
- Public Member Functions inherited from SWE4211RPi::PeriodicTask
 PeriodicTask (std::string threadName, uint32_t period, int priority=1)
 
virtual ~PeriodicTask ()
 
virtual void setTaskPeriod (uint32_t period) final
 
virtual uint32_t getTaskPeriod () final
 
void run () final
 
virtual void printInformation ()
 
virtual double getCPUUsageInfo ()
 
virtual void resetThreadDiagnostics ()
 
- Public Member Functions inherited from SWE4211RPi::RunnableClass
 RunnableClass (std::string threadName, int priority=0)
 
virtual ~RunnableClass ()
 
virtual pid_t getThreadID ()
 
virtual void start () final
 
virtual void setPriority (int priority) final
 
virtual int getPriority () final
 
virtual bool isShutdown () final
 
virtual bool isStarted () final
 
virtual void stopThreadExecution ()
 
virtual void waitForShutdown ()
 

Private Attributes

SWE4211RPi::CommandQueue & referencequeue
 
SWE4211RPi::GPIO & light
 
SWE4211RPi::GPIO & pushbutton
 
int dutyCycle =50
 
bool lampOn = false
 

Additional Inherited Members

- Static Public Member Functions inherited from SWE4211RPi::RunnableClass
static void printThreads ()
 
static void resetAllThreadInformation ()
 
- Protected Member Functions inherited from SWE4211RPi::PeriodicTask
virtual void startChildRunnables () final
 
virtual void stopChildRunnables () final
 
virtual void startChildTasks ()
 
virtual void stopChildTasks ()
 
- Protected Attributes inherited from SWE4211RPi::PeriodicTask
uint32_t taskPeriod = 100000
 
- Protected Attributes inherited from SWE4211RPi::RunnableClass
std::thread * myThread = NULL
 
pthread_t executingThread = 0
 
pid_t myOSThreadID = 0
 
std::string myName
 
bool keepGoing = true
 
bool runCompleted = false
 
bool runStarted = false
 
int priority = -1
 
- Static Protected Attributes inherited from SWE4211RPi::RunnableClass
static std::list< RunnableClass * > runningThreads
 

Detailed Description

A periodic task that controls a single light. On each period it processes any commands waiting on its command queue and then drives the light using software pulse width modulation (PWM), so that the fraction of each period the light is on sets its brightness.

Constructor & Destructor Documentation

◆ LightController()

LightController::LightController ( int  gpioOutPin,
int  gpioInPin,
SWE4211RPi::CommandQueue &  queue,
std::string  threadName,
uint32_t  period 
)

This constructor will instantiate a new instance of the light controller.

Parameters
gpioOutPinThis is the GPIO pin that drives the light controlled by this device.
gpioInPinThis is the GPIO pin connected to the pushbutton that can also turn on the light.
queueThis is the instance of the queue that will be used to send commands to the light controller.
threadNameThis is the name given to the executing thread. It is a simple string that is human readable.
periodThis is the period for the task, given in microseconds. Must be at least 100 microseconds.
  1. All initialization is done in the constructor's member initializer list. Reference members must be initialized there, because a reference cannot be assigned after it is created.

◆ ~LightController()

LightController::~LightController ( )
virtual

This destructor destroys the existing instance of the light controller and releases its GPIO pins.

This destructor destroys the existing instance of the light controller.

  1. Release each GPIO instance that was obtained in the constructor as is applicable.

Member Function Documentation

◆ taskMethod()

void LightController::taskMethod ( )
virtual

This is the task method. The task method is invoked periodically, once every task period. Each invocation processes any waiting commands and then generates one period of the PWM signal for the light.

This is the task method. The task method is invoked periodically, once every task period. Each invocation processes any commands that are waiting on the queue and then generates one period of the PWM signal for the light.

  1. Start by determining whether any commands have arrived on the queue. If the queue has an item on it, dequeue it and process it as is applicable. Continue doing this until the queue is empty.

    1. There are one or more items on the queue. Remove the first item by dequeueing it.

    2. Determine which command was received and process it accordingly. Commands are bitmapped (see NetworkCommands.h), so test whether the bits for each command are set rather than comparing the whole value. Check for the commands in the order below. Any command that is not recognized, such as a broadcast message, is ignored.

      1. If the off command has been received:
        1. Record that the light should be turned off.
      1. If the on command has been received:
        1. Record that the light should be turned on.
      1. If a new PWM duty cycle has been received:
        1. Retrieve the requested duty cycle from the first parameter of the message.
        2. Check that the value is valid. The message uses a scale of 0 to 1000, where 1000 means 100% (see NetworkCommands.h). Ignore any value outside that range and keep the current duty cycle.
        3. If the value is valid, convert it to the percentage scale used by this class (0 to 100) and store it.

  2. Determine whether the light should be lit during this period. The light should be lit if the lamp is on (lampOn) or if the pushbutton is currently pressed. Remember that the pushbutton is active low. If the light should be lit, turn it on for the duty-cycle portion of the period and then turn it off.

    1. If the lamp is off and the pushbutton is not pressed, do nothing and simply end this iteration of the periodic task. The light is already off.

  3. Otherwise:

    1. The light should be lit at the current duty cycle, so turn it on by setting the output pin to the appropriate level. Remember that the light is active low.

    2. Calculate how long the light should stay on. This is the task period multiplied by the duty cycle percentage. The task period is in microseconds. Be careful with integer math: multiply before dividing so that the result is not truncated to zero.

    3. Sleep for the calculated amount of time while the light is on.

    4. Now turn the light off. It stays off for the rest of the period, until the next invocation of this task method.

  4. This ends the periodic task. It will execute again after the next period elapses, as long as the task is set up properly.

Implements SWE4211RPi::PeriodicTask.

Here is the call graph for this function:

Member Data Documentation

◆ dutyCycle

int LightController::dutyCycle =50
private

This value keeps track of the duty cycle for the PWM of this device. The duty cycle is the percentage of each period that the light is on. It can range from 0 to 100, inclusive.

◆ lampOn

bool LightController::lampOn = false
private

This value keeps track of the state of the lamp. If the value is false, the lamp is off. If the value is true, the lamp should be turned on at the brightness given by the duty cycle.

◆ light

SWE4211RPi::GPIO& LightController::light
private

This is the GPIO output for the light that is controlled by this class. The light is active low.

◆ pushbutton

SWE4211RPi::GPIO& LightController::pushbutton
private

This is the GPIO input for the pushbutton. The pushbutton is active low: it reads high when released and low when pressed.

◆ referencequeue

SWE4211RPi::CommandQueue& LightController::referencequeue
private

This is the queue that is read to receive commands for controlling the light.


The documentation for this class was generated from the following files: