Skip to content

Instantly share code, notes, and snippets.

@ddollar
Created March 7, 2013 21:43
Show Gist options
  • Save ddollar/5112084 to your computer and use it in GitHub Desktop.
Save ddollar/5112084 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#define GREEN_BUTTON 7
#define RED_BUTTON 3
#define MCP_CHANNEL 0
#define MCP_FREQUENCY 100000
#define DELAY 0
#define DOT_WAVE 30
void error(char *msg) {
fprintf(stderr, "gpio: %s\n", msg);
exit(1);
}
void setup_buttons() {
pinMode(GREEN_BUTTON, INPUT);
pinMode(RED_BUTTON, INPUT);
}
void read_buttons() {
printf("g:%d r:%d ", digitalRead(GREEN_BUTTON), digitalRead(RED_BUTTON));
}
void setup_mcp() {
if (wiringPiSPISetup(MCP_CHANNEL, MCP_FREQUENCY) == -1) {
error("could not set up wiringPi SPI");
}
}
int min = 0;
int max = 0;
char block[] = { 0xe2, 0x96, 0x88, 0x0 };
void read_mcp() {
uint8_t buffer[] = { 0x01, 0b10100000, 0x00 };
int result, delta, dots, i;
wiringPiSPIDataRW(MCP_CHANNEL, buffer, 3);
result = ((buffer[1] & 3) << 8) + buffer[2];
delta = (512 - result);
if (delta > max) max = delta;
if (delta < min) min = delta;
printf("d:%-4d mx:%-4d mn:%-4d", delta, max, min);
dots = delta / DOT_WAVE;
for (i = -DOT_WAVE; i < 0; i++)
(dots < i) ? printf("%s", block) : printf(" ");
for (i = 1; i <= DOT_WAVE; i++)
(dots > i) ? printf("%s", block) : printf(" ");
printf("\n");
}
void setup (void) {
if (geteuid() != 0) {
error("need to be root");
}
if (wiringPiSetup() == -1) {
error("could not set up wiringPi");
}
printf("setup... ");
fflush(stdout);
setup_buttons();
setup_mcp();
printf("ok\n");
}
int main (void) {
setup();
for (;;) {
read_buttons();
read_mcp();
delay(DELAY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment