/* * Copyright (C) 2018 Tom Keddie * * This file is subject to the terms and conditions of the GNU Lesser General * Public License v2.1. See the file LICENSE in the top level directory for more * details. */ /** * @ingroup tests * @{ * * @file * @brief Manual test application for GPIO peripheral drivers using * Arduino pin numbering * * @author Tom Keddie * * @} */ #include #include #include "shell.h" #include "periph/gpio.h" #define ARDUINO_PIN_NUMOF ((int)(sizeof(arduino_pinmap)/sizeof(arduino_pinmap[0]))) static int get_digital_pin(const char* pinstring) { int arduino_pin = -1; char *endptr = NULL; if (*pinstring == 'A') { /* TODO : add analogInputToDigitalPin macro to arduino_board.h for all boards */ return -1; } arduino_pin = strtol(pinstring, &endptr, 0); /* strtol returns 0 on error, manual check for pin 0, works for 0 but not 00 etc, this will have to be acceptable */ if (arduino_pin == 0 && (pinstring[0] != '0' || pinstring[1] != '\0')) arduino_pin = -1; if ((*endptr != '\0') || (arduino_pin == -1) || (arduino_pin > ARDUINO_PIN_NUMOF)) { return -1; } return arduino_pin; } static int init_pin(int argc, char **argv, int mode) { int pin; if (argc < 2) { printf("usage: %s \n", argv[0]); return 1; } pin = get_digital_pin(argv[1]); if (pin < 0) { printf("Could not map Arduino pin %s to a gpio.\nExpected numeric pin number.\n", argv[1]); return 1; } pinMode(pin, mode); return 0; } static int init_out(int argc, char **argv) { return init_pin(argc, argv, OUTPUT); } static int init_in(int argc, char **argv) { return init_pin(argc, argv, INPUT); } static int init_in_pu(int argc, char **argv) { return init_pin(argc, argv, INPUT_PULLUP); } static int read(int argc, char **argv) { int pin; if (argc < 2) { printf("usage: %s \n", argv[0]); return 1; } pin = get_digital_pin(argv[1]); if (pin < 0) { printf("Could not map Arduino pin %s to a gpio.\nExpected numeric pin number.\n", argv[1]); return 1; } if (digitalRead(pin)) { printf("ARDUINO_PIN(%i) is HIGH\n", pin); } else { printf("ARDUINO_PIN(%i) is LOW\n", pin); } return 0; } static int set(int argc, char **argv) { int pin; if (argc < 2) { printf("usage: %s \n", argv[0]); return 1; } pin = get_digital_pin(argv[1]); if (pin < 0) { printf("Could not map Arduino pin %s to a gpio.\nExpected numeric pin number.\n", argv[1]); return 1; } digitalWrite(pin, 1); return 0; } static int clear(int argc, char **argv) { int pin; if (argc < 2) { printf("usage: %s \n", argv[0]); return 1; } pin = get_digital_pin(argv[1]); if (pin < 0) { printf("Could not map Arduino pin %s to a gpio.\nExpected numeric pin number.\n", argv[1]); return 1; } digitalWrite(pin, 0); return 0; } static int toggle(int argc, char **argv) { int pin; if (argc < 2) { printf("usage: %s \n", argv[0]); return 1; } pin = get_digital_pin(argv[1]); if (pin < 0) { printf("Could not map Arduino pin %s to a gpio.\nExpected numeric pin number.\n", argv[1]); return 1; } /* not thread safe but Arduino doesn't promise such things */ if (digitalRead(pin)) { digitalWrite(pin, 0); } else { digitalWrite(pin, 1); } return 0; } static const shell_command_t shell_commands[] = { { "init_out", "init as output (push-pull mode)", init_out }, { "init_in", "init as input w/o pull resistor", init_in }, { "init_in_pu", "init as input with pull-up", init_in_pu }, { "read", "read pin status", read }, { "set", "set pin to HIGH", set }, { "clear", "set pin to LOW", clear }, { "toggle", "toggle pin", toggle }, { NULL, NULL, NULL } }; void setup(void) { puts("Arduino GPIO peripheral driver test. ? for help"); /* start the shell */ char line_buf[SHELL_DEFAULT_BUFSIZE]; shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); } void loop(void) { }