Merged from git.drogon.net, SPI driver helpers, C++ wrappers, softPwm, piNes, gertboard, SPI

This commit is contained in:
Gordon Henderson
2012-08-28 18:37:54 +01:00
committed by Philip Howard
parent 21f0472265
commit ae40bdaf6a
22 changed files with 1708 additions and 279 deletions

View File

@@ -35,11 +35,11 @@ LIBS = -lwiringPi
# Should not alter anything below this line
###############################################################################
SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c
SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c gertboard.c nes.c delayTest.c softPwm.c
OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o
OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o gertboard.o nes.o delayTest.o softPwm.o
all: test1 test2 speed lcd wfi piface
all: test1 test2 speed lcd wfi piface gertboard nes softPwm
test1: test1.o
@echo [link]
@@ -65,13 +65,30 @@ piface: piface.o
@echo [link]
$(CC) -o $@ piface.o $(LDFLAGS) $(LIBS) -lpthread
gertboard: gertboard.o
@echo [link]
$(CC) -o $@ gertboard.o $(LDFLAGS) $(LIBS) -lm
nes: nes.o
@echo [link]
$(CC) -o $@ nes.o $(LDFLAGS) $(LIBS) -lm
softPwm: softPwm.o
@echo [link]
$(CC) -o $@ softPwm.o $(LDFLAGS) $(LIBS) -lm -lpthread
delayTest: delayTest.o
@echo [link]
$(CC) -o $@ delayTest.o $(LDFLAGS) $(LIBS)
.c.o:
@echo [CC] $<
@$(CC) -c $(CFLAGS) $< -o $@
clean:
rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface
rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface gertboard nes delayTest softPwm
tags: $(SRC)
@echo [ctags]

68
examples/delayTest.c Normal file
View File

@@ -0,0 +1,68 @@
#include <stdio.h>
#include <unistd.h>
#include <wiringPi.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#define CYCLES 1000
#define DELAY 99
int main()
{
int x ;
struct timeval t1, t2 ;
long long t ;
unsigned int max, min ;
unsigned int values [CYCLES] ;
max = 0 ;
min = 1000000 ;
if (wiringPiSetup () == -1)
return 1 ;
piHiPri (10) ;
sleep (1) ;
// Baseline test
gettimeofday (&t1, NULL) ;
gettimeofday (&t2, NULL) ;
t = t2.tv_usec - t1.tv_usec ;
printf ("Baseline test: %lld\n", t);
for (x = 0 ; x < CYCLES ; ++x)
{
gettimeofday (&t1, NULL) ;
delayMicroseconds (DELAY) ;
gettimeofday (&t2, NULL) ;
t = t2.tv_usec - t1.tv_usec ;
if (t > max) max = t ;
if (t < min) min = t ;
values [x] = t ;
}
printf ("Done: Max: %d, min: %d\n", max, min) ;
for (x = 0 ; x < CYCLES ; ++x)
{
printf ("%4d", values [x]) ;
if (values [x] > DELAY)
printf (".") ;
else if (values [x] < DELAY)
printf ("-") ;
else
printf (" ") ;
if (((x + 1) % 20) == 0)
printf ("\n") ;
}
printf ("\n") ;
return 0 ;
}

77
examples/gertboard.c Normal file
View File

@@ -0,0 +1,77 @@
/*
* gertboard.c:
* Simple test for the SPI bus on the Gertboard
*
* Hardware setup:
* D/A port 0 jumpered to A/D port 0.
*
* We output a sine wave on D/A port 0 and sample A/D port 0. We then
* copy this value to D/A port 1 and use a 'scope on both D/A ports
* to check all's well.
*
*/
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#define B_SIZE 200
#undef DO_TIMING
#include <wiringPi.h>
#include <gertboard.h>
int main (void)
{
double angle ;
int i ;
uint32_t x1 ;
int buffer [B_SIZE] ;
#ifdef DO_TIMING
unsigned int now, then ;
#endif
printf ("Raspberry Pi Gertboard SPI test program\n") ;
if (wiringPiSetupSys () < 0)
return -1 ;
if (gertboardSPISetup () < 0)
return 1 ;
// Generate a Sine Wave
for (i = 0 ; i < B_SIZE ; ++i)
{
angle = ((double)i / (double)B_SIZE) * M_PI * 2.0 ;
buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ;
}
for (;;)
{
#ifdef DO_TIMING
then = millis () ;
#endif
for (i = 0 ; i < B_SIZE ; ++i)
{
gertboardAnalogWrite (0, buffer [i]) ;
#ifndef DO_TIMING
x1 = gertboardAnalogRead (0) ;
gertboardAnalogWrite (1, x1 >> 2) ; // 10-bit A/D, 8-bit D/A
#endif
}
#ifdef DO_TIMING
now = millis () ;
printf ("%4d mS, %9.7f S/sample", now - then, ((double)(now - then) / 1000.0) / (double)B_SIZE) ;
printf (" -> %9.4f samples/sec \n", 1 / (((double)(now - then) / 1000.0) / (double)B_SIZE)) ;
#endif
}
return 0 ;
}

BIN
examples/gertboard.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

44
examples/nes.c Normal file
View File

@@ -0,0 +1,44 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <piNes.h>
#define BLANK "| "
int main ()
{
int joystick ;
unsigned int buttons ;
if (wiringPiSetup () == -1)
{
fprintf (stdout, "oops: %s\n", strerror (errno)) ;
return 1 ;
}
if ((joystick = setupNesJoystick (2, 1, 0)) == -1)
{
fprintf (stdout, "Unable to setup joystick\n") ;
return 1 ;
}
for (;;)
{
buttons = readNesJoystick (joystick) ;
if ((buttons & NES_UP) != 0) printf ("| UP " ) ; else printf (BLANK) ;
if ((buttons & NES_DOWN) != 0) printf ("| DOWN " ) ; else printf (BLANK) ;
if ((buttons & NES_LEFT) != 0) printf ("| LEFT " ) ; else printf (BLANK) ;
if ((buttons & NES_RIGHT) != 0) printf ("|RIGHT " ) ; else printf (BLANK) ;
if ((buttons & NES_SELECT) != 0) printf ("|SELECT" ) ; else printf (BLANK) ;
if ((buttons & NES_START) != 0) printf ("|START " ) ; else printf (BLANK) ;
if ((buttons & NES_A) != 0) printf ("| A " ) ; else printf (BLANK) ;
if ((buttons & NES_B) != 0) printf ("| B " ) ; else printf (BLANK) ;
printf ("|\n") ;
}
return 0 ;
}

69
examples/softPwm.c Normal file
View File

@@ -0,0 +1,69 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <softPwm.h>
#define RANGE 100
#define NUM_LEDS 12
int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 } ;
int values [NUM_LEDS] = { 0, 17, 32, 50, 67, 85, 100, 85, 67, 50, 32, 17 } ;
int main ()
{
int i, j ;
char buf [80] ;
if (wiringPiSetup () == -1)
{
fprintf (stdout, "oops: %s\n", strerror (errno)) ;
return 1 ;
}
for (i = 0 ; i < NUM_LEDS ; ++i)
{
softPwmCreate (ledMap [i], 0, RANGE) ;
printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ;
}
fgets (buf, 80, stdin) ;
// Bring all up one by one:
for (i = 0 ; i < NUM_LEDS ; ++i)
for (j = 0 ; j <= 100 ; ++j)
{
softPwmWrite (ledMap [i], j) ;
delay (10) ;
}
fgets (buf, 80, stdin) ;
// Down fast
for (i = 100 ; i > 0 ; --i)
{
for (j = 0 ; j < NUM_LEDS ; ++j)
softPwmWrite (ledMap [j], i) ;
delay (10) ;
}
fgets (buf, 80, stdin) ;
for (;;)
{
for (i = 0 ; i < NUM_LEDS ; ++i)
softPwmWrite (ledMap [i], values [i]) ;
delay (50) ;
i = values [0] ;
for (j = 0 ; j < NUM_LEDS - 1 ; ++j)
values [j] = values [j + 1] ;
values [NUM_LEDS - 1] = i ;
}
}