summaryrefslogtreecommitdiff
path: root/commandline/i2c.c
blob: 8d283474c90ff94cd5fc7c8da1272b8b7be293cd (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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "i2c-util.h"

int main(int argc, char **argv)
{
	char line[8];
	signed char i;
	short int number;

	i2c_init();
	i2c_start();
	puts("ready");

	while (fgets(line, 8, stdin) != NULL) {
		if (sscanf(line, "%hi\n", &number) == 1) {
			if ((number >= 0) && (number <= 256)) {
				for (i = 7; i >= -1; i--) {
					if ((i < 0) || (number == 256) || (number & (1 << i))) {
						puts("sda ↑");
						set_sda(1);
						//verify_sda_high();
					} else {
						puts("sda ↓");
						set_sda(0);
						//verify_sda_low();
					}
					usleep(10);
					puts("scl ↑");
					set_scl(1);
					usleep(10);
					//verify_scl_high();
					if (i < 0) {
						if (get_status() & (1 << BIT_SDA))
							puts("> NAK");
						else
							puts("> ACK");
					}
					else if (number == 256) {
						if (get_status() & (1 << BIT_SDA))
							puts("1");
						else
							puts("0");
					}
					puts("scl ↓");
					set_scl(0);
					usleep(10);
					//verify_scl_low();
				}
			}
		}
		if (strcmp(line, "push\n") == 0) {
			i2c_stop();
			i2c_start();
		}
	}

	i2c_stop();
	i2c_deinit();
	return 0;
}