Created
March 11, 2013 12:58
-
-
Save samueltardieu/5134056 to your computer and use it in GitHub Desktop.
Le communication challenge fait en cours ce matin. Et il fonctionne :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ch.h" | |
#include "hal.h" | |
#include "lcd.h" | |
#define SCORED_QUIZZ 1 | |
#define TEST_QUIZZ 2 | |
// Define this as TEST_QUIZZ or SCORED_QUIZZ | |
#define QUIZZ TEST_QUIZZ | |
// Define this as your initials | |
#define INITIALS "5354" | |
#define BAD_QUIZZ (3 - QUIZZ) | |
// void lcd_init(unsigned int prio); | |
// void lcd_char(uint8_t rs, uint8_t byte); | |
static char question[81]; | |
#define ANSWER_DECL(n) \ | |
static char answer ## n[17]; | |
ANSWER_DECL(0); | |
ANSWER_DECL(1); | |
ANSWER_DECL(2); | |
ANSWER_DECL(3); | |
static char * const answers[] = { answer0, answer1, answer2, answer3 }; | |
static void fatal_error(void) { | |
while (1) ; | |
} | |
static bool_t switch_pressed(void) { | |
return palReadPad(GPIOC, GPIOC_SWITCH_L) == PAL_LOW; | |
} | |
static void xbee_putc(char c) { | |
if (c == BAD_QUIZZ) | |
fatal_error(); | |
sdPut(&SD3, c); | |
} | |
static void xbee_puts(const char *s) { | |
while (*s) | |
xbee_putc(*s++); | |
} | |
static void xbee_init(void) { | |
static const SerialConfig XBeeConfig = { 9600, 0, 0, 0 }; | |
sdStart(&SD3, &XBeeConfig); | |
chThdSleepMilliseconds(1100); | |
xbee_puts("+++"); | |
chThdSleepMilliseconds(1100); | |
xbee_puts("ATID5351,MY" INITIALS ",DH0,DL5351,CN\r"); | |
chThdSleepMilliseconds(100); | |
} | |
static char xbee_getc(void) { | |
return sdGet(&SD3); | |
} | |
static size_t xbee_readline(char *buffer, size_t max) { | |
size_t i = 0; | |
while (1) { | |
char c = sdGet(&SD3); | |
if (c == '\n') { | |
buffer[i] = 0; | |
return i; | |
} | |
if (i == max - 1) | |
fatal_error(); | |
buffer[i++] = c; | |
} | |
} | |
static void lcd_putline(int line, const char *str) { | |
// Go to the beginning of the first or second line | |
lcd_char(0, 0x80 | (line ? 0x40 : 0)); | |
// Display up to 16 characters of str then spaces | |
int i; | |
for (i = 0; i < 16 && str[i]; i++) | |
lcd_char(1, str[i]); | |
for (; i < 16; i++) | |
lcd_char(1, ' '); | |
} | |
static void start_quizz(void) { | |
xbee_putc(QUIZZ); | |
while (xbee_getc() != QUIZZ) ; | |
if (xbee_getc() != QUIZZ || xbee_getc() != QUIZZ) | |
fatal_error(); | |
} | |
static void do_question(void) { | |
uint8_t id = xbee_getc(); | |
// Read question and answers | |
size_t qlen = xbee_readline(question, sizeof question); | |
for (int i = 0; i < 4; i++) | |
xbee_readline(answers[i], sizeof answer0); | |
while (switch_pressed()) ; | |
size_t q = 0; | |
int i; | |
for (i = 0;; i = (i + 1) % 4) { | |
lcd_putline(0, question + q); | |
lcd_putline(1, answers[i]); | |
chThdSleepSeconds(1); | |
if (switch_pressed()) | |
break; | |
q += 16; | |
if (q >= qlen) | |
q = 0; | |
} | |
uint8_t answer = (id << 2) | i; | |
xbee_putc(answer); | |
xbee_putc(0); | |
xbee_putc(answer); | |
xbee_putc(0); | |
xbee_putc(answer); | |
} | |
static void display_score(void) { | |
uint8_t score = xbee_getc() & 0xf; | |
lcd_putline(0, "Score:"); | |
if (score == 10) | |
lcd_putline(1, "10"); | |
else { | |
const char s[] = { score + '0', 0 }; | |
lcd_putline(1, s); | |
} | |
} | |
int main(void) { | |
halInit(); | |
chSysInit(); | |
lcd_init(IDLEPRIO + 1); | |
lcd_putline(0, "ROSE 2013"); | |
lcd_putline(1, "Challenge"); | |
xbee_init(); | |
start_quizz(); | |
for (int i = 0; i < 10; i++) | |
do_question(); | |
display_score(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment