Java Magazine, Sept/Oct 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 18 internet of things Then the constructor calls the pwm enable method with true as a parameter to set the enable status of the PWM enabled pin and allow the code to start setting the output duty cycle percentage for the PWM pin with calls to the pwm write method Finally the constructor calls the setBrightnessLevel method with 0 as the value for the brightnessLevel argument This way the constructor sets the brightness level to 0 for the LED wired to the specified pin number and turns of the LED Specifically the call turns of a specific color component of the RGB LED The class declares a setBrightnessLevel method that translates a brightness level from 0 to 255 inclusive into the appropriate output duty cycle value for the PWM pin The method receives a brightness level int value in the brightnessLevel argument First the code makes sure that the brightness level is a value between 0 and 255 inclusive If the value is out of range the code uses either the lower level or the upperlevel value and saves it in the validBrightnessLevel local variable Then the code calculates the required output dutycycle percentage for the PWM pin to represent the brightness level as a float value between 10f and 00f 100 percent and 0 percent It is necessary to divide the valid brightness level validBrightnessLevel by 255f The code saves the value in the convertedLevel variable The next line calls the this pwm write method with the convertedLevel variable for the percentage argument and sets the output duty cycle for the pin configured as the PWM output to convertedLevel Finally the code saves the valid brightness level in the brightnessLevel field which is read only accessible through the getBrightnessLevel method The last line prints details about the brightness level set to the LED identified with a name and wired to a specific pin number The line is printed with System out format and it is possible to see the output when you run the generated JAR file through the IDE or by running commands through SSH on Yocto Linux running on the board Ill dive deep on the benefits of printing useful information later Measuring Ambient Light via Analog Input Now I create a new VoltageInput class that represents a voltage source connected to an analog input pin on the board The following lines show the code for this new class class VoltageInput private final int analogPin private final Aio aio public VoltageInput int analogPin this analogPin analogPin this aio new Aio analogPin Configure the ADC short for Analog to Digital Converter resolution to 12 bits 0 to 4095 this aio setBit 12 public float getVoltage long rawValue this aio read float voltageValue rawValue 4095f 5f return voltageValue The class declares a method that translates a brightness level from 0 to 255 inclusive into the appropriate output duty cycle value for the PWM pin
You must have JavaScript enabled to view digital editions.