|
discoLight
|
#include <LightController.h>


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 |
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.
| 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.
| gpioOutPin | This is the GPIO pin that drives the light controlled by this device. |
| gpioInPin | This is the GPIO pin connected to the pushbutton that can also turn on the light. |
| queue | This is the instance of the queue that will be used to send commands to the light controller. |
| threadName | This is the name given to the executing thread. It is a simple string that is human readable. |
| period | This is the period for the task, given in microseconds. Must be at least 100 microseconds. |
|
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.
|
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.
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.
There are one or more items on the queue. Remove the first item by dequeueing it.
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.
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.
Otherwise:
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.
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.
Sleep for the calculated amount of time while the light is on.
Now turn the light off. It stays off for the rest of the period, until the next invocation of this task method.
Implements SWE4211RPi::PeriodicTask.

|
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.
|
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.
|
private |
This is the GPIO output for the light that is controlled by this class. The light is active low.
|
private |
This is the GPIO input for the pushbutton. The pushbutton is active low: it reads high when released and low when pressed.
|
private |
This is the queue that is read to receive commands for controlling the light.