Java Magazine, September/October 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 21 internet of things possible to access the light level description by calling the getLightLevel method Controlling One Input and Three Outputs with a Board Manager Now I will create a new BoardManager class that creates an instance of the previously coded SimpleLightSensor class and three instances of the VariableBrightnessLed class This way there is one instance of the VariableBrightnessLed class for each color component of the RGB LED The class fires actions when the ambient light changes Specifically the class adjusts the brightness for the three color components of the RGB LED based on the measured ambient light The following lines show the code for this new class class BoardManager public final SimpleLightSensor lightSensor public final VariableBrightnessLed redLed public final VariableBrightnessLed greenLed public final VariableBrightnessLed blueLed public BoardManager this lightSensor new SimpleLightSensor 0 this redLed new VariableBrightnessLed 6 Red this greenLed new VariableBrightnessLed 5 Green this blueLed new VariableBrightnessLed 3 Blue public void setRGBBrightnessLevel int value this redLed setBrightnessLevel value this greenLed setBrightnessLevel value this blueLed setBrightnessLevel value public void updateLedsBasedOnLight String lightLevel this lightSensor getLightLevel switch lightLevel case SimpleLightSensor LL_ EXTREMELY_ DARK this setRGBBrightnessLevel 255 break case SimpleLightSensor LL_ VERY_ DARK this setRGBBrightnessLevel 128 break case SimpleLightSensor LL_ JUST_ DARK this setRGBBrightnessLevel 64 break default this setRGBBrightnessLevel 0 break The BoardManager class declares the following four fields that the constructor initializes lightSensor An instance of SimpleLightSensor which represents the photoresistor included in the voltage divider connected to the analog pin labeled A0 redLed An instance of VariableBrightnessLed which represents the red component of the RGB LED connected to the GPIO pin labeled 6 greenLed An instance of VariableBrightnessLed which represents the green component of the RGB LED connected to the GPIO pin labeled 5 blueLed An instance of VariableBrightnessLed which represents the blue component of the RGB LED connected to the GPIO pin labeled 3
You must have JavaScript enabled to view digital editions.