initial cleanup and mapping
This commit is contained in:
221
examples/Gertboard/7segments.c
Normal file
221
examples/Gertboard/7segments.c
Normal file
@ -0,0 +1,221 @@
|
||||
/*
|
||||
* 7segments.c:
|
||||
* Simple test program to see if we can drive a 7-segment LED
|
||||
* display using the GPIO and little else on the Raspberry Pi
|
||||
*
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#undef PHOTO_HACK
|
||||
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* Segment mapping
|
||||
*
|
||||
* --a--
|
||||
* | |
|
||||
* f b
|
||||
* | |
|
||||
* --g--
|
||||
* | |
|
||||
* e c
|
||||
* | |
|
||||
* --d-- p
|
||||
*/
|
||||
|
||||
// GPIO Pin Mapping
|
||||
|
||||
static int digits [6] = { 7, 11, 10, 13, 12, 14 } ;
|
||||
static int segments [7] = { 6, 5, 4, 3, 2, 1, 0 } ;
|
||||
|
||||
|
||||
static const int segmentDigits [] =
|
||||
{
|
||||
// a b c d e f g Segments
|
||||
// 6 5 4 3 2 1 0, // wiringPi pin No.
|
||||
|
||||
1, 1, 1, 1, 1, 1, 0, // 0
|
||||
0, 1, 1, 0, 0, 0, 0, // 1
|
||||
1, 1, 0, 1, 1, 0, 1, // 2
|
||||
1, 1, 1, 1, 0, 0, 1, // 3
|
||||
0, 1, 1, 0, 0, 1, 1, // 4
|
||||
1, 0, 1, 1, 0, 1, 1, // 5
|
||||
1, 0, 1, 1, 1, 1, 1, // 6
|
||||
1, 1, 1, 0, 0, 0, 0, // 7
|
||||
1, 1, 1, 1, 1, 1, 1, // 8
|
||||
1, 1, 1, 1, 0, 1, 1, // 9
|
||||
1, 1, 1, 0, 1, 1, 1, // A
|
||||
0, 0, 1, 1, 1, 1, 1, // b
|
||||
1, 0, 0, 1, 1, 1, 0, // C
|
||||
0, 1, 1, 1, 1, 0, 1, // d
|
||||
1, 0, 0, 1, 1, 1, 1, // E
|
||||
1, 0, 0, 0, 1, 1, 1, // F
|
||||
0, 0, 0, 0, 0, 0, 0, // blank
|
||||
} ;
|
||||
|
||||
|
||||
// display:
|
||||
// A global variable which is written to by the main program and
|
||||
// read from by the thread that updates the display. Only the first
|
||||
// 6 characters are used.
|
||||
|
||||
char display [8] ;
|
||||
|
||||
|
||||
/*
|
||||
* displayDigits:
|
||||
* This is our thread that's run concurrently with the main program.
|
||||
* Essentially sit in a loop, parsing and displaying the data held in
|
||||
* the "display" global.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
PI_THREAD (displayDigits)
|
||||
{
|
||||
int digit, segment ;
|
||||
int index, d, segVal ;
|
||||
|
||||
piHiPri (50) ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
for (digit = 0 ; digit < 6 ; ++digit)
|
||||
{
|
||||
for (segment = 0 ; segment < 7 ; ++segment)
|
||||
{
|
||||
d = toupper (display [digit]) ;
|
||||
/**/ if ((d >= '0') && (d <= '9')) // Digit
|
||||
index = d - '0' ;
|
||||
else if ((d >= 'A') && (d <= 'F')) // Hex
|
||||
index = d - 'A' + 10 ;
|
||||
else
|
||||
index = 16 ; // Blank
|
||||
|
||||
segVal = segmentDigits [index * 7 + segment] ;
|
||||
|
||||
digitalWrite (segments [segment], segVal) ;
|
||||
}
|
||||
digitalWrite (digits [digit], 1) ;
|
||||
delay (2) ;
|
||||
digitalWrite (digits [digit], 0) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* setup:
|
||||
* Initialise the hardware and start the thread
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void setup (void)
|
||||
{
|
||||
int i, c ;
|
||||
|
||||
wiringPiSetup () ;
|
||||
|
||||
// 7 segments
|
||||
|
||||
for (i = 0 ; i < 7 ; ++i)
|
||||
{ digitalWrite (segments [i], 0) ; pinMode (segments [i], OUTPUT) ; }
|
||||
|
||||
// 6 digits
|
||||
|
||||
for (i = 0 ; i < 6 ; ++i)
|
||||
{ digitalWrite (digits [i], 0) ; pinMode (digits [i], OUTPUT) ; }
|
||||
|
||||
strcpy (display, " ") ;
|
||||
piThreadCreate (displayDigits) ;
|
||||
delay (10) ; // Just to make sure it's started
|
||||
|
||||
// Quick countdown LED test sort of thing
|
||||
|
||||
c = 999999 ;
|
||||
for (i = 0 ; i < 10 ; ++i)
|
||||
{
|
||||
sprintf (display, "%06d", c) ;
|
||||
delay (400) ;
|
||||
c -= 111111 ;
|
||||
}
|
||||
|
||||
strcpy (display, " ") ;
|
||||
delay (400) ;
|
||||
|
||||
#ifdef PHOTO_HACK
|
||||
sprintf (display, "%s", "123456") ;
|
||||
for (;;)
|
||||
delay (1000) ;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* teenager:
|
||||
* No explanation needed. (Nor one given!)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void teenager (void)
|
||||
{
|
||||
char *message = " feedbeef babe cafe b00b " ;
|
||||
int i ;
|
||||
|
||||
for (i = 0 ; i < strlen (message) - 4 ; ++i)
|
||||
{
|
||||
strncpy (display, &message [i], 6) ;
|
||||
delay (200) ;
|
||||
}
|
||||
delay (1000) ;
|
||||
for (i = 0 ; i < 3 ; ++i)
|
||||
{
|
||||
strcpy (display, " ") ;
|
||||
delay (150) ;
|
||||
strcpy (display, " b00b ") ;
|
||||
delay (250) ;
|
||||
}
|
||||
delay (1000) ;
|
||||
strcpy (display, " ") ;
|
||||
delay (1000) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************
|
||||
* main:
|
||||
* Let the fun begin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int main (void)
|
||||
{
|
||||
struct tm *t ;
|
||||
time_t tim ;
|
||||
|
||||
setup () ;
|
||||
teenager () ;
|
||||
|
||||
tim = time (NULL) ;
|
||||
for (;;)
|
||||
{
|
||||
while (time (NULL) == tim)
|
||||
delay (5) ;
|
||||
|
||||
tim = time (NULL) ;
|
||||
t = localtime (&tim) ;
|
||||
|
||||
sprintf (display, "%02d%02d%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
|
||||
|
||||
delay (500) ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
}
|
74
examples/Gertboard/Makefile
Normal file
74
examples/Gertboard/Makefile
Normal file
@ -0,0 +1,74 @@
|
||||
#
|
||||
# Makefile:
|
||||
# Gertboard - Examples using wiringPi
|
||||
#
|
||||
# Copyright (c) 2013 Gordon Henderson
|
||||
#################################################################################
|
||||
|
||||
#DEBUG = -g -O0
|
||||
DEBUG = -O3
|
||||
CC = gcc
|
||||
INCLUDE = -I/usr/local/include
|
||||
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe
|
||||
|
||||
LDFLAGS = -L/usr/local/lib
|
||||
LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm
|
||||
|
||||
# Should not alter anything below this line
|
||||
###############################################################################
|
||||
|
||||
SRC = gertboard.c \
|
||||
buttons.c 7segments.c \
|
||||
voltmeter.c temperature.c vumeter.c \
|
||||
record.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
BINS = $(SRC:.c=)
|
||||
|
||||
all: $(BINS)
|
||||
|
||||
gertboard: gertboard.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
buttons: buttons.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
7segments: 7segments.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ 7segments.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
voltmeter: voltmeter.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ voltmeter.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
temperature: temperature.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ temperature.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
vumeter: vumeter.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ vumeter.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
record: record.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ record.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
.c.o:
|
||||
@echo [CC] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
@echo [Clean]
|
||||
@rm -f $(OBJ) *~ core tags $(BINS)
|
||||
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
depend:
|
||||
makedepend -Y $(SRC)
|
||||
|
||||
# DO NOT DELETE
|
83
examples/Gertboard/buttons.c
Normal file
83
examples/Gertboard/buttons.c
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* buttons.c:
|
||||
* Read the Gertboard buttons. Each one will act as an on/off
|
||||
* tiggle switch for 3 different LEDs
|
||||
*
|
||||
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wiringPi.h>
|
||||
|
||||
// Array to keep track of our LEDs
|
||||
|
||||
int leds [] = { 0, 0, 0 } ;
|
||||
|
||||
// scanButton:
|
||||
// See if a button is pushed, if so, then flip that LED and
|
||||
// wait for the button to be let-go
|
||||
|
||||
void scanButton (int button)
|
||||
{
|
||||
if (digitalRead (button) == HIGH) // Low is pushed
|
||||
return ;
|
||||
|
||||
leds [button] ^= 1 ; // Invert state
|
||||
digitalWrite (4 + button, leds [button]) ;
|
||||
|
||||
while (digitalRead (button) == LOW) // Wait for release
|
||||
delay (10) ;
|
||||
}
|
||||
|
||||
int main (void)
|
||||
{
|
||||
int i ;
|
||||
|
||||
printf ("Raspberry Pi Gertboard Button Test\n") ;
|
||||
|
||||
wiringPiSetup () ;
|
||||
|
||||
// Setup the outputs:
|
||||
// Pins 3, 4, 5, 6 and 7 output:
|
||||
// We're not using 3 or 4, but make sure they're off anyway
|
||||
// (Using same hardware config as blink12.c)
|
||||
|
||||
for (i = 3 ; i < 8 ; ++i)
|
||||
{
|
||||
pinMode (i, OUTPUT) ;
|
||||
digitalWrite (i, 0) ;
|
||||
}
|
||||
|
||||
// Setup the inputs
|
||||
|
||||
for (i = 0 ; i < 3 ; ++i)
|
||||
{
|
||||
pinMode (i, INPUT) ;
|
||||
pullUpDnControl (i, PUD_UP) ;
|
||||
leds [i] = 0 ;
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
for (i = 0 ; i < 3 ; ++i)
|
||||
scanButton (i) ;
|
||||
delay (1) ;
|
||||
}
|
||||
}
|
96
examples/Gertboard/gertboard.c
Normal file
96
examples/Gertboard/gertboard.c
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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
|
||||
* plot the input value on the terminal as a sort of vertical scrolling
|
||||
* oscilloscipe.
|
||||
*
|
||||
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
// Gertboard D to A is an 8-bit unit.
|
||||
|
||||
#define B_SIZE 256
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <gertboard.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
double angle ;
|
||||
int i, inputValue ;
|
||||
int buffer [B_SIZE] ;
|
||||
int cols ;
|
||||
struct winsize w ;
|
||||
|
||||
|
||||
printf ("Raspberry Pi Gertboard SPI test program\n") ;
|
||||
printf ("=======================================\n") ;
|
||||
|
||||
ioctl (fileno (stdin), TIOCGWINSZ, &w);
|
||||
cols = w.ws_col - 2 ;
|
||||
|
||||
// Always initialise wiringPi. Use wiringPiSys() if you don't need
|
||||
// (or want) to run as root
|
||||
|
||||
wiringPiSetupSys () ;
|
||||
|
||||
// Initialise the Gertboard analog hardware at pin 100
|
||||
|
||||
gertboardAnalogSetup (100) ;
|
||||
|
||||
// Generate a Sine Wave and store in our buffer
|
||||
|
||||
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) ;
|
||||
}
|
||||
|
||||
// Loop, output the sine wave on analog out port 0, read it into A-D port 0
|
||||
// and display it on the screen
|
||||
|
||||
for (;;)
|
||||
{
|
||||
for (i = 0 ; i < B_SIZE ; ++i)
|
||||
{
|
||||
analogWrite (100, buffer [i]) ;
|
||||
|
||||
inputValue = analogRead (100) ;
|
||||
|
||||
// We don't need to wory about the scale or sign - the analog hardware is
|
||||
// a 10-bit value, so 0-1023. Just scale this to our terminal
|
||||
|
||||
printf ("%*s\n", (inputValue * cols) / 1023, "*") ;
|
||||
delay (2) ;
|
||||
}
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
}
|
60
examples/Gertboard/record.c
Normal file
60
examples/Gertboard/record.c
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* record.c:
|
||||
* Record some audio via the Gertboard
|
||||
*
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <gertboard.h>
|
||||
|
||||
#define B_SIZE 40000
|
||||
|
||||
int main ()
|
||||
{
|
||||
int i ;
|
||||
struct timeval tStart, tEnd, tTaken ;
|
||||
unsigned char buffer [B_SIZE] ;
|
||||
|
||||
printf ("\n") ;
|
||||
printf ("Gertboard demo: Recorder\n") ;
|
||||
printf ("========================\n") ;
|
||||
|
||||
// Always initialise wiringPi. Use wiringPiSys() if you don't need
|
||||
// (or want) to run as root
|
||||
|
||||
wiringPiSetupSys () ;
|
||||
|
||||
// Initialise the Gertboard analog hardware at pin 100
|
||||
|
||||
gertboardAnalogSetup (100) ;
|
||||
|
||||
gettimeofday (&tStart, NULL) ;
|
||||
|
||||
for (i = 0 ; i < B_SIZE ; ++i)
|
||||
buffer [i] = analogRead (100) >> 2 ;
|
||||
|
||||
gettimeofday (&tEnd, NULL) ;
|
||||
|
||||
timersub (&tEnd, &tStart, &tTaken) ;
|
||||
|
||||
printf ("Time taken for %d reads: %ld.%ld\n", B_SIZE, tTaken.tv_sec, tTaken.tv_usec) ;
|
||||
|
||||
gettimeofday (&tStart, NULL) ;
|
||||
|
||||
for (i = 0 ; i < B_SIZE ; ++i)
|
||||
analogWrite (100, buffer [i]) ;
|
||||
|
||||
gettimeofday (&tEnd, NULL) ;
|
||||
|
||||
timersub (&tEnd, &tStart, &tTaken) ;
|
||||
|
||||
printf ("Time taken for %d writes: %ld.%ld\n", B_SIZE, tTaken.tv_sec, tTaken.tv_usec) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
78
examples/Gertboard/temperature.c
Normal file
78
examples/Gertboard/temperature.c
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* temperature.c:
|
||||
* Demonstrate use of the Gertboard A to D converter to make
|
||||
* a simple thermometer using the LM35.
|
||||
*
|
||||
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <gertboard.h>
|
||||
|
||||
int main ()
|
||||
{
|
||||
int x1, x2 ;
|
||||
double v1, v2 ;
|
||||
|
||||
printf ("\n") ;
|
||||
printf ("Gertboard demo: Simple Thermemeter\n") ;
|
||||
printf ("==================================\n") ;
|
||||
|
||||
// Always initialise wiringPi. Use wiringPiSys() if you don't need
|
||||
// (or want) to run as root
|
||||
|
||||
wiringPiSetupSys () ;
|
||||
|
||||
// Initialise the Gertboard analog hardware at pin 100
|
||||
|
||||
gertboardAnalogSetup (100) ;
|
||||
|
||||
printf ("\n") ;
|
||||
printf ("| Channel 0 | Channel 1 | Temperature 1 | Temperature 2 |\n") ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
|
||||
// Read the 2 channels:
|
||||
|
||||
x1 = analogRead (100) ;
|
||||
x2 = analogRead (101) ;
|
||||
|
||||
// Convert to a voltage:
|
||||
|
||||
v1 = (double)x1 / 1023.0 * 3.3 ;
|
||||
v2 = (double)x2 / 1023.0 * 3.3 ;
|
||||
|
||||
// Print
|
||||
|
||||
printf ("| %6.3f | %6.3f |", v1, v2) ;
|
||||
|
||||
// Print Temperature of both channels by converting the LM35 reading
|
||||
// to a temperature. Fortunately these are easy: 0.01 volts per C.
|
||||
|
||||
printf (" %4.1f | %4.1f |\r", v1 * 100.0, v2 * 100.0) ;
|
||||
fflush (stdout) ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
73
examples/Gertboard/voltmeter.c
Normal file
73
examples/Gertboard/voltmeter.c
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* voltmeter.c:
|
||||
* Demonstrate use of the Gertboard A to D converter to make
|
||||
* a simple voltmeter.
|
||||
*
|
||||
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <gertboard.h>
|
||||
|
||||
int main ()
|
||||
{
|
||||
int x1, x2 ;
|
||||
double v1, v2 ;
|
||||
|
||||
printf ("\n") ;
|
||||
printf ("Gertboard demo: Simple Voltmeters\n") ;
|
||||
printf ("=================================\n") ;
|
||||
|
||||
// Always initialise wiringPi. Use wiringPiSys() if you don't need
|
||||
// (or want) to run as root
|
||||
|
||||
wiringPiSetupSys () ;
|
||||
|
||||
// Initialise the Gertboard analog hardware at pin 100
|
||||
|
||||
gertboardAnalogSetup (100) ;
|
||||
|
||||
printf ("\n") ;
|
||||
printf ("| Channel 0 | Channel 1 |\n") ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
|
||||
// Read the 2 channels:
|
||||
|
||||
x1 = analogRead (100) ;
|
||||
x2 = analogRead (101) ;
|
||||
|
||||
// Convert to a voltage:
|
||||
|
||||
v1 = (double)x1 / 1023.0 * 3.3 ;
|
||||
v2 = (double)x2 / 1023.0 * 3.3 ;
|
||||
|
||||
// Print
|
||||
|
||||
printf ("| %6.3f | %6.3f |\r", v1, v2) ;
|
||||
fflush (stdout) ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
152
examples/Gertboard/vumeter.c
Normal file
152
examples/Gertboard/vumeter.c
Normal file
@ -0,0 +1,152 @@
|
||||
/*
|
||||
* vumeter.c:
|
||||
* Simple VU meter
|
||||
*
|
||||
* Heres the theory:
|
||||
* We will sample at 4000 samples/sec and put the data into a
|
||||
* low-pass filter with a depth of 1000 samples. This will give
|
||||
* us 1/4 a second of lag on the signal, but I think it might
|
||||
* produce a more pleasing output.
|
||||
*
|
||||
* The input of the microphone should be at mid-pont with no
|
||||
* sound input, but we might have to sample that too, to get
|
||||
* our reference zero...
|
||||
*
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <gertboard.h>
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE (1==1)
|
||||
#define FALSE (!TRUE)
|
||||
#endif
|
||||
|
||||
#define B_SIZE 1000
|
||||
#define S_SIZE 128
|
||||
|
||||
static int buffer [B_SIZE] ;
|
||||
static int bPtr = 0 ;
|
||||
|
||||
/*
|
||||
* ledPercent:
|
||||
* Output the given value as a percentage on the LEDs
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void ledPercent (int percent)
|
||||
{
|
||||
unsigned int output = 0 ;
|
||||
|
||||
if (percent > 11) output |= 0x01 ;
|
||||
if (percent > 22) output |= 0x02 ;
|
||||
if (percent > 33) output |= 0x04 ;
|
||||
if (percent > 44) output |= 0x08 ;
|
||||
if (percent > 55) output |= 0x10 ;
|
||||
if (percent > 66) output |= 0x20 ;
|
||||
if (percent > 77) output |= 0x40 ;
|
||||
if (percent > 88) output |= 0x80 ;
|
||||
|
||||
digitalWriteByte (output) ;
|
||||
}
|
||||
|
||||
static unsigned int tPeriod, tNextSampleTime ;
|
||||
|
||||
/*
|
||||
* sample:
|
||||
* Get a sample from the Gertboard. If not enough time has elapsed
|
||||
* since the last sample, then wait...
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void sample (void)
|
||||
{
|
||||
unsigned int tFuture ;
|
||||
|
||||
// Calculate the future sample time
|
||||
|
||||
tFuture = tPeriod + tNextSampleTime ;
|
||||
|
||||
// Wait until the next sample time
|
||||
|
||||
while (micros () < tNextSampleTime)
|
||||
;
|
||||
|
||||
buffer [bPtr] = gertboardAnalogRead (0) ;
|
||||
|
||||
tNextSampleTime = tFuture ;
|
||||
}
|
||||
|
||||
|
||||
int main ()
|
||||
{
|
||||
int quietLevel, min, max ;
|
||||
int i, sum ;
|
||||
unsigned int tStart, tEnd ;
|
||||
|
||||
printf ("\n") ;
|
||||
printf ("Gertboard demo: VU Meter\n") ;
|
||||
printf ("========================\n") ;
|
||||
|
||||
wiringPiSetup () ;
|
||||
gertboardSPISetup () ;
|
||||
|
||||
ledPercent (0) ;
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
pinMode (i, OUTPUT) ;
|
||||
|
||||
for (bPtr = 0 ; bPtr < B_SIZE ; ++bPtr)
|
||||
buffer [bPtr] = 99 ;
|
||||
|
||||
tPeriod = 1000000 / 1000 ;
|
||||
|
||||
printf ("Shhhh.... ") ; fflush (stdout) ;
|
||||
delay (1000) ;
|
||||
printf ("Sampling quiet... ") ; fflush (stdout) ;
|
||||
|
||||
tStart = micros () ;
|
||||
|
||||
tNextSampleTime = micros () ;
|
||||
for (bPtr = 0 ; bPtr < B_SIZE ; ++bPtr)
|
||||
sample () ;
|
||||
|
||||
tEnd = micros () ;
|
||||
|
||||
quietLevel = 0 ;
|
||||
max = 0 ;
|
||||
min = 1024 ;
|
||||
for (i = 0 ; i < B_SIZE ; ++i)
|
||||
{
|
||||
quietLevel += buffer [i] ;
|
||||
if (buffer [i] > max) max = buffer [i] ;
|
||||
if (buffer [i] < min) min = buffer [i] ;
|
||||
}
|
||||
quietLevel /= B_SIZE ;
|
||||
|
||||
printf ("Done. Quiet level is: %d [%d:%d] [%d:%d]\n", quietLevel, min, max, quietLevel - min, max - quietLevel) ;
|
||||
|
||||
printf ("Time taken for %d reads: %duS\n", B_SIZE, tEnd - tStart) ;
|
||||
|
||||
for (bPtr = 0 ;;)
|
||||
{
|
||||
sample () ;
|
||||
sum = 0 ;
|
||||
for (i = 0 ; i < S_SIZE ; ++i)
|
||||
sum += buffer [i] ;
|
||||
sum /= S_SIZE ;
|
||||
sum = abs (quietLevel - sum) ;
|
||||
sum = (sum * 1000) / quietLevel ;
|
||||
ledPercent (sum) ;
|
||||
if (++bPtr > S_SIZE)
|
||||
bPtr = 0 ;
|
||||
}
|
||||
|
||||
|
||||
return 0 ;
|
||||
}
|
Reference in New Issue
Block a user