Taylor Nichols / Mbed 2 deprecated kegmo

Dependencies:   4DGL-uLCD-SE TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 #include "TextLCD.h"
00004 #include <mpr121.h>
00005 
00006 // on-board LEDs
00007 DigitalOut led1 ( LED1 ) ;
00008 DigitalOut led2 ( LED2 ) ;
00009 DigitalOut led3 ( LED3 ) ;
00010 DigitalOut led4 ( LED4 ) ;
00011 
00012 //  COM port 
00013 Serial com ( USBTX , USBRX , 9600 ); 
00014 
00015 //  keypad
00016 InterruptIn interrupt ( p26 ) ; 
00017 I2C i2c ( p9 , p10 ) ;
00018 Mpr121 keypad ( & i2c , Mpr121::ADD_VSS ) ; 
00019 
00020 #define LCD_ON 1
00021 
00022 //  LCD ( tx , rx , reset )
00023 #if LCD_ON
00024 uLCD_4DGL lcd ( p28 , p27 , p5 ) ;
00025 #endif
00026 
00027 //  flow meter
00028 InterruptIn pulse_interrupt ( p11 ) ;
00029 int unsigned pulse_count ( 0 ) ;
00030 float pulse_milliLiters ( )
00031 {
00032     float const factor ( 300.0f / 206.0f ) ;
00033     return ( ( float ) pulse_count * factor ) / 0.450f ;
00034 }
00035 void pulse ( ) 
00036 {
00037     ++ pulse_count ;
00038     led4 = ! led4 ;
00039 #if LCD_ON
00040     lcd.locate ( 4 , 8 ) ;
00041     lcd.printf ( "%f mL" , pulse_milliLiters ( ) ) ;
00042 #endif
00043 }
00044 
00045 void read_pin ( )
00046 {
00047 #if LCD_ON
00048     lcd.filled_rectangle ( 0 , 0 , 128 , 128 , BLACK ) ; 
00049     lcd.locate ( 4 , 6 ) ;
00050     lcd.printf ( "Enter PIN" ) ; 
00051     lcd.locate ( 4 , 7 ) ;
00052     lcd.printf ( "on keypad" ) ;
00053 #endif
00054 }
00055 
00056 enum
00057 {
00058     pin_row = 8 , pin_col = 6
00059 } ;
00060 
00061 int unsigned pin_count ( 0 ) ;
00062 int unsigned pin_value ( 0 ) ;
00063 
00064 void keypad_callback ( ) 
00065 {
00066     led2 = ! led2 ;
00067     
00068     int value ( keypad.read(0x00) ) ; value += keypad.read(0x01) << 8 ;
00069 
00070     switch ( value ) // map keys to keypad values
00071     {
00072         case 0 : return ; // key release
00073 
00074         case 0x1 : 
00075             // DELETE
00076 //            lcd.locate ( pin_col + pin_count , pin_row ) ;
00077 //            lcd.printf ( " " ) ;
00078             if ( pin_count != 0 ) 
00079             {
00080                 -- pin_count ;
00081             }
00082             pin_value /= 10 ;
00083             return ; 
00084              
00085         case 0x100 :
00086             // DONE
00087             com.printf ( "a%020f\n" , pulse_milliLiters ( ) ) ;
00088 #if LCD_ON
00089             lcd.locate ( 0 , 10 ) ;
00090             lcd.printf ( "Done pouring." ) ;
00091 #endif 
00092             pulse_count = 0 ;
00093             read_pin ( ) ;
00094             return ;
00095 
00096         case 0x10 : pin_value = pin_value * 10 ; break ; // 0
00097         case 0x2 : pin_value = pin_value * 10 + 1 ; break ; // 1
00098         case 0x20 : pin_value = pin_value * 10 + 2 ; break ; // 2
00099         case 0x200 : pin_value = pin_value * 10 + 3 ; break ; // 3
00100         case 0x4 : pin_value = pin_value * 10 + 4 ; break ; // 4
00101         case 0x40 : pin_value = pin_value * 10 + 5 ; break ; // 5
00102         case 0x400 : pin_value = pin_value * 10 + 6 ; break ; // 6
00103         case 0x8 : pin_value = pin_value * 10 + 7 ; break ; // 7
00104         case 0x80 : pin_value = pin_value * 10 + 8 ; break ; // 8
00105         case 0x800 : pin_value = pin_value * 10 + 9 ; break ; // 9
00106     }
00107     
00108 #if LCD_ON
00109     lcd.locate ( pin_col + pin_count , pin_row ) ;
00110     lcd.printf ( "*" ) ;
00111 #endif 
00112 
00113     if ( pin_count == 3 )
00114     {
00115         com.printf ( "p%04d\n\r" , pin_value ) ; // transmit pin
00116     
00117         pin_value = pin_count = 0 ; // reset pin
00118     
00119         char readbuffer [ 2 ] ;
00120         com.gets ( readbuffer , 2 ) ; // receive verification
00121     
00122 #if LCD_ON
00123         lcd.locate ( 4 , 6 ) ;
00124 #endif
00125         switch ( readbuffer [ 0 ] ) // parse verification
00126         {
00127             case '0' : // INVALID
00128                 led3 = ! led3 ;
00129 #if LCD_ON
00130                 lcd.filled_rectangle ( 0 , 0 , 128 , 128 , RED ) ;  
00131                 lcd.locate ( 4 , 5 ) ;
00132                 lcd.printf ( "INVALID PIN" ) ;
00133                 wait_ms ( 1024 ) ;
00134 #endif 
00135                 read_pin ( ) ;
00136                 break ;
00137                 
00138             case '1' : // VALID
00139                 led4 = ! led4 ;
00140                 char buffer [ 6 ] ;
00141                 com.gets ( buffer , sizeof ( buffer ) ) ;
00142 #if LCD_ON
00143                 lcd.filled_rectangle ( 0 , 0 , 128 , 128 , GREEN ) ;   
00144                 lcd.printf ( "Pour beer" ) ;
00145 //                lcd.locate ( 4 , 8 ) ;
00146                 lcd.locate ( 4 , 10 ) ;
00147                 lcd.printf ( "Current owed: $%s" , buffer ) ;
00148 #endif
00149                 break ;
00150                 
00151             default: // ERROR
00152 #if LCD_ON
00153                 lcd.filled_rectangle ( 0 , 0 , 128 , 128 , YELLOW ) ; 
00154                 lcd.printf ( "TRANSMISSION ERROR" ) ;
00155 #endif
00156                 led2 = ! led2 ;
00157                 exit ( 1 ) ;
00158                 break ;
00159         }
00160         return ;
00161     }
00162     ++ pin_count ; 
00163 }
00164     
00165 int main ( ) 
00166 {
00167     pulse_interrupt.rise ( & pulse ) ;
00168     interrupt.fall ( & keypad_callback ) ;   
00169     interrupt.mode ( PullUp ) ;
00170     
00171     read_pin ( ) ;
00172 
00173     while(1) 
00174     {
00175         led1 = 1;
00176         wait(0.2);
00177         led1 = 0;
00178         wait(0.2);
00179     }
00180 }