Hallo zusammen!
Experimentiere gerade mit meinem Single-board Computer und bin dabei an
folgende Aufgabenstellung gestoßen. Ich möchte gerne Hardware-GPIO
ansteuern, zum Beispiel eine LED blinken lassen. Nachdem ich z.B. jede
Sekunde die LED ein- und dann wieder anschalten will, aber als
nicht-blockierende Funktion (sprich in der Wartezeit soll das Programm
normal weiterlaufen; die LED als eine Art "Heartbeat" oder
Status-Anzeige) habe ich mir mal POSIX-Threads anschauen.
Ich habe mir mal diese Seite angeschaut
http://softpixel.com/~cwright/programming/threads/threads.c.php und den
Code etwa so modifiziert:
1 | #include <pthread.h>
|
2 | #include <stdio.h>
|
3 |
|
4 | void *threadFunc(void *arg)
|
5 | {
|
6 | while(1)
|
7 | {
|
8 | printf("Switch LED ON\n");
|
9 | usleep(1000000);
|
10 | printf("Switch LED ON\n");
|
11 | usleep(1000000);
|
12 | }
|
13 |
|
14 | return NULL;
|
15 | }
|
16 |
|
17 | int main(void)
|
18 | {
|
19 | pthread_t pth; // this is our thread identifier
|
20 | int i = 0;
|
21 |
|
22 | pthread_create(&pth,NULL,threadFunc,"foo");
|
23 |
|
24 | while(1)
|
25 | {
|
26 | usleep(1);
|
27 | DoSomething();
|
28 | }
|
29 |
|
30 | printf("main waiting for thread to terminate...\n");
|
31 | pthread_join(pth,NULL);
|
32 |
|
33 | return 0;
|
34 | }
|
Leider scheint das mit threadFunc nicht so ganz zu funktionieren. Wenn
ich aber noch ein printf in die while-Schleife der main-Funktion mache
mit einer sinnlosen Ausgabe scheints zu gehen. Kann mir da jemand
helfen?
Danke im Voraus!
Gruß
Matthias