Uit Hack42
Ga naar: navigatie, zoeken
k (→‎Code or it didn't happen: Is it me or mediawiki)
Regel 17: Regel 17:
 
/* ALIX 1C GPIO reader and handler. Contains bugs. Guaranteed! */
 
/* ALIX 1C GPIO reader and handler. Contains bugs. Guaranteed! */
 
/* TODO:
 
/* TODO:
  * player script in the background
 
 
   * stupid output from scripts (or failure to start) to /dev/null
 
   * stupid output from scripts (or failure to start) to /dev/null
  * Don't just copy-paste code but make a nice function for it.
 
 
   * Get rid of BugBlue signatures, they are ugly
 
   * Get rid of BugBlue signatures, they are ugly
 
*/
 
*/
Regel 35: Regel 33:
  
 
typedef unsigned char BYTE;
 
typedef unsigned char BYTE;
 +
struct tm *local;
  
 
const char* toBin(const BYTE c)
 
const char* toBin(const BYTE c)
Regel 79: Regel 78:
 
}
 
}
  
<code>
+
// now do the damn thing
 +
BYTE action(BYTE ot,BYTE byte,int bit,int pin,char *desc) {
 +
        char cmd[50];
 +
int tmp = bit & byte;
 +
  if(tmp==0 && ot==0) {
 +
printf("%s: %s is open\n",asctime(local),desc);
 +
sprintf(cmd,"./%d-open.sh",pin);
 +
} else if(tmp!=0 && ot==1) {
 +
printf("%s: %s is closed\n",asctime(local),desc);
 +
sprintf(cmd,"./%d-close.sh",pin);
 +
        }
 +
        cmd[49]=0;
 +
pid_t childpid = fork();
 +
if (childpid >= 0) { /* we created a child (or is it called smurf) */
 +
if (childpid == 0) { /* I'm tha smurf */
 +
iopl(0); /* still running as root, but don't need the privs anymore */
 +
exit(system(cmd));  // do your stuff and die
 +
} else { /* I'm your daddy */
 +
/* totally useless structure, but I wanted to type the above line */
 +
}
 +
}
 +
return ot;
 +
}
 +
 
 
int main(int nargs, char **argv)
 
int main(int nargs, char **argv)
 
{
 
{
int test1;
 
int test2;
 
 
BYTE byte = 0;
 
BYTE byte = 0;
struct tm *local;
 
 
time_t t;
 
time_t t;
 
t = time(NULL);
 
t = time(NULL);
Regel 119: Regel 138:
 
// Natuurlijk is GP10-17 (1 = open) GP20-26 (0=open) en GP30-35 (0=open)
 
// Natuurlijk is GP10-17 (1 = open) GP20-26 (0=open) en GP30-35 (0=open)
 
const BYTE dataIdx = 0xf1;
 
const BYTE dataIdx = 0xf1;
int ot1=0,ot2=0;
+
BYTE ot1=0,ot2=0,ot3=0,ot4=0,ot5=0,ot6=0,ot7=0,ot8=0;
 
while(1) {
 
while(1) {
 
byte = get(dataIdx);
 
byte = get(dataIdx);
 
local = localtime(&t);
 
local = localtime(&t);
 
if(DEBUG) printf("DATA = [0x%02x] (%s)\n", byte, toBin(byte));
 
if(DEBUG) printf("DATA = [0x%02x] (%s)\n", byte, toBin(byte));
 
+
ot1=action(ot1,byte,1,1,"Voor-tussendeur");
test1 = 1 & byte;
+
ot2=action(ot2,byte,2,2,"Achter-tussendeur");
if(test1==0 && ot1==0) {
+
ot3=action(ot3,byte,4,3,"GPIO12");
printf("%s Voor-tussendeur is open\n",asctime(local));
+
ot4=action(ot4,byte,8,4,"GPIO13");
system("./1-open.sh");
+
ot5=action(ot5,byte,16,5,"GPIO14");
ot1=1;
+
ot6=action(ot6,byte,32,6,"GPIO15");
} else if(test1!=0 && ot1==1) {
+
ot7=action(ot7,byte,64,7,"GPIO16");
printf("%s Voor-Tussendeur is closed\n",asctime(local));
+
ot8=action(ot8,byte,128,8,"GPIO17");
system("./1-close.sh");
 
ot1=0;
 
}
 
 
 
test2 = 2 & byte;
 
if(test2==0 && ot2==0) {
 
printf("%s Achter-tussendeur is open\n",asctime(local));
 
system("./2-open.sh");
 
ot2=1;
 
} else if(test2!=0 && ot2==1) {
 
printf("%s Voor-Tussendeur is closed\n",asctime(local));
 
system("./2-closed.sh");
 
ot2=0;
 
}
 
 
usleep(200000);
 
usleep(200000);
 
}
 
}

Versie van 14 mrt 2011 21:25

Project: ESpaceState
Schroefje24.png
Schroefje24.png
Schroefje24.png
Schroefje24.png
ESpaceState Picture.jpg

ESpaceState

Naam ESpaceState
Door BugBlue
Status Uitvoer
Madskillz solderen, C coden, GPIO, docs lezen, schroeven, spijkeren, debuggen
Doel / Omschrijving
GPIO switches misbruiken om status van de space te detecten
Alle Projecten - Project Toevoegen
File:ESpaceState_Picture.jpg noez

De Alix.1C barPC heeft mplayer, aplay, en 21 GPIO switches pins. Deze pins kun je misbruiken om hier en daar wat uit te lezen als je er een switch tussen zet.

Op dit moment zijn er 2 deuren voorzien van microswitches en 1 deur van bekabeling hiernaartoe.

De code draait als root op de barPC omdat het iets van privs nodig heeft op I/O ports. Chip docs op http://www.itox.com/pages/support/wdt/W83627HF.pdf

Code or it didn't happen

/* ALIX 1C GPIO reader and handler. Contains bugs. Guaranteed! */
/* TODO:
   * stupid output from scripts (or failure to start) to /dev/null
   * Get rid of BugBlue signatures, they are ugly
*/

#include <sys/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>

#define		PORT_INDEX	0x2e
#define		PORT_DATA	0x2f
#define		DEBUG		0

typedef unsigned char BYTE;
struct tm *local;

const char* toBin(const BYTE c)
{
	static char buff[9];
	BYTE mask = 128;
	int i=0;
	while(mask)
	{
		sprintf(buff+i++, mask & c ? "1" : "0");
		mask >>= 1;
	}
	sprintf(buff+i, "%c", '\0');
	return buff;
}

void init()
{
	outb_p(0x87, PORT_INDEX);
	outb_p(0x87, PORT_INDEX);
}

void done()
{
	outb_p(0xAA, PORT_INDEX);
}

void set(BYTE idx, BYTE val)
{
	outb_p(idx, PORT_INDEX);
	outb_p(val, PORT_DATA);
}

BYTE get(BYTE idx)
{
	outb_p(idx, PORT_INDEX);
	return inb_p(PORT_DATA);
}

/* Time to move away */
void leave(int sig) {
	done();
	exit(sig);
}

// now do the damn thing
BYTE action(BYTE ot,BYTE byte,int bit,int pin,char *desc) {
        char cmd[50];
	int tmp = bit & byte;
  	if(tmp==0 && ot==0) {
		printf("%s: %s is open\n",asctime(local),desc);
		sprintf(cmd,"./%d-open.sh",pin);
	} else if(tmp!=0 && ot==1) {
		printf("%s: %s is closed\n",asctime(local),desc);
		sprintf(cmd,"./%d-close.sh",pin);
        }
        cmd[49]=0;
	pid_t childpid = fork();
	if (childpid >= 0) { /* we created a child (or is it called smurf) */
		if (childpid == 0) { /* I'm tha smurf */
			iopl(0); /* still running as root, but don't need the privs anymore */
			exit(system(cmd));  // do your stuff and die
		} else { /* I'm your daddy */
			/* totally useless structure, but I wanted to type the above line */
		}
	}
	return ot;
}

int main(int nargs, char **argv)
{
	BYTE byte = 0;
	time_t t;
	t = time(NULL);

	(void) signal(SIGKILL,leave);

	iopl(3);
	
	init();
	
	// check if OK - must be 0x52
	byte = get(0x20);
	if(DEBUG) printf("0x52 = [0x%02x] (%s)\n", byte, toBin(byte));
	
	// check if GPIO11 is active
	byte = get(0x2a);
	if(DEBUG) printf("CR2A = [0x%02x] (%s)\n", byte, toBin(byte));
	
	// set (and get) logical device to #7 - GPIO1 (GP10-17)
	// GPIO next = #8 (GP20-26)
	// GPIO next = #9 (GP30-35)
	const BYTE devIdx = 0x07;
	set(devIdx, 0x07);
	byte = get(devIdx);
	if(DEBUG) printf("LOGICAL DEVICE# = [0x%02x] (%s)\n", byte, toBin(byte));
	
	// check GPIO1 functions [1:input, 0:output]
	const BYTE funcIdx = 0xf0;
	byte = get(funcIdx);
	if(DEBUG) printf("FUNCTION = [0x%02x] (%s)\n", byte, toBin(byte));

	// GPIO1 data
	// Natuurlijk is GP10-17 (1 = open) GP20-26 (0=open) en GP30-35 (0=open)
	const BYTE dataIdx = 0xf1;
	BYTE ot1=0,ot2=0,ot3=0,ot4=0,ot5=0,ot6=0,ot7=0,ot8=0;
	while(1) {
		byte = get(dataIdx);
		local = localtime(&t);
		if(DEBUG) printf("DATA = [0x%02x] (%s)\n", byte, toBin(byte));	
		ot1=action(ot1,byte,1,1,"Voor-tussendeur");
		ot2=action(ot2,byte,2,2,"Achter-tussendeur");
		ot3=action(ot3,byte,4,3,"GPIO12");
		ot4=action(ot4,byte,8,4,"GPIO13");
		ot5=action(ot5,byte,16,5,"GPIO14");
		ot6=action(ot6,byte,32,6,"GPIO15");
		ot7=action(ot7,byte,64,7,"GPIO16");
		ot8=action(ot8,byte,128,8,"GPIO17");
		usleep(200000);
	}
	
	exit(1); // WTF do we do here?
}