|
| 1 | +/* ============================================================== |
| 2 | + * Copyright (c) 2015 - 2020 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * ============================================================= */ |
| 6 | + |
| 7 | +/* Analog input |
| 8 | + * Read values from a gpio analog input pin. |
| 9 | + */ |
| 10 | + |
| 11 | +#include <unistd.h> |
| 12 | + |
| 13 | +#include <iostream> |
| 14 | +#include <mraa.hpp> |
| 15 | + |
| 16 | +// Define the following if using a Grove Pi Shield |
| 17 | +#define USING_GROVE_PI_SHIELD |
| 18 | +using namespace std; |
| 19 | +using namespace mraa; |
| 20 | + |
| 21 | +// leave warning/error message in console and wait for user to press Enter |
| 22 | +void consoleMessage(const string& str) { |
| 23 | + cerr << str << endl; |
| 24 | + sleep(10); |
| 25 | +} |
| 26 | + |
| 27 | +// check if running as root |
| 28 | +void checkRoot(void) { |
| 29 | + int euid = geteuid(); |
| 30 | + if (euid) { |
| 31 | + consoleMessage( |
| 32 | + "This project uses Mraa I/O operations that may require\n" |
| 33 | + "'root' privileges, but you are running as non - root user.\n" |
| 34 | + "Passwordless keys(RSA key pairs) are recommended \n" |
| 35 | + "to securely connect to your target with root privileges. \n" |
| 36 | + "See the project's Readme for more info.\n\n"); |
| 37 | + } |
| 38 | + return; |
| 39 | +} |
| 40 | + |
| 41 | +// set pin values depending on the current board (platform) |
| 42 | +void initPlatform(int& gpioPin) { |
| 43 | + // check which board we are running on |
| 44 | + Platform platform = getPlatformType(); |
| 45 | + switch (platform) { |
| 46 | + case INTEL_UP2: |
| 47 | +#ifdef USING_GROVE_PI_SHIELD |
| 48 | + gpioPin = 2 + 512; // A2 Connector (512 offset needed for the shield) |
| 49 | + break; |
| 50 | +#endif |
| 51 | + default: |
| 52 | + string unknownPlatformMessage = |
| 53 | + "This sample uses the MRAA/UPM library for I/O access, " |
| 54 | + "you are running it on an unrecognized platform.\n" |
| 55 | + "You may need to modify the MRAA/UPM initialization code to " |
| 56 | + "ensure it works properly on your platform.\n"; |
| 57 | + consoleMessage(unknownPlatformMessage); |
| 58 | + } |
| 59 | + return; |
| 60 | +} |
| 61 | + |
| 62 | +int main() { |
| 63 | + // Check access permissions for the current user |
| 64 | + // Can be commented out for targets with user level I/O access enabled |
| 65 | + checkRoot(); |
| 66 | + |
| 67 | + int gpioPin = 2; |
| 68 | + initPlatform(gpioPin); |
| 69 | + |
| 70 | +#ifdef USING_GROVE_PI_SHIELD |
| 71 | + addSubplatform(GROVEPI, "0"); |
| 72 | +#endif |
| 73 | + // create an analog input object from MRAA using the pin |
| 74 | + Aio* a_pin = new Aio(gpioPin); |
| 75 | + if (a_pin == NULL) { |
| 76 | + consoleMessage("Can't create mraa::Aio object, exiting"); |
| 77 | + return MRAA_ERROR_UNSPECIFIED; |
| 78 | + } |
| 79 | + |
| 80 | + // loop forever printing the input value every second |
| 81 | + for (;;) { |
| 82 | + uint16_t pin_value; |
| 83 | + try { |
| 84 | + // read the current input voltage |
| 85 | + pin_value = a_pin->read(); |
| 86 | + } catch (const invalid_argument& readExc) { |
| 87 | + // if incorrect voltage value input |
| 88 | + cerr << "Invalid argument, exception thrown: " << readExc.what() << endl; |
| 89 | + consoleMessage("MRAA cannot read pin value!"); |
| 90 | + return MRAA_ERROR_INVALID_PARAMETER; |
| 91 | + } |
| 92 | + cout << "analog input value " << pin_value << endl; |
| 93 | + sleep(1); |
| 94 | + } |
| 95 | + |
| 96 | + return SUCCESS; |
| 97 | +} |
0 commit comments