Hangman game using qp a 16x2 LCD and joystick.

Dependencies:   TextLCD mbed qp

Files at this revision

API Documentation at this revision

Comitter:
tylerjw
Date:
Thu Feb 09 03:57:44 2012 +0000
Parent:
0:1521c946a57b
Commit message:
Problems with qp library...
TODO: BSP for button and joystick then main.cpp

Changed in this revision

bsp.cpp Show annotated file Show diff for this revision Revisions of this file
bsp.h Show annotated file Show diff for this revision Revisions of this file
hangman.h Show annotated file Show diff for this revision Revisions of this file
host.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
player.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 1521c946a57b -r 4efaebc256d3 bsp.cpp
--- a/bsp.cpp	Wed Feb 08 22:20:11 2012 +0000
+++ b/bsp.cpp	Thu Feb 09 03:57:44 2012 +0000
@@ -4,6 +4,8 @@
 #include "LPC17xx.h"
 #include "mbed.h"
 
+Q_DEFINE_THIS_FILE
+
 enum ISR_Priorities { // ISR priorities from highest urgency to lowest
     BUTTON_PRIO,
     SYSTICK_PRIO,
diff -r 1521c946a57b -r 4efaebc256d3 bsp.h
--- a/bsp.h	Wed Feb 08 22:20:11 2012 +0000
+++ b/bsp.h	Thu Feb 09 03:57:44 2012 +0000
@@ -1,9 +1,9 @@
 #ifndef bsp_h
 #define bsp_h
 
-#include "TextLCD.h"
+#define BSP_TICKS_PER_SEC   100     // system clock tick rate
 
-#define BSP_TICKS_PER_SEC   100     // system clock tick rate
+#include "TextLCD.h"
 
 TextLCD lcd(p10, p11, p17, p18, p19, p20); // rs, e, d4-d7
 
diff -r 1521c946a57b -r 4efaebc256d3 hangman.h
--- a/hangman.h	Wed Feb 08 22:20:11 2012 +0000
+++ b/hangman.h	Thu Feb 09 03:57:44 2012 +0000
@@ -4,15 +4,14 @@
 #include "bsp.h"
 
 enum HangmanSignals {
-    START_SIG,
+    START_SIG = 1,
     PLAY_SIG,
     SELECT_SIG,
-    WIN_SIG,
-    LOOSE_SIG,
     FINISHED_SIG,
     TERMINATE_SIG,
     MAX_PUB_SIG,
     
+    BUTTON_SIG,
     SCROLL_SIG,
     MAX_SIG
 };
diff -r 1521c946a57b -r 4efaebc256d3 host.cpp
--- a/host.cpp	Wed Feb 08 22:20:11 2012 +0000
+++ b/host.cpp	Thu Feb 09 03:57:44 2012 +0000
@@ -2,6 +2,8 @@
 #include "hangman.h"
 #include "bsp.h"
 
+Q_DEFINE_THIS_FILE
+
 class Host : public QActive {
 private:
     char* word;
@@ -151,8 +153,8 @@
                 if (strpbrk(me->word,"abcdefghijklmnopqrs") == NULL) { // win!
                     BSP_lcdScrollIn(win_msg, press_play_msg); // win msg
                     // post play to player
-                    HostEvt *pe = Q_NEW(HostEvt, WIN_SIG);
-                    pe->scroll_pos = 0; // center
+                    HostEvt *pe = Q_NEW(HostEvt, FINISHED_SIG);
+                    pe->scroll_pos = 1; // win
                     QF::PUBLISH(pe, me);
 
                     // go to welcome state
@@ -162,8 +164,8 @@
                 if (++me->incorrect_letters >= MAX_LETTERS) { // loose
                     BSP_lcdScrollIn(loose_msg, press_play_msg); // message
                     // post play to player
-                    HostEvt *pe = Q_NEW(HostEvt, WIN_SIG);
-                    pe->scroll_pos = 0; // center
+                    HostEvt *pe = Q_NEW(HostEvt, FINISHED_SIG);
+                    pe->scroll_pos = 0; // loss
                     QF::PUBLISH(pe, me);
 
                     // go to welcome state
diff -r 1521c946a57b -r 4efaebc256d3 main.cpp
--- a/main.cpp	Wed Feb 08 22:20:11 2012 +0000
+++ b/main.cpp	Thu Feb 09 03:57:44 2012 +0000
@@ -1,4 +1,8 @@
-#include "mbed.h"
+#include "qp_port.h"
+#include "hangman.h"
+#include "bsp.h"
+
+Q_DEFINE_THIS_FILE
 
 DigitalOut myled(LED1);
 
diff -r 1521c946a57b -r 4efaebc256d3 player.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player.cpp	Thu Feb 09 03:57:44 2012 +0000
@@ -0,0 +1,100 @@
+#include "qp_port.h"
+#include "dpp.h"
+#include "bsp.h"
+
+Q_DEFINE_THIS_FILE
+
+class Player : public QActive {
+private:
+    QTimeEvt m_timeEvt;                       // to timeout update joystick pos
+    uint8_t wins;
+    uint8_t losses;
+
+public:
+    Player();
+
+private:
+    static QState initial (Player *me, QEvent const *e);
+    static QState thinking(Player *me, QEvent const *e);
+    static QState playing (Player *me, QEvent const *e);
+};
+
+static Player l_player;
+
+#define JOY_TIME    (BSP_TICKS_PER_SEC/5) // update joystick position every 5 ticks
+
+enum InternalSignals {                                     // internal signals
+    TIMEOUT_SIG = MAX_SIG
+};
+
+QActive * const AO_Player = l_player;
+
+//............................................................................
+Player::Player()
+    : QActive((QStateHandler)&Player::initial),
+      m_timeEvt(TIMEOUT_SIG)
+{}
+//............................................................................
+QState Player::initial(Player *me, QEvent const *) {
+    static uint8_t registered;            // starts off with 0, per C-standard
+    if (!registered) {
+        QS_OBJ_DICTIONARY(&l_player);
+        QS_OBJ_DICTIONARY(&l_player.m_timeEvt);
+        
+        QS_FUN_DICTIONARY(&Player::initial);
+        QS_FUN_DICTIONARY(&Player::thinking);
+        QS_FUN_DICTIONARY(&Player::playing);
+        
+        registered = (uint8_t)1;
+    }
+    
+    QS_SIG_DICTIONARY(PLAY_SIG, me);               // signal for each Players
+    QS_SIG_DICTIONARY(FINISHED_SIG, me);           // signal for each Players
+    
+    // init wins and losses ctr
+    wins = losses = 0;
+    
+    return Q_TRAN(&Player::thinking); // go to thinking
+}
+//............................................................................
+QState Player::thinking(Player *me, QEvent const *) {
+    switch (e->sig) {
+        case Q_ENTRY_SIG: {
+        // TODO : INITIALIZE BTN
+        }
+        case BUTTON_SIG: {
+            return Q_TRAN(&Player::playing); // go to playing state
+        }
+    }
+    return Q_SUPER(&QHsm::top);
+}
+//............................................................................
+QState Player::playing(Player *me, QEvent const *) {
+    switch (e->sig) {
+        case Q_ENTRY_SIG: {
+            me->m_timeEvt.postIn(me, JOY_TIME); // init joystick update (TIMEOUT_SIG)
+            // TODO : INITIALIZE BTN
+            return Q_HANDLED();
+        }
+        case TIMEOUT_SIG: {
+            int pos = BSP_joyUpdate();
+            HostEvt *pe = Q_NEW(HostEvt, SCROLL_SIG);
+            pe->scroll_pos = pos;
+            QF::PUBLISH(pe, me);
+        }
+        case BUTTON_SIG: {
+            HostEvt *pe = Q_NEW(HostEvt, SELECT_SIG);
+            pe->scroll_pos = 0; // no change
+            QF::PUBLISH(pe, me);
+        }
+        case FINISHED_SIG: {
+            if(((HostEvt *)e)->scroll_pos == 1) // win
+                wins++;
+            else
+                losses++;
+            
+            return Q_TRAN(&Player::thinking); // go to thinking state   
+        }                
+    }
+    return Q_SUPER(&QHsm::top);
+}
\ No newline at end of file