1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-06-28 22:19:00 +02:00
cdf2018-principal/chef/src/ihm.c

295 lines
7.1 KiB
C
Raw Normal View History

2018-04-30 16:15:47 +02:00
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <wiringPi.h>
#include "ihm.h"
#include "movement.h"
#include "parcours.h"
#include "points.h"
// Globales
pthread_t tIHM;
2018-04-30 22:40:20 +02:00
pthread_t tStdinIHM;
2018-04-30 16:15:47 +02:00
// Fonctions
void configureIHM()
{
initLCD();
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Demarrage");
2018-04-30 16:15:47 +02:00
}
void startIHM()
{
pinMode(IHM_PIN_ROUGE, INPUT);
pullUpDnControl(IHM_PIN_ROUGE, PUD_UP);
pinMode(IHM_PIN_JAUNE, INPUT);
pullUpDnControl(IHM_PIN_JAUNE, PUD_UP);
pinMode(IHM_PIN_TIRETTE, INPUT);
pullUpDnControl(IHM_PIN_TIRETTE, PUD_UP);
pthread_create(&tIHM, NULL, TaskIHM, NULL);
2018-04-30 22:40:20 +02:00
pthread_create(&tStdinIHM, NULL, TaskStdinIHM, NULL);
2018-04-30 16:15:47 +02:00
pthread_join(tIHM, NULL);
}
// t1 - t2
void diffTimespec(const struct timespec* t1, const struct timespec* t2, struct timespec* td)
{
if ((t1->tv_nsec - t2->tv_nsec) < 0) {
td->tv_sec = t1->tv_sec - t2->tv_sec - 1;
td->tv_nsec = t1->tv_nsec - t2->tv_nsec + 1000000000UL;
} else {
td->tv_sec = t1->tv_sec - t2->tv_sec;
td->tv_nsec = t1->tv_nsec - t2->tv_nsec;
}
}
bool debunkButtonIHM(int pin)
{
int t;
// Press (cancel if wrong)
for (t = IHM_DEBUNK_TIME / 2; t > 0; t--) {
if (digitalRead(pin) != LOW) {
return false;
}
delay(1);
}
// Release (re-wait if wrong)
for (t = IHM_DEBUNK_TIME / 2; t > 0; t--) {
if (digitalRead(pin) != HIGH) {
t = IHM_DEBUNK_TIME / 2;
}
delay(1);
}
return true;
}
2018-04-30 22:40:20 +02:00
enum boutons stdinbutton = none;
void* TaskStdinIHM(void* pdata)
{
(void)pdata;
for (;;) {
char c = getchar();
if (c == '1') {
stdinbutton = jaune;
} else if (c == '2') {
stdinbutton = rouge;
}
}
return NULL;
}
2018-04-30 16:15:47 +02:00
enum boutons pressedIHM(int timeout)
{
bool block = timeout < 0;
while (timeout > 0 || block) {
2018-04-30 22:40:20 +02:00
if (stdinbutton != none) {
enum boutons bout = stdinbutton;
stdinbutton = none;
return bout;
}
2018-04-30 16:15:47 +02:00
if (debunkButtonIHM(IHM_PIN_JAUNE)) {
return jaune;
}
if (debunkButtonIHM(IHM_PIN_ROUGE)) {
return rouge;
}
delay(IHM_POLLING_INTERVAL);
timeout -= IHM_POLLING_INTERVAL;
}
return none;
}
bool tirettePresente()
{
int etat, newEtat;
int t;
for (t = 0; t < IHM_DEBUNK_TIME; t++) {
newEtat = digitalRead(IHM_PIN_TIRETTE);
if (etat != newEtat) {
t = 0;
etat = newEtat;
}
delay(1);
}
return etat == LOW;
}
bool isDebug = false;
bool isOrange = true;
bool annuler = false;
clock_t lastCalibrage = 0;
pthread_t tParcours;
2018-04-30 22:40:20 +02:00
char* orangeStr = "Orange";
char* vertStr = "Vert";
char* getCouleur()
2018-04-30 16:15:47 +02:00
{
2018-04-30 22:40:20 +02:00
return isOrange ? orangeStr : vertStr;
2018-04-30 16:15:47 +02:00
}
void* TaskIHM(void* pdata)
{
(void)pdata;
enum boutons bout;
for (;;) {
// Debug
for (;;) {
clearLCD();
2018-04-30 22:40:20 +02:00
printfToLCD(LCD_LINE_1, "Debug : %s", isDebug ? "On" : "Off");
2018-04-30 16:15:47 +02:00
if (isDebug) {
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_2, "192.168.0.0 TODO");
2018-04-30 16:15:47 +02:00
}
bout = pressedIHM(IHM_REFRESH_INTERVAL);
if (bout == rouge) {
isDebug = !isDebug;
} else if (bout == jaune) {
break;
}
}
// Couleur
for (;;) {
clearLCD();
2018-04-30 22:40:20 +02:00
printfToLCD(LCD_LINE_1, "Couleur : %s", getCouleur());
2018-04-30 16:15:47 +02:00
bout = pressedIHM(IHM_BLOCK);
if (bout == rouge) {
isOrange = !isOrange;
} else if (bout == jaune) {
break;
}
}
// Calibrage
for (;;) {
clearLCD();
if (lastCalibrage != 0) {
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Calibre il y a");
printfToLCD(LCD_LINE_2, "%ld secondes", (clock() - lastCalibrage) / CLOCKS_PER_SEC);
2018-04-30 16:15:47 +02:00
} else {
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Calibrer");
printfToLCD(LCD_LINE_2, "(%s)", getCouleur());
2018-04-30 16:15:47 +02:00
}
bout = pressedIHM(IHM_REFRESH_INTERVAL);
if (bout == rouge) {
clearLCD();
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Calibrage...");
delay(3000); // TODO
lastCalibrage = clock(); // TODO struct timespec
2018-04-30 16:15:47 +02:00
} else if (bout == jaune) {
break;
}
}
// Diagnostics
for (;;) {
clearLCD();
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Diagnostiquer");
2018-04-30 16:15:47 +02:00
bout = pressedIHM(IHM_BLOCK);
if (bout == rouge) {
clearLCD();
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Diagnostics...");
2018-04-30 16:15:47 +02:00
delay(3000); // TODO
} else if (bout == jaune) {
break;
}
}
// Parcours
for (;;) {
clearLCD();
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Lancer parcours");
printfToLCD(LCD_LINE_2, "(%s)", getCouleur());
2018-04-30 16:15:47 +02:00
bout = pressedIHM(IHM_BLOCK);
if (bout == rouge) {
// No tirette
annuler = false;
while (!tirettePresente()) {
clearLCD();
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Inserez tirette");
printToLCD(LCD_LINE_2, "(ROUGE: ignorer)");
2018-04-30 16:15:47 +02:00
bout = pressedIHM(IHM_REFRESH_INTERVAL);
if (bout == rouge) {
break;
} else if (bout == jaune) {
annuler = true;
break;
}
}
if (annuler) {
continue;
}
// Go!
prepareParcours(isOrange);
while (tirettePresente()) {
bout = pressedIHM(IHM_POLLING_INTERVAL);
if (bout == jaune) {
annuler = true;
break;
}
}
if (annuler) {
continue;
}
startParcours(); // TODO On a different thread
int toWait;
while ((toWait = updateParcours()) >= 0) {
if (pressedIHM(toWait) != none) {
break;
}
}
stopParcours();
pressedIHM(IHM_BLOCK); // Nécessite 3 appuis pour éviter d'enlever le score par inadvertance
pressedIHM(IHM_BLOCK);
pressedIHM(IHM_BLOCK);
} else if (bout == jaune) {
break;
}
}
// RàZ
for (;;) {
clearLCD();
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Remettre a zero");
2018-04-30 16:15:47 +02:00
bout = pressedIHM(IHM_BLOCK);
if (bout == rouge) {
clearLCD();
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Remise a zero...");
2018-04-30 16:15:47 +02:00
delay(3000); // TODO
} else if (bout == jaune) {
break;
}
}
}
}
void deconfigureIHM()
{
clearLCD();
2018-04-30 22:40:20 +02:00
printToLCD(LCD_LINE_1, "Bye bye!");
2018-04-30 16:15:47 +02:00
}