Accelerometer eingebunden #1, Messung läuft in einem Thread
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
#include <libgy521.h>
|
#include <libgy521.h>
|
||||||
#include <math.h> //for fabs
|
#include <math.h> //for fabs
|
||||||
|
|
||||||
|
|
||||||
|
float acceldata = 0;
|
||||||
|
int kill = 0;
|
||||||
|
|
||||||
void setupAccelerometer(void) {
|
void setupAccelerometer(void) {
|
||||||
setupgy521();
|
setupgy521();
|
||||||
}
|
}
|
||||||
@@ -11,7 +15,7 @@ float getaccelerometerdata(void) {
|
|||||||
float temp;
|
float temp;
|
||||||
int i, z;
|
int i, z;
|
||||||
|
|
||||||
for (i = 0, i < 3; i++) {
|
for (i = 0; i < 3; i++) {
|
||||||
for (z = 0; z < 3 - i; z++) {
|
for (z = 0; z < 3 - i; z++) {
|
||||||
if (xyz[z] < xyz[z + 1]) {
|
if (xyz[z] < xyz[z + 1]) {
|
||||||
temp = xyz[z];
|
temp = xyz[z];
|
||||||
@@ -23,3 +27,17 @@ float getaccelerometerdata(void) {
|
|||||||
|
|
||||||
return fabs(xyz[0]); //Absolutwert fuer float
|
return fabs(xyz[0]); //Absolutwert fuer float
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int updateAccelerometerValue(void) {
|
||||||
|
acceldata = getaccelerometerdata();
|
||||||
|
return kill;
|
||||||
|
}
|
||||||
|
|
||||||
|
void killAccelerometerMeasure(void) {
|
||||||
|
kill = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
float getAccelData(void) {
|
||||||
|
return acceldata;
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -1,2 +1,5 @@
|
|||||||
void setupAccelerometer(void);
|
void setupAccelerometer(void);
|
||||||
float getaccelerometerdata(void);
|
float getaccelerometerdata(void);
|
||||||
|
int updateAccelerometerValue(void);
|
||||||
|
void killAccelerometerMeasure(void);
|
||||||
|
float getAccelData(void);
|
||||||
|
21
main.c
21
main.c
@@ -2,23 +2,44 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "song.h"
|
#include "song.h"
|
||||||
|
#include <wiringPi.h>
|
||||||
|
#include "accelerometer.h"
|
||||||
|
|
||||||
|
|
||||||
void shutdown(void) {
|
void shutdown(void) {
|
||||||
//Diese Methode wird von beim beenden aufgerufen.
|
//Diese Methode wird von beim beenden aufgerufen.
|
||||||
printf("Auf Wiedersehen!\n");
|
printf("Auf Wiedersehen!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PI_THREAD (accelerometerupdater) {
|
||||||
|
while (updateAccelerometerValue() == 0) {
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
return 0; //Muss returned werden, da void *
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
int t1;
|
||||||
atexit(shutdown);
|
atexit(shutdown);
|
||||||
if (setupSoftTone() != 0) {
|
if (setupSoftTone() != 0) {
|
||||||
printf("SoftTone konnte nicht initialisiert werden.\n");
|
printf("SoftTone konnte nicht initialisiert werden.\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t1 = piThreadCreate(accelerometerupdater);
|
||||||
|
if (t1 != 0) {
|
||||||
|
printf("Accelerometer Thread nicht gestartet.\n");
|
||||||
|
}
|
||||||
|
|
||||||
//Intro
|
//Intro
|
||||||
play(intro, noteDurations, introlength);
|
play(intro, noteDurations, introlength);
|
||||||
|
|
||||||
|
|
||||||
//Main theme
|
//Main theme
|
||||||
play(melody, melodyNoteDurations, songlength);
|
play(melody, melodyNoteDurations, songlength);
|
||||||
|
|
||||||
|
//Accelerometer Thread beenden
|
||||||
|
killAccelerometerMeasure();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
2
makefile
2
makefile
@@ -2,7 +2,7 @@ VERSION = 1.0
|
|||||||
CC = /usr/bin/gcc
|
CC = /usr/bin/gcc
|
||||||
CFLAGS = -Wall -g -D_REENTRANT -DVERSION=\"$(VERSION)\"
|
CFLAGS = -Wall -g -D_REENTRANT -DVERSION=\"$(VERSION)\"
|
||||||
|
|
||||||
LDFLAGS = -lwiringPi -lpthread
|
LDFLAGS = -lwiringPi -lpthread -lgy521
|
||||||
|
|
||||||
OBJ = main.o player.o accelerometer.o
|
OBJ = main.o player.o accelerometer.o
|
||||||
|
|
||||||
|
8
player.c
8
player.c
@@ -1,6 +1,9 @@
|
|||||||
#include <wiringPi.h>
|
#include <wiringPi.h>
|
||||||
#include <softTone.h>
|
#include <softTone.h>
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include "accelerometer.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define PIN 0 //Realer pin 11
|
#define PIN 0 //Realer pin 11
|
||||||
|
|
||||||
@@ -9,6 +12,7 @@ int setupSoftTone(void) {
|
|||||||
int returncode = 0;
|
int returncode = 0;
|
||||||
returncode += wiringPiSetup(); //Braucht root Rechte
|
returncode += wiringPiSetup(); //Braucht root Rechte
|
||||||
returncode += softToneCreate(PIN);
|
returncode += softToneCreate(PIN);
|
||||||
|
setupAccelerometer();
|
||||||
return returncode; // >0 --> error
|
return returncode; // >0 --> error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +33,9 @@ void play(int *notes, int *durations, int songlength) {
|
|||||||
* Diese wird später mit dem Beschleunigungssensor
|
* Diese wird später mit dem Beschleunigungssensor
|
||||||
* gesteuert. Vorerst sind es aber 30%
|
* gesteuert. Vorerst sind es aber 30%
|
||||||
*/
|
*/
|
||||||
pauseBetweenNotes = nDuration * 1.30;
|
//pauseBetweenNotes = nDuration * 1.30;
|
||||||
|
pauseBetweenNotes = nDuration * 1.30 - (getAccelData() * 38.0);
|
||||||
|
|
||||||
delay(pauseBetweenNotes);
|
delay(pauseBetweenNotes);
|
||||||
// Sicherheitshalber Ton ausschalten
|
// Sicherheitshalber Ton ausschalten
|
||||||
softToneWrite(PIN, 0);
|
softToneWrite(PIN, 0);
|
||||||
|
Reference in New Issue
Block a user