summaryrefslogtreecommitdiff
path: root/blink.c
blob: 4598503fbe06f702c41c041e3a906888823346c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
	Copyright (c) 2016 CurlyMo <curlymoo1@gmail.com>

  This Source Code Form is subject to the terms of the Mozilla Public
  License, v. 2.0. If a copy of the MPL was not distributed with this
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

#include "wiringx.h"
#include "../src/platform/platform.h"

char *usage =
	"Usage: %s platform GPIO\n"
	"       GPIO is the GPIO to write to\n"
	"Example: %s raspberrypi2 10\n";

int main(int argc, char *argv[]) {
	char *str = NULL, *platform = NULL;
	char usagestr[130];
	int gpio = 0, invalid = 0;

	memset(usagestr, '\0', 130);

	// expect only 1 argument => argc must be 2
	if(argc != 3) {
		snprintf(usagestr, 129, usage, argv[0], argv[0]);
		puts(usagestr);
		return -1;
	}

	// check for a valid, numeric argument
	platform = argv[1];
	str = argv[2];
	while(*str != '\0') {
		if(!isdigit(*str)) {
			invalid = 1;
		}
		str++;
	}
	if(invalid == 1) {
		printf("%s: Invalid GPIO %s\n", argv[0], argv[2]);
		return -1;
	}

	gpio = atoi(argv[2]);

	if(wiringXSetup(platform, NULL) == -1) {
		wiringXGC();
		return -1;
	}

	if(wiringXValidGPIO(gpio) != 0) {
		printf("%s: Invalid GPIO %d\n", argv[0], gpio);
		wiringXGC();
		return -1;
	}

	pinMode(gpio, PINMODE_OUTPUT);
	while(1) {
		printf("Writing to GPIO %d: High\n", gpio);
		digitalWrite(gpio, HIGH);
		sleep(1);
		printf("Writing to GPIO %d: Low\n", gpio);
		digitalWrite(gpio, LOW);
		sleep(1);
	}
}