1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-11-14 20:36:03 +01:00
cdf2018-principal/chef/src/ihm.c
2018-04-30 22:40:20 +02:00

295 lines
7.1 KiB
C

#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;
pthread_t tStdinIHM;
// Fonctions
void configureIHM()
{
initLCD();
printToLCD(LCD_LINE_1, "Demarrage");
}
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);
pthread_create(&tStdinIHM, NULL, TaskStdinIHM, NULL);
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;
}
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;
}
enum boutons pressedIHM(int timeout)
{
bool block = timeout < 0;
while (timeout > 0 || block) {
if (stdinbutton != none) {
enum boutons bout = stdinbutton;
stdinbutton = none;
return bout;
}
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;
char* orangeStr = "Orange";
char* vertStr = "Vert";
char* getCouleur()
{
return isOrange ? orangeStr : vertStr;
}
void* TaskIHM(void* pdata)
{
(void)pdata;
enum boutons bout;
for (;;) {
// Debug
for (;;) {
clearLCD();
printfToLCD(LCD_LINE_1, "Debug : %s", isDebug ? "On" : "Off");
if (isDebug) {
printToLCD(LCD_LINE_2, "192.168.0.0 TODO");
}
bout = pressedIHM(IHM_REFRESH_INTERVAL);
if (bout == rouge) {
isDebug = !isDebug;
} else if (bout == jaune) {
break;
}
}
// Couleur
for (;;) {
clearLCD();
printfToLCD(LCD_LINE_1, "Couleur : %s", getCouleur());
bout = pressedIHM(IHM_BLOCK);
if (bout == rouge) {
isOrange = !isOrange;
} else if (bout == jaune) {
break;
}
}
// Calibrage
for (;;) {
clearLCD();
if (lastCalibrage != 0) {
printToLCD(LCD_LINE_1, "Calibre il y a");
printfToLCD(LCD_LINE_2, "%ld secondes", (clock() - lastCalibrage) / CLOCKS_PER_SEC);
} else {
printToLCD(LCD_LINE_1, "Calibrer");
printfToLCD(LCD_LINE_2, "(%s)", getCouleur());
}
bout = pressedIHM(IHM_REFRESH_INTERVAL);
if (bout == rouge) {
clearLCD();
printToLCD(LCD_LINE_1, "Calibrage...");
delay(3000); // TODO
lastCalibrage = clock(); // TODO struct timespec
} else if (bout == jaune) {
break;
}
}
// Diagnostics
for (;;) {
clearLCD();
printToLCD(LCD_LINE_1, "Diagnostiquer");
bout = pressedIHM(IHM_BLOCK);
if (bout == rouge) {
clearLCD();
printToLCD(LCD_LINE_1, "Diagnostics...");
delay(3000); // TODO
} else if (bout == jaune) {
break;
}
}
// Parcours
for (;;) {
clearLCD();
printToLCD(LCD_LINE_1, "Lancer parcours");
printfToLCD(LCD_LINE_2, "(%s)", getCouleur());
bout = pressedIHM(IHM_BLOCK);
if (bout == rouge) {
// No tirette
annuler = false;
while (!tirettePresente()) {
clearLCD();
printToLCD(LCD_LINE_1, "Inserez tirette");
printToLCD(LCD_LINE_2, "(ROUGE: ignorer)");
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();
printToLCD(LCD_LINE_1, "Remettre a zero");
bout = pressedIHM(IHM_BLOCK);
if (bout == rouge) {
clearLCD();
printToLCD(LCD_LINE_1, "Remise a zero...");
delay(3000); // TODO
} else if (bout == jaune) {
break;
}
}
}
}
void deconfigureIHM()
{
clearLCD();
printToLCD(LCD_LINE_1, "Bye bye!");
}