Java Magazine, September/October 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 17 internet of things of the VariableBrightnessLed and SimpleLightSensor classes and will interact with them Finally I will create a master class called AmbientLightAndLed which coordinates the operations of the previously explained classes First I create a new VariableBrightnessLed class that represents an LED connected to the board that can have a brightness level from 0 to 255 inclusive The following code lines show the import statements that I use for all the classes and the code for the new class import mraa Aio import mraa Pwm class VariableBrightnessLed private final int gpioPin private final String name private final Pwm pwm private int brightnessLevel public VariableBrightnessLed int gpioPin String name this name name this gpioPin gpioPin this pwm new Pwm gpioPin this pwm period_ us 700 this pwm enable true Set the initial brightness level to 0 this setBrightnessLevel 0 public void setBrightnessLevel int brightnessLevel int validBrightnessLevel brightnessLevel if validBrightnessLevel 255 validBrightnessLevel 255 else if validBrightnessLevel 0 validBrightnessLevel 0 float convertedLevel validBrightnessLevel 255f this pwm write convertedLevel this brightnessLevel validBrightnessLevel System out format s LED connected to PWM Pin d set to brightness level d n this name this gpioPin validBrightnessLevel public int getBrightnessLevel return this brightnessLevel public int getGpioPin return this gpioPin When I create an instance of the VariableBrightnessLed class it is necessary to specify the GPIO pin number to which the LED is connected in the gpioPin int argument and a name for the LED in the name String argument The constructor creates a new mraa Pwm instance with the received gpioPin as its pin argument saves its reference in the pwm field and calls its period_ us method to configure the PWM period to 700 microseconds 700 μs This way the output duty cycle will determine the percentage of the 700 μs period during which the signal is in the on state For example a 010 10 percent output duty cycle means that the signal is on during 70 μs of the 700 μs period
You must have JavaScript enabled to view digital editions.