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...')
 
 
(18 tussenliggende versies door 2 gebruikers niet weergegeven)
Regel 6: Regel 6:
 
|Samenvatting=GPIO switches misbruiken om status van de space te detecten
 
|Samenvatting=GPIO switches misbruiken om status van de space te detecten
 
}}
 
}}
 +
 
De Alix.1C barPC heeft mplayer, aplay, en 21 GPIO switches pins. Deze pins kun je misbruiken om hier  
 
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.
 
en daar wat uit te lezen als je er een switch tussen zet.
Regel 14: Regel 15:
  
 
=== Code or it didn't happen ===
 
=== Code or it didn't happen ===
<code>
+
<pre>
/* ALIX 1C GPIO reader and handler. Contains bugs. Guaranteed! */
+
#include <stdint.h>
/* 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 <sys/io.h>
 
#include <stdio.h>
 
#include <stdio.h>
Regel 29: Regel 23:
 
#include <unistd.h>
 
#include <unistd.h>
 
#include <signal.h>
 
#include <signal.h>
 +
#include <sys/wait.h>
  
#define PORT_INDEX 0x2e
+
#define         PORT_INDEX     0x2e
#define PORT_DATA 0x2f
+
#define         PORT_DATA       0x2f
#define DEBUG 0
+
#define         DEBUG           0
  
 
typedef unsigned char BYTE;
 
typedef unsigned char BYTE;
 +
struct tm *local;
  
 
const char* toBin(const BYTE c)
 
const char* toBin(const BYTE c)
 
{
 
{
static char buff[9];
+
        static char buff[9]="        "; // auto 0 termination++
BYTE mask = 128;
+
        BYTE bit;                       // always wanted to do that.
int i=0;
+
        for(bit=0;bit<8;bit++)
while(mask)
+
                buff[7-bit]=(c>>bit) & 1 ? '1' : '0'; // eigtdoc
{
+
        return buff;
sprintf(buff+i++, mask & c ? "1" : "0");
 
mask >>= 1;
 
}
 
sprintf(buff+i, "%c", '\0');
 
return buff;
 
 
}
 
}
  
 
void init()
 
void init()
 
{
 
{
outb_p(0x87, PORT_INDEX);
+
        outb_p(0x87, PORT_INDEX);
outb_p(0x87, PORT_INDEX);
+
        outb_p(0x87, PORT_INDEX);
 
}
 
}
  
 
void done()
 
void done()
 
{
 
{
outb_p(0xAA, PORT_INDEX);
+
        outb_p(0xAA, PORT_INDEX);
 
}
 
}
  
 
void set(BYTE idx, BYTE val)
 
void set(BYTE idx, BYTE val)
 
{
 
{
outb_p(idx, PORT_INDEX);
+
        outb_p(idx, PORT_INDEX);
outb_p(val, PORT_DATA);
+
        outb_p(val, PORT_DATA);
 
}
 
}
  
 
BYTE get(BYTE idx)
 
BYTE get(BYTE idx)
 
{
 
{
outb_p(idx, PORT_INDEX);
+
        outb_p(idx, PORT_INDEX);
return inb_p(PORT_DATA);
+
        return inb_p(PORT_DATA);
 
}
 
}
  
 
/* Time to move away */
 
/* Time to move away */
void leave(int sig) {
+
void leave(int sig)  
done();
+
{
exit(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;
 +
        int docmd=0;
 +
        if(tmp==0 && ot==0) {
 +
                printf("%s: %s is open\n",asctime(local),desc);
 +
                sprintf(cmd,"./%d-open.sh",pin);
 +
                ot=1;
 +
                docmd=1;
 +
        } else if(tmp!=0 && ot==1) {
 +
                printf("%s: %s is closed\n",asctime(local),desc);
 +
                sprintf(cmd,"./%d-close.sh",pin);
 +
                ot=0;
 +
                docmd=1;
 +
        }
 +
        if(docmd) {
 +
                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;
 
}
 
}
  
<code>
 
 
int main(int nargs, char **argv)
 
int main(int nargs, char **argv)
 
{
 
{
int test1;
+
        BYTE byte = 0;
int test2;
+
        time_t t;
BYTE byte = 0;
+
        t = time(NULL);
struct tm *local;
+
        pid_t w;
time_t t;
+
        int status;
t = time(NULL);
+
 
 +
        (void) signal(SIGKILL,leave);
 +
 
 +
        iopl(3);
 +
 
 +
        init();
  
(void) signal(SIGKILL,leave);
+
        if(DEBUG) {
 +
                // check if OK - must be 0x52
 +
                byte = get(0x20);
 +
                printf("0x52 = [0x%02x] (%s)\n", byte, toBin(byte));
  
iopl(3);
+
                // check if GPIO11 is active
+
                byte = get(0x2a);
init();
+
                if(DEBUG) printf("CR2A = [0x%02x] (%s)\n", byte, toBin(byte));
+
        }
// check if OK - must be 0x52
+
        // set (and get) logical device to #7 - GPIO1 (GP10-17)
byte = get(0x20);
+
        // GPIO next = #8 (GP20-26)
if(DEBUG) printf("0x52 = [0x%02x] (%s)\n", byte, toBin(byte));
+
        // GPIO next = #9 (GP30-35)
+
        const BYTE devIdx = 0x07;
// check if GPIO11 is active
+
        set(devIdx, 0x07);
byte = get(0x2a);
+
        byte = get(devIdx);
if(DEBUG) printf("CR2A = [0x%02x] (%s)\n", byte, toBin(byte));
+
        if(DEBUG) printf("LOGICAL DEVICE# = [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
+
        // check GPIO1 functions [1:input, 0:output]
// Natuurlijk is GP10-17 (1 = open) GP20-26 (0=open) en GP30-35 (0=open)
+
        const BYTE funcIdx = 0xf0;
const BYTE dataIdx = 0xf1;
+
        byte = get(funcIdx);
int ot1=0,ot2=0;
+
        if(DEBUG) printf("FUNCTION = [0x%02x] (%s)\n", byte, toBin(byte));
while(1) {
 
byte = get(dataIdx);
 
local = localtime(&t);
 
if(DEBUG) printf("DATA = [0x%02x] (%s)\n", byte, toBin(byte));
 
  
test1 = 1 & byte;
+
        // GPIO1 data
if(test1==0 && ot1==0) {
+
        // Natuurlijk is GP10-17 (1 = open) GP20-26 (0=open) en GP30-35 (0=open)
printf("%s Voor-tussendeur is open\n",asctime(local));
+
        const BYTE dataIdx = 0xf1;
system("./1-open.sh");
+
        BYTE ot1=0,ot2=0,ot3=0,ot4=0,ot5=0,ot6=0,ot7=0,ot8=0;
ot1=1;
+
        while(1) {
} else if(test1!=0 && ot1==1) {
+
                byte = get(dataIdx);
printf("%s Voor-Tussendeur is closed\n",asctime(local));
+
                local = localtime(&t);
system("./1-close.sh");
+
                if(DEBUG) printf("DATA = [0x%02x] (%s)\n", byte, toBin(byte));
ot1=0;
+
                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);
 +
                w = waitpid(-1, &status, WUNTRACED | WCONTINUED);
 +
        }
  
test2 = 2 & byte;
+
        exit(1); // WTF do we do here?
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?
 
 
}
 
}
</code>
+
</pre>

Huidige versie van 7 mrt 2012 om 18:06

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

#include <stdint.h>
#include <sys/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.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]="        "; // auto 0 termination++
        BYTE bit;                       // always wanted to do that.
        for(bit=0;bit<8;bit++)
                buff[7-bit]=(c>>bit) & 1 ? '1' : '0'; // eigtdoc
        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;
        int docmd=0;
        if(tmp==0 && ot==0) {
                printf("%s: %s is open\n",asctime(local),desc);
                sprintf(cmd,"./%d-open.sh",pin);
                ot=1;
                docmd=1;
        } else if(tmp!=0 && ot==1) {
                printf("%s: %s is closed\n",asctime(local),desc);
                sprintf(cmd,"./%d-close.sh",pin);
                ot=0;
                docmd=1;
        }
        if(docmd) {
                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);
        pid_t w;
        int status;

        (void) signal(SIGKILL,leave);

        iopl(3);

        init();

        if(DEBUG) {
                // check if OK - must be 0x52
                byte = get(0x20);
                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);
                w = waitpid(-1, &status, WUNTRACED | WCONTINUED);
        }

        exit(1); // WTF do we do here?
}