Last active
January 28, 2017 20:31
-
-
Save sergiocasero/61d1da762928c92189585a2865625d28 to your computer and use it in GitHub Desktop.
Listing Ports with android things
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void initializeHardware() { | |
// Create peripheralManagerService and get GPIO list | |
PeripheralManagerService peripheralManagerService = new PeripheralManagerService(); | |
List<String> portList = peripheralManagerService.getGpioList(); | |
if (!portList.isEmpty()) { | |
try { | |
// Initialize GPIOs | |
initializeGpios(peripheralManagerService, portList, portList.size() < MAX_RELAYS ? portList.size() : MAX_RELAYS); | |
} catch (IOException e) { | |
view.showError("An error has ocurred"); | |
} | |
} else { | |
view.showError("There are no available ports"); | |
} | |
} | |
private void initializeGpios(PeripheralManagerService peripheralManagerService, List<String> portList, int size) | |
throws IOException { | |
relays = new ArrayList<>(); | |
for (int index = 0; index < size; index++) { | |
String port = portList.get(index); | |
Gpio gpio = peripheralManagerService.openGpio(port); | |
gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); | |
gpio.setActiveType(Gpio.ACTIVE_LOW); | |
gpio.setValue(true); // Set value to TRUE, so lights should be on | |
relays.add(new Relay(gpio, port, port)); | |
} | |
// If board doesn´t have max relays, added rest of relays with "none" text to inform to the user | |
if (size < MAX_RELAYS) { | |
for (int index = size; index < MAX_RELAYS; index++) { | |
relays.add(new Relay(null, "NONE")); | |
} | |
} | |
view.showConnectionInformation(relays); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment