Keg sharing system

Dependencies:   4DGL-uLCD-SE TextLCD mbed

main.cpp

Committer:
taylornichols
Date:
2016-12-08
Revision:
1:f531f70a27c3
Parent:
0:dd375f44efef
Child:
2:7d499335dda5

File content as of revision 1:f531f70a27c3:

#include "mbed.h"
#include "uLCD_4DGL.h"
#include "TextLCD.h"
#include <mpr121.h>


// on-board LEDs
DigitalOut led1 ( LED1 ) ;
DigitalOut led2 ( LED2 ) ;
DigitalOut led3 ( LED3 ) ;
DigitalOut led4 ( LED4 ) ;

// COM port 
Serial com ( USBTX , USBRX , 9600 ); 

// keypad
InterruptIn interrupt ( p26 ) ; 
I2C i2c ( p9 , p10 ) ;
Mpr121 keypad ( & i2c , Mpr121::ADD_VSS ) ; 

//  LCD ( tx , rx , reset )
//uLCD_4DGL lcd( p28 , p27 , p30 ); 
//uLCD_4DGL lcd( p28 , p27 , p11 ); 
// text display
//  TextLCD lcd ( p22 , p23 , p24 , p25 , p26 , p27 ) ; // rs, e, d4-d7

//  flow meter
InterruptIn pulse_interrupt ( p5 ) ;
int unsigned pulse_count ( 0 ) ;
void pulse ( ) 
{
    ++ pulse_count ;
//    com.printf ( "PULSE (%d)" , pulse_count ) ; 
//    lcd.locate ( 0 , 4 ) ;
//    lcd.printf ( "PULSE (%d)" , pulse_count ) ; 
}

int unsigned pin_count ( 0 ) ;
int unsigned pin_value ( 0 ) ;
void keypad_callback ( ) 
{
    led2 = ! led2 ;
    
    int value ( keypad.read(0x00) ) ; value += keypad.read(0x01) << 8 ;

    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 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
        case 0x4 : pin_value = pin_value * 10 + 4 ; break ; // 4
        case 0x40 : pin_value = pin_value * 10 + 5 ; break ; // 5
        case 0x400 : pin_value = pin_value * 10 + 6 ; break ; // 6
        case 0x8 : pin_value = pin_value * 10 + 7 ; break ; // 7
        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 ( "*" ) ;
    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 ;
        
        char readbuffer [ 2 ] ;
        com.gets( readbuffer , 2 ) ; // receive verification
        
//        lcd.locate ( 0 , 0 ) ;
        switch ( readbuffer [ 0 ] ) // parse verification
        {
            case '0' : // INVALID
//                lcd.filled_rectangle ( 0 , 0 , 128 , 128 , RED ) ;  
//                lcd.printf ( "INVALID PIN" ) ;
                led3 = ! led3 ;
                break ;
                
            case '1' : // VALID
//                lcd.filled_rectangle ( 0 , 0 , 128 , 128 , GREEN ) ;   
//                lcd.printf ( "PIN ACCEPTED" ) ;
                led4 = ! led4 ;
                char buffer [ 32 ] ;
                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 ) ;
                break ;
                
            default: // ERROR
//                lcd.filled_rectangle ( 0 , 0 , 128 , 128 , YELLOW ) ; 
//                lcd.printf ( "TRANSMISSION ERROR" ) ;
                led2 = ! led2 ;
                break ;
        }
        return ;
    }
    ++ pin_count ; 
}
    
int main ( ) 
{
    //  INIT
    interrupt.fall ( & keypad_callback ) ;   
    interrupt.mode ( PullUp ) ;
    pulse_interrupt.rise ( & pulse ) ;
//    txt.locate ( 0 , 0 ) ;
//    txt.printf ( "INITT" ) ;
//    lcd.locate ( 2 , 2 ) ;
//    lcd.printf ( "INIT" ) ;
//    lcd.set_background ( RED ) ;


    while(1) 
    {
        led1 = 1;
        wait(0.2);
        led1 = 0;
        wait(0.2);
    }
}