Преглед изворни кода

Changed wiringPiISR so callback gets pin#

pull/158/head
Jim Parziale пре 2 година
родитељ
комит
00b873cdce
13 измењених фајлова са 181 додато и 127 уклоњено
  1. +49
    -17
      examples/Makefile
  2. +7
    -18
      examples/clock.c
  3. +1
    -1
      examples/delayTest.c
  4. +1
    -1
      examples/isr-osc.c
  5. +8
    -8
      examples/isr.c
  6. +1
    -1
      examples/isr1.c
  7. +1
    -1
      examples/lowPower.c
  8. +0
    -0
      examples/servo-reset.sh
  9. +2
    -0
      examples/spiSpeed.c
  10. +102
    -75
      examples/wfi.c
  11. +1
    -1
      gpio/gpio.c
  12. +7
    -3
      wiringPi/wiringPi.c
  13. +1
    -1
      wiringPi/wiringPi.h

+ 49
- 17
examples/Makefile Прегледај датотеку

@@ -38,23 +38,55 @@ LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt
# Should not alter anything below this line
###############################################################################

SRC = blink.c blink8.c blink12.c \
blink12drcs.c \
pwm.c \
speed.c wfi.c isr.c isr-osc.c \
lcd.c lcd-adafruit.c clock.c \
nes.c \
softPwm.c softTone.c \
delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \
lowPower.c \
max31855.c \
rht03.c \
servo.c \
isr1.c \
test1.c \
test2.c \
buttons.c buttons2.c \
getBanks.c
#SRC = blink.c blink8.c blink12.c \
# blink12drcs.c \
# pwm.c \
# speed.c wfi.c isr.c isr-osc.c \
# lcd.c lcd-adafruit.c clock.c \
# nes.c \
# softPwm.c softTone.c \
# delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \
# lowPower.c \
# max31855.c \
# rht03.c \
# servo.c \
# isr1.c \
# buttons.c buttons2.c \
# getBanks.c

SRC = \
blink12.c \
blink12drcs.c \
blink6drcs.c \
blink8.c \
blink8-drcn.c \
blink.c \
blink-thread.c \
buttons2.c \
buttons.c \
clock.c \
delayTest.c \
ds1302.c \
getBanks.c \
isr1.c \
isr.c \
isr-osc.c \
lcd-adafruit.c \
lcd.c \
lowPower.c \
max31855.c \
nes.c \
okLed.c \
pwm.c \
rht03.c \
serialRead.c \
serialTest.c \
servo.c \
softPwm.c \
softTone.c \
speed.c \
spiSpeed.c \
wfi.c \

OBJ = $(SRC:.c=.o)



+ 7
- 18
examples/clock.c Прегледај датотеку

@@ -78,8 +78,7 @@ void drawClockHands (void)
if (h > 12)
h -= 12 ;

// Hour hand

// Hour hand
angle = h * 30 + m * 0.5 ;
x0 = sin (rads (angle)) * (clockRadius * 0.75) ;
y0 = cos (rads (angle)) * (clockRadius * 0.75) ;
@@ -91,8 +90,7 @@ void drawClockHands (void)
lcd128x64lineTo (x0, y0, 1) ;
}

// Minute hand

// Minute hand
angle = m * 6 ;
x0 = sin (rads (angle)) * (clockRadius * 0.9) ;
y0 = cos (rads (angle)) * (clockRadius * 0.9) ;
@@ -104,8 +102,7 @@ void drawClockHands (void)
lcd128x64lineTo (x0, y0, 1) ;
}

// Second hand

// Second hand
angle = s * 6 ;
x0 = sin (rads (angle)) * (clockRadius * 0.2) ;
y0 = cos (rads (angle)) * (clockRadius * 0.2) ;
@@ -115,12 +112,11 @@ void drawClockHands (void)
lcd128x64circle (0, 0, clockRadius * 0.1, 0, 1) ;
lcd128x64circle (0, 0, clockRadius * 0.05, 1, 1) ;

// Text:

// Text:
sprintf (text, "%02d:%02d:%02d", h24, m, s) ;
lcd128x64puts (32, 24, text, 0, 1) ;

sprintf (text, "%2d/%2d/%2d", now->tm_mday, now->tm_mon + 1, now->tm_year - 100) ;
sprintf (text, "%2d/%2d/%2d", (uint8_t)now->tm_mday, (uint8_t)now->tm_mon + 1, (uint8_t)(now->tm_year - 100)) ;
lcd128x64puts (32, -23, text, 0, 1) ;
}

@@ -133,16 +129,14 @@ void drawClockFace (void)
lcd128x64circle (0,0, clockRadius, 1, TRUE) ;
lcd128x64circle (0,0, clockRadius - thickness, 0, TRUE) ;

// The four big indicators for 12,15,30 and 45

// The four big indicators for 12,15,30 and 45
lcd128x64rectangle (- 3, clockRadius - barLen, 3, clockRadius, 1, TRUE) ; // 12
lcd128x64rectangle (clockRadius - barLen, 3, clockRadius, -3, 1, TRUE) ; // 3
lcd128x64rectangle (- 3, -clockRadius + barLen, 3, -clockRadius, 1, TRUE) ; // 6
lcd128x64rectangle (-clockRadius + barLen, 3, -clockRadius, -3, 1, TRUE) ; // 9


// Smaller 5 and 1 minute ticks

// Smaller 5 and 1 minute ticks
for (m = 0 ; m < 60 ; ++m)
{
px1 = sin (rads (m * 6)) * clockRadius ;
@@ -167,15 +161,11 @@ void setup (void)
lcd128x64setOrigin (32, 32) ;
}




/*
***********************************************************************
* The main program
***********************************************************************
*/

int main (int argc, char *argv [])
{
time_t now ;
@@ -195,7 +185,6 @@ int main (int argc, char *argv [])
while (time (NULL) == now)
delayMs(10) ;
}

return 0 ;
}

+ 1
- 1
examples/delayTest.c Прегледај датотеку

@@ -37,7 +37,7 @@ int main()
int t ;
int max, min ;
int del ;
int underRuns, overRuns, exactRuns, bogusRuns, total ;
int underRuns, overRuns, exactRuns, total;
int descheds ;




+ 1
- 1
examples/isr-osc.c Прегледај датотеку

@@ -73,7 +73,7 @@ static void Signal_handler(int sig);
*********************************************************************************
*/

void myInterrupt (void)
void myInterrupt (int pin)
{
digitalWrite (OUT_PIN, 1) ;
++globalCounter;


+ 8
- 8
examples/isr.c Прегледај датотеку

@@ -58,14 +58,14 @@ static void Signal_handler(int sig);
*********************************************************************************
*/

void myInterrupt0 (void) { ++globalCounter [0] ; }
void myInterrupt1 (void) { ++globalCounter [1] ; }
void myInterrupt2 (void) { ++globalCounter [2] ; }
void myInterrupt3 (void) { ++globalCounter [3] ; }
void myInterrupt4 (void) { ++globalCounter [4] ; }
void myInterrupt5 (void) { ++globalCounter [5] ; }
void myInterrupt6 (void) { ++globalCounter [6] ; }
void myInterrupt7 (void) { ++globalCounter [7] ; }
void myInterrupt0 (int pin) { ++globalCounter [0] ; }
void myInterrupt1 (int pin) { ++globalCounter [1] ; }
void myInterrupt2 (int pin) { ++globalCounter [2] ; }
void myInterrupt3 (int pin) { ++globalCounter [3] ; }
void myInterrupt4 (int pin) { ++globalCounter [4] ; }
void myInterrupt5 (int pin) { ++globalCounter [5] ; }
void myInterrupt6 (int pin) { ++globalCounter [6] ; }
void myInterrupt7 (int pin) { ++globalCounter [7] ; }


/*


+ 1
- 1
examples/isr1.c Прегледај датотеку

@@ -68,7 +68,7 @@ static void Signal_handler(int sig);
*********************************************************************************
*/

void myInterrupt(void) {
void myInterrupt(int pin) {
// Only report button release on actual button release.
// This interrupt sometimes fires for the wrong edge!



+ 1
- 1
examples/lowPower.c Прегледај датотеку

@@ -40,7 +40,7 @@
*********************************************************************************
*/

void lowPower (void)
void lowPower (int pin)
{
time_t t ;



+ 0
- 0
examples/servo-reset.sh Прегледај датотеку


+ 2
- 0
examples/spiSpeed.c Прегледај датотеку

@@ -36,8 +36,10 @@
#include <wiringPi.h>
#include <wiringPiSPI.h>

#ifndef TRUE
#define TRUE (1==1)
#define FALSE (!TRUE)
#endif

#define SPI_CHAN 0
#define NUM_TIMES 100


+ 102
- 75
examples/wfi.c Прегледај датотеку

@@ -1,21 +1,21 @@
/*
* wfi.c:
* Wait for Interrupt test program
* Wait for Interrupt test program
*
* This program demonstrates the use of the waitForInterrupt()
* function in wiringPi. It listens to a button input on
* BCM_GPIO pin 17 (wiringPi pin 0)
* This program demonstrates the use of the waitForInterrupt()
* function in wiringPi. It listens to a button input on
* BCM_GPIO pin 17 (wiringPi pin 0)
*
* The biggest issue with this method is that it really only works
* well in Sys mode.
* The biggest issue with this method is that it really only works
* well in Sys mode.
*
* Jan 2013: This way of doing things is sort of deprecated now, see
* the wiringPiISR() function instead and the isr.c test program here.
* Jan 2013: This way of doing things is sort of deprecated now, see
* the wiringPiISR() function instead and the isr.c test program here.
*
* Copyright (c) 2012-2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://github.com/WiringPi/WiringPi/
* https://github.com/WiringPi/WiringPi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
@@ -34,131 +34,158 @@

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>

#include <wiringPi.h>

static int terminate_process = 0;
static void Signal_handler(int sig);

// A 'key' which we can lock and unlock - values are 0 through 3
// This is interpreted internally as a pthread_mutex by wiringPi
// which is hiding some of that to make life simple.
// This is interpreted internally as a pthread_mutex by wiringPi
// which is hiding some of that to make life simple.

#define COUNT_KEY 0
#define COUNT_KEY 0

// What BCM_GPIO input are we using?

#define BUTTON_PIN 17
#define BUTTON_PIN 26

// Debounce time in mS

#define DEBOUNCE_TIME 100
#define DEBOUNCE_TIME 100


// globalCounter:
// Global variable to count interrupts
// Should be declared volatile to make sure the compiler doesn't cache it.
// Global variable to count interrupts
// Should be declared volatile to make sure the compiler doesn't cache it.

static volatile int globalCounter = 0 ;
static volatile int globalCounter = 0;


/*
* waitForIt:
* This is a thread created using the wiringPi simplified threading
* mechanism. It will wait on an interrupt on the button and increment
* a counter.
* This is a thread created using the wiringPi simplified threading
* mechanism. It will wait on an interrupt on the button and increment
* a counter.
*********************************************************************************
*/

PI_THREAD (waitForIt)
{
int state = 0 ;
int debounceTime = 0 ;
int state = 0;
int debounceTime = 0;

(void)piHiPri (10) ; // Set this thread to be high priority
(void)piHiPri (10); // Set this thread to be high priority

for (;;)
(void)digitalRead (BUTTON_PIN); // pre-read pin to avoid false trigger

while (!terminate_process)
{
if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it
if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it
{
// Bouncing?

if (millis () < debounceTime)
// Bouncing?
if (millis() < debounceTime)
{
debounceTime = millis () + DEBOUNCE_TIME ;
continue ;
debounceTime = millis() + DEBOUNCE_TIME;
continue;
}

// We have a valid one
// We have a valid one
state ^= 1;

state ^= 1 ;

piLock (COUNT_KEY) ;
++globalCounter ;
piUnlock (COUNT_KEY) ;

// Wait for key to be released
piLock (COUNT_KEY);
++globalCounter;
piUnlock (COUNT_KEY);

// Wait for key to be released
while (digitalRead (BUTTON_PIN) == LOW)
delayMs (1) ;
{
delayMs (10);
}

debounceTime = millis () + DEBOUNCE_TIME ;
debounceTime = millis() + DEBOUNCE_TIME;
}
}

return (void *) NULL ;
return (void *) NULL;
}


/*
* setup:
* Demo a crude but effective way to initialise the hardware
* main
*********************************************************************************
*/

void setup (void)
int main (void)
{
int lastCounter = 0;
int myCounter = 0;

// Use the gpio program to initialise the hardware
// (This is the crude, but effective)

system ("gpio edge 17 falling") ;

// Setup wiringPi
// ----------------------------------
// Demo a crude but effective way to initialise the hardware

wiringPiSetupSys () ;
// Use the gpio program to initialise the hardware
// (This is the crude, but effective)
system ("gpio edge 26 falling");

// Fire off our interrupt handler
// Setup wiringPi
wiringPiSetupSys();

piThreadCreate (waitForIt) ;
// Fire off our interrupt handler
int tid = piThreadCreate (waitForIt);

}
// ----------------------------------

// Set the handler for SIGTERM (15)
signal(SIGTERM, Signal_handler);
signal(SIGINT, Signal_handler);
signal(SIGQUIT, Signal_handler);
signal(SIGTRAP, Signal_handler);
signal(SIGABRT, Signal_handler);
signal(SIGALRM, Signal_handler);
signal(SIGUSR1, Signal_handler);
signal(SIGUSR2, Signal_handler);

/*
* main
*********************************************************************************
*/

int main (void)
{
int lastCounter = 0 ;
int myCounter = 0 ;

setup () ;

for (;;)
while (myCounter < 25)
{
printf ("Waiting ... ");
fflush (stdout) ;
fflush (stdout);

while (myCounter == lastCounter)
{
piLock (COUNT_KEY) ;
myCounter = globalCounter ;
piUnlock (COUNT_KEY) ;
delayMs(500) ;
piLock (COUNT_KEY);
myCounter = globalCounter;
piUnlock (COUNT_KEY);
delayMs(75);
}
if (terminate_process) break;

printf (" Done. myCounter: %5d\n", myCounter) ;
lastCounter = myCounter ;
printf (" Done. myCounter: %5d\n", myCounter);
lastCounter = myCounter;
}
pthread_join(tid, NULL);

return 0 ;
printf ("Done.\n");

return EXIT_SUCCESS;
}

//**********************************************************************************************************************

/**
* Intercepts and handles signals from QNX
* This function is called when the SIGTERM signal is raised by QNX
*/
void Signal_handler(int sig)
{
printf("Received signal %d\n", sig);

// Signal process to exit.
terminate_process = 1;

globalCounter++;
}

//**********************************************************************************************************************

+ 1
- 1
gpio/gpio.c Прегледај датотеку

@@ -499,7 +499,7 @@ void doExport (int argc, char *argv[])
* to exit the program. Crude but effective.
*********************************************************************************
*/
static void wfi (void)
static void wfi (int pin)
{
exit (EXIT_SUCCESS);
}


+ 7
- 3
wiringPi/wiringPi.c Прегледај датотеку

@@ -315,7 +315,7 @@ static int sysFds[64] =
};

// ISR Data
static void (*isrFunctions[64])(void);
static void (*isrFunctions[64])(int pin);


// Doing it the Arduino way with lookup tables...
@@ -1851,8 +1851,12 @@ static void *interruptHandler (UNU void *arg)
pinPass = -1;

for (;;)
{
if (waitForInterrupt (myPin, -1) > 0)
isrFunctions[myPin] ();
{
isrFunctions[myPin](myPin);
}
}

return NULL;
}
@@ -1866,7 +1870,7 @@ static void *interruptHandler (UNU void *arg)
* Returns 0 on success, -1 on failure (or program exits if !wiringPiReturnCodes)
*********************************************************************************
*/
int wiringPiISR (int pin, int mode, void (*function)(void))
int wiringPiISR (int pin, int mode, void (*function)(int))
{
pthread_t threadId;
const char *modeS;


+ 1
- 1
wiringPi/wiringPi.h Прегледај датотеку

@@ -269,7 +269,7 @@ extern void digitalWriteByte2 (int value);

// Interrupts (Also Pi hardware specific)
extern int waitForInterrupt (int pin, int mS); // Must be in SYS mode (wiringPiSetupSys was called for setup)
extern int wiringPiISR (int pin, int mode, void (*function)(void)); // Uses SYS mode, and also waitForInterrupt
extern int wiringPiISR (int pin, int mode, void (*function)(int)); // Uses SYS mode, and also waitForInterrupt

// Threads
extern int piThreadCreate (void *(*fn)(void *));


Loading…
Откажи
Сачувај