Uit Hack42
Ga naar: navigatie, zoeken
(Nieuwe pagina aangemaakt met '{{Project |Naam=ESpaceState |Eigenaar=BugBlue |Status=Uitvoer |Skills=solderen, C coden, GPIO, docs lezen, schroeven, spijkeren, debuggen |Samenvatting=GPIO switches m...')
 
k (→‎Code or it didn't happen: Is it me or mediawiki)
Regel 14: Regel 14:
  
 
=== Code or it didn't happen ===
 
=== Code or it didn't happen ===
<code>
+
<pre>
 
/* ALIX 1C GPIO reader and handler. Contains bugs. Guaranteed! */
 
/* ALIX 1C GPIO reader and handler. Contains bugs. Guaranteed! */
 
/* TODO:
 
/* TODO:
Regel 151: Regel 151:
 
exit(1); // WTF do we do here?
 
exit(1); // WTF do we do here?
 
}
 
}
</code>
+
</pre>

Versie van 14 mrt 2011 19:55

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:
   * player script in the background
   * 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
*/

#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;

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);
}

<code>
int main(int nargs, char **argv)
{
	int test1;
	int test2;
	BYTE byte = 0;
	struct tm *local;
	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;
	int ot1=0,ot2=0;
	while(1) {
		byte = get(dataIdx);
		local = localtime(&t);
		if(DEBUG) printf("DATA = [0x%02x] (%s)\n", byte, toBin(byte));	

		test1 = 1 & byte;
		if(test1==0 && ot1==0) {
			printf("%s Voor-tussendeur is open\n",asctime(local));
			system("./1-open.sh");
			ot1=1;
		} else if(test1!=0 && ot1==1) {
			printf("%s Voor-Tussendeur is closed\n",asctime(local));
			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);
	}
	
	exit(1); // WTF do we do here?
}