Keg sharing system

Dependencies:   4DGL-uLCD-SE TextLCD mbed

Revision:
3:861e3433e5e1
Parent:
2:7d499335dda5
Child:
4:bcb5be37a0e1
--- a/main.cpp	Fri Dec 09 00:21:58 2016 +0000
+++ b/main.cpp	Fri Dec 09 02:01:55 2016 +0000
@@ -3,49 +3,54 @@
 #include "TextLCD.h"
 #include <mpr121.h>
 
-
 // on-board LEDs
 DigitalOut led1 ( LED1 ) ;
 DigitalOut led2 ( LED2 ) ;
 DigitalOut led3 ( LED3 ) ;
 DigitalOut led4 ( LED4 ) ;
 
-// COM port 
+//  COM port 
 Serial com ( USBTX , USBRX , 9600 ); 
 
-// keypad
+//  keypad
 InterruptIn interrupt ( p26 ) ; 
 I2C i2c ( p9 , p10 ) ;
 Mpr121 keypad ( & i2c , Mpr121::ADD_VSS ) ; 
 
 //  LCD ( tx , rx , reset )
-uLCD_4DGL lcd(p28,p27,p5); // serial tx, serial rx, reset pin;
-
-//uLCD_4DGL lcd( p28 , p27 , p30 ); 
-//uLCD_4DGL lcd( p28 , p27 , p11 ); 
-// text display
-
-//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7
-//TextLCD lcd ( p22 , p23 , p24 , p25 , p26 , p27 ) ; // rs, e, d4-d7
+uLCD_4DGL lcd ( p28 , p27 , p5 ) ;
 
 //  flow meter
-
-
-InterruptIn pulse_interrupt ( p5 ) ;
-
+InterruptIn pulse_interrupt ( p11 ) ;
 int unsigned pulse_count ( 0 ) ;
-
+float pulse_milliLiters ( )
+{
+    return ( ( float ) pulse_count ) / 0.450f ;
+}
 void pulse ( ) 
 {
     ++ pulse_count ;
+    lcd.locate ( 4 , 8 ) ;
+    lcd.printf ( "%f mL" , pulse_milliLiters ( ) ) ;
 }
 
-//    com.printf ( "PULSE (%d)" , pulse_count ) ; 
-//    lcd.locate ( 0 , 4 ) ;
-//    lcd.printf ( "PULSE (%d)" , pulse_count ) ; 
+void read_pin ( )
+{
+    lcd.filled_rectangle ( 0 , 0 , 128 , 128 , BLACK ) ; 
+    lcd.locate ( 4 , 6 ) ;
+    lcd.printf ( "Enter PIN" ) ; 
+    lcd.locate ( 4 , 7 ) ;
+    lcd.printf ( "on keypad" ) ;
+}
+
+enum
+{
+    pin_row = 8 , pin_col = 6
+} ;
 
 int unsigned pin_count ( 0 ) ;
 int unsigned pin_value ( 0 ) ;
+
 void keypad_callback ( ) 
 {
     led2 = ! led2 ;
@@ -55,13 +60,28 @@
     switch ( value ) // map keys to keypad values
     {
         case 0 : return ; // key release
-        case 0x1 : if ( pin_count != 0 ) -- pin_count ; pin_value /= 10 ; return ; // DELETE
+
+        case 0x1 : 
+            // DELETE
+            lcd.locate ( pin_col + pin_count , pin_row ) ;
+            lcd.printf ( " " ) ;
+            if ( pin_count != 0 ) 
+            {
+                -- pin_count ;
+            }
+            pin_value /= 10 ;
+            return ; 
+             
+        case 0x100 :
+            // DONE
+            com.printf ( "a%020f\n" , pulse_milliLiters ( ) ) ;
+            lcd.locate ( 0 , 10 ) ;
+            lcd.printf ( "Done pouring." ) ;
+            pulse_count = 0 ;
+            read_pin ( ) ;
+            return ;
+
         case 0x10 : pin_value = pin_value * 10 ; break ; // 0
-        case 0x100 : // DONE
-            float const milliLiters ( ( ( float ) pulse_count ) / 0.450f ) ;
-            com.printf ( "a%020f\n" , milliLiters ) ;
-            pulse_count = 0 ;
-            return ;
         case 0x2 : pin_value = pin_value * 10 + 1 ; break ; // 1
         case 0x20 : pin_value = pin_value * 10 + 2 ; break ; // 2
         case 0x200 : pin_value = pin_value * 10 + 3 ; break ; // 3
@@ -72,47 +92,48 @@
         case 0x80 : pin_value = pin_value * 10 + 8 ; break ; // 8
         case 0x800 : pin_value = pin_value * 10 + 9 ; break ; // 9
     }
-//    lcd.locate ( pin_count , 4 ) ;
-//    lcd.printf ( "*" ) ;
+    
+    lcd.locate ( pin_col + pin_count , pin_row ) ;
+    lcd.printf ( "*" ) ;
+    
     if ( pin_count == 3 )
     {
-//        lcd.locate ( 0 , 0 ) ;
-  //      lcd.locate( 4 , 5 ) ;
-//        lcd.printf ( "Pin: %d" , pin_value ) ;        
         com.printf ( "p%04d\n\r" , pin_value ) ; // transmit pin
-        pin_value = pin_count = 0 ;
-//        return ;
-        
+    
+        pin_value = pin_count = 0 ; // reset pin
+    
         char readbuffer [ 2 ] ;
-        com.gets( readbuffer , 2 ) ; // receive verification
+        com.gets ( readbuffer , 2 ) ; // receive verification
+    
+        lcd.locate ( 4 , 6 ) ;
         
-//        lcd.locate ( 0 , 0 ) ;
         switch ( readbuffer [ 0 ] ) // parse verification
         {
             case '0' : // INVALID
-//                lcd.filled_rectangle ( 0 , 0 , 128 , 128 , RED ) ;  
-//                lcd.printf ( "INVALID PIN" ) ;
+                lcd.filled_rectangle ( 0 , 0 , 128 , 128 , RED ) ;  
+                lcd.locate ( 4 , 5 ) ;
+                lcd.printf ( "INVALID PIN" ) ;
                 led3 = ! led3 ;
+                wait_ms ( 1024 ) ;
+                read_pin ( ) ;
                 break ;
                 
             case '1' : // VALID
-//                lcd.filled_rectangle ( 0 , 0 , 128 , 128 , GREEN ) ;   
-//                lcd.printf ( "PIN ACCEPTED" ) ;
+                lcd.filled_rectangle ( 0 , 0 , 128 , 128 , GREEN ) ;   
+                lcd.printf ( "Pour beer" ) ;
+                lcd.locate ( 4 , 8 ) ;
                 led4 = ! led4 ;
-                char buffer [ 32 ] ;
+                char buffer [ 6 ] ;
                 com.gets ( buffer , sizeof ( buffer ) ) ;
-//                lcd.printf ( "You owe $%s" , buffer ) ;
-//                float const owed ( atof ( buffer ) ) ;
-//                lcd.locate ( 0 , 6 ) ;
-//                lcd.printf ( 
-                // charge account
-//                com.printf ( "Poured %d milliliters of beer.\n" , milliliters ) ;
+                lcd.locate ( 4 , 10 ) ;
+                lcd.printf ( "Current owed: $%s" , buffer ) ;
                 break ;
                 
             default: // ERROR
-//                lcd.filled_rectangle ( 0 , 0 , 128 , 128 , YELLOW ) ; 
-//                lcd.printf ( "TRANSMISSION ERROR" ) ;
+                lcd.filled_rectangle ( 0 , 0 , 128 , 128 , YELLOW ) ; 
+                lcd.printf ( "TRANSMISSION ERROR" ) ;
                 led2 = ! led2 ;
+                exit ( 1 ) ;
                 break ;
         }
         return ;
@@ -122,25 +143,11 @@
     
 int main ( ) 
 {
-    lcd.printf("\nHello uLCD World\n"); //Default Green on black text
-    lcd.printf("\n  Starting Demo...");
-//    return 0 ;
     pulse_interrupt.rise ( & pulse ) ;
-    
-    // ...
-    
-//    lcd.locate(0,0);
-//    lcd.printf("tst");
-    
-    //  INIT
     interrupt.fall ( & keypad_callback ) ;   
     interrupt.mode ( PullUp ) ;
-//    txt.locate ( 0 , 0 ) ;
-//    txt.printf ( "INITT" ) ;
-//    lcd.locate ( 2 , 2 ) ;
-//    lcd.printf ( "INIT" ) ;
-//    lcd.set_background ( RED ) ;
-
+    
+    read_pin ( ) ;
 
     while(1) 
     {