![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Keg sharing system
Dependencies: 4DGL-uLCD-SE TextLCD mbed
main.cpp@5:571cd0eea9be, 2016-12-09 (annotated)
- Committer:
- taylornichols
- Date:
- Fri Dec 09 03:18:03 2016 +0000
- Revision:
- 5:571cd0eea9be
- Parent:
- 4:bcb5be37a0e1
#if LCD_ON
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
taylornichols | 0:dd375f44efef | 1 | #include "mbed.h" |
taylornichols | 1:f531f70a27c3 | 2 | #include "uLCD_4DGL.h" |
taylornichols | 1:f531f70a27c3 | 3 | #include "TextLCD.h" |
taylornichols | 0:dd375f44efef | 4 | #include <mpr121.h> |
taylornichols | 0:dd375f44efef | 5 | |
taylornichols | 0:dd375f44efef | 6 | // on-board LEDs |
taylornichols | 0:dd375f44efef | 7 | DigitalOut led1 ( LED1 ) ; |
taylornichols | 0:dd375f44efef | 8 | DigitalOut led2 ( LED2 ) ; |
taylornichols | 0:dd375f44efef | 9 | DigitalOut led3 ( LED3 ) ; |
taylornichols | 0:dd375f44efef | 10 | DigitalOut led4 ( LED4 ) ; |
taylornichols | 0:dd375f44efef | 11 | |
taylornichols | 3:861e3433e5e1 | 12 | // COM port |
taylornichols | 1:f531f70a27c3 | 13 | Serial com ( USBTX , USBRX , 9600 ); |
taylornichols | 0:dd375f44efef | 14 | |
taylornichols | 3:861e3433e5e1 | 15 | // keypad |
taylornichols | 0:dd375f44efef | 16 | InterruptIn interrupt ( p26 ) ; |
taylornichols | 0:dd375f44efef | 17 | I2C i2c ( p9 , p10 ) ; |
taylornichols | 0:dd375f44efef | 18 | Mpr121 keypad ( & i2c , Mpr121::ADD_VSS ) ; |
taylornichols | 0:dd375f44efef | 19 | |
taylornichols | 5:571cd0eea9be | 20 | #define LCD_ON 1 |
taylornichols | 5:571cd0eea9be | 21 | |
taylornichols | 1:f531f70a27c3 | 22 | // LCD ( tx , rx , reset ) |
taylornichols | 5:571cd0eea9be | 23 | #if LCD_ON |
taylornichols | 5:571cd0eea9be | 24 | uLCD_4DGL lcd ( p28 , p27 , p5 ) ; |
taylornichols | 5:571cd0eea9be | 25 | #endif |
taylornichols | 1:f531f70a27c3 | 26 | |
taylornichols | 1:f531f70a27c3 | 27 | // flow meter |
taylornichols | 3:861e3433e5e1 | 28 | InterruptIn pulse_interrupt ( p11 ) ; |
taylornichols | 1:f531f70a27c3 | 29 | int unsigned pulse_count ( 0 ) ; |
taylornichols | 3:861e3433e5e1 | 30 | float pulse_milliLiters ( ) |
taylornichols | 3:861e3433e5e1 | 31 | { |
taylornichols | 5:571cd0eea9be | 32 | float const factor ( 300.0f / 206.0f ) ; |
taylornichols | 5:571cd0eea9be | 33 | return ( ( float ) pulse_count * factor ) / 0.450f ; |
taylornichols | 3:861e3433e5e1 | 34 | } |
taylornichols | 1:f531f70a27c3 | 35 | void pulse ( ) |
taylornichols | 1:f531f70a27c3 | 36 | { |
taylornichols | 1:f531f70a27c3 | 37 | ++ pulse_count ; |
taylornichols | 5:571cd0eea9be | 38 | led4 = ! led4 ; |
taylornichols | 5:571cd0eea9be | 39 | #if LCD_ON |
taylornichols | 5:571cd0eea9be | 40 | lcd.locate ( 4 , 8 ) ; |
taylornichols | 5:571cd0eea9be | 41 | lcd.printf ( "%f mL" , pulse_milliLiters ( ) ) ; |
taylornichols | 5:571cd0eea9be | 42 | #endif |
taylornichols | 2:7d499335dda5 | 43 | } |
taylornichols | 2:7d499335dda5 | 44 | |
taylornichols | 3:861e3433e5e1 | 45 | void read_pin ( ) |
taylornichols | 3:861e3433e5e1 | 46 | { |
taylornichols | 5:571cd0eea9be | 47 | #if LCD_ON |
taylornichols | 5:571cd0eea9be | 48 | lcd.filled_rectangle ( 0 , 0 , 128 , 128 , BLACK ) ; |
taylornichols | 5:571cd0eea9be | 49 | lcd.locate ( 4 , 6 ) ; |
taylornichols | 5:571cd0eea9be | 50 | lcd.printf ( "Enter PIN" ) ; |
taylornichols | 5:571cd0eea9be | 51 | lcd.locate ( 4 , 7 ) ; |
taylornichols | 5:571cd0eea9be | 52 | lcd.printf ( "on keypad" ) ; |
taylornichols | 5:571cd0eea9be | 53 | #endif |
taylornichols | 3:861e3433e5e1 | 54 | } |
taylornichols | 3:861e3433e5e1 | 55 | |
taylornichols | 3:861e3433e5e1 | 56 | enum |
taylornichols | 3:861e3433e5e1 | 57 | { |
taylornichols | 3:861e3433e5e1 | 58 | pin_row = 8 , pin_col = 6 |
taylornichols | 3:861e3433e5e1 | 59 | } ; |
taylornichols | 1:f531f70a27c3 | 60 | |
taylornichols | 0:dd375f44efef | 61 | int unsigned pin_count ( 0 ) ; |
taylornichols | 0:dd375f44efef | 62 | int unsigned pin_value ( 0 ) ; |
taylornichols | 3:861e3433e5e1 | 63 | |
taylornichols | 0:dd375f44efef | 64 | void keypad_callback ( ) |
taylornichols | 0:dd375f44efef | 65 | { |
taylornichols | 0:dd375f44efef | 66 | led2 = ! led2 ; |
taylornichols | 1:f531f70a27c3 | 67 | |
taylornichols | 1:f531f70a27c3 | 68 | int value ( keypad.read(0x00) ) ; value += keypad.read(0x01) << 8 ; |
taylornichols | 1:f531f70a27c3 | 69 | |
taylornichols | 1:f531f70a27c3 | 70 | switch ( value ) // map keys to keypad values |
taylornichols | 0:dd375f44efef | 71 | { |
taylornichols | 1:f531f70a27c3 | 72 | case 0 : return ; // key release |
taylornichols | 3:861e3433e5e1 | 73 | |
taylornichols | 3:861e3433e5e1 | 74 | case 0x1 : |
taylornichols | 3:861e3433e5e1 | 75 | // DELETE |
taylornichols | 4:bcb5be37a0e1 | 76 | // lcd.locate ( pin_col + pin_count , pin_row ) ; |
taylornichols | 4:bcb5be37a0e1 | 77 | // lcd.printf ( " " ) ; |
taylornichols | 3:861e3433e5e1 | 78 | if ( pin_count != 0 ) |
taylornichols | 3:861e3433e5e1 | 79 | { |
taylornichols | 3:861e3433e5e1 | 80 | -- pin_count ; |
taylornichols | 3:861e3433e5e1 | 81 | } |
taylornichols | 3:861e3433e5e1 | 82 | pin_value /= 10 ; |
taylornichols | 3:861e3433e5e1 | 83 | return ; |
taylornichols | 3:861e3433e5e1 | 84 | |
taylornichols | 3:861e3433e5e1 | 85 | case 0x100 : |
taylornichols | 3:861e3433e5e1 | 86 | // DONE |
taylornichols | 3:861e3433e5e1 | 87 | com.printf ( "a%020f\n" , pulse_milliLiters ( ) ) ; |
taylornichols | 5:571cd0eea9be | 88 | #if LCD_ON |
taylornichols | 5:571cd0eea9be | 89 | lcd.locate ( 0 , 10 ) ; |
taylornichols | 5:571cd0eea9be | 90 | lcd.printf ( "Done pouring." ) ; |
taylornichols | 5:571cd0eea9be | 91 | #endif |
taylornichols | 3:861e3433e5e1 | 92 | pulse_count = 0 ; |
taylornichols | 3:861e3433e5e1 | 93 | read_pin ( ) ; |
taylornichols | 3:861e3433e5e1 | 94 | return ; |
taylornichols | 3:861e3433e5e1 | 95 | |
taylornichols | 0:dd375f44efef | 96 | case 0x10 : pin_value = pin_value * 10 ; break ; // 0 |
taylornichols | 1:f531f70a27c3 | 97 | case 0x2 : pin_value = pin_value * 10 + 1 ; break ; // 1 |
taylornichols | 1:f531f70a27c3 | 98 | case 0x20 : pin_value = pin_value * 10 + 2 ; break ; // 2 |
taylornichols | 1:f531f70a27c3 | 99 | case 0x200 : pin_value = pin_value * 10 + 3 ; break ; // 3 |
taylornichols | 1:f531f70a27c3 | 100 | case 0x4 : pin_value = pin_value * 10 + 4 ; break ; // 4 |
taylornichols | 1:f531f70a27c3 | 101 | case 0x40 : pin_value = pin_value * 10 + 5 ; break ; // 5 |
taylornichols | 1:f531f70a27c3 | 102 | case 0x400 : pin_value = pin_value * 10 + 6 ; break ; // 6 |
taylornichols | 1:f531f70a27c3 | 103 | case 0x8 : pin_value = pin_value * 10 + 7 ; break ; // 7 |
taylornichols | 1:f531f70a27c3 | 104 | case 0x80 : pin_value = pin_value * 10 + 8 ; break ; // 8 |
taylornichols | 1:f531f70a27c3 | 105 | case 0x800 : pin_value = pin_value * 10 + 9 ; break ; // 9 |
taylornichols | 0:dd375f44efef | 106 | } |
taylornichols | 3:861e3433e5e1 | 107 | |
taylornichols | 5:571cd0eea9be | 108 | #if LCD_ON |
taylornichols | 5:571cd0eea9be | 109 | lcd.locate ( pin_col + pin_count , pin_row ) ; |
taylornichols | 5:571cd0eea9be | 110 | lcd.printf ( "*" ) ; |
taylornichols | 5:571cd0eea9be | 111 | #endif |
taylornichols | 5:571cd0eea9be | 112 | |
taylornichols | 1:f531f70a27c3 | 113 | if ( pin_count == 3 ) |
taylornichols | 0:dd375f44efef | 114 | { |
taylornichols | 1:f531f70a27c3 | 115 | com.printf ( "p%04d\n\r" , pin_value ) ; // transmit pin |
taylornichols | 3:861e3433e5e1 | 116 | |
taylornichols | 3:861e3433e5e1 | 117 | pin_value = pin_count = 0 ; // reset pin |
taylornichols | 3:861e3433e5e1 | 118 | |
taylornichols | 1:f531f70a27c3 | 119 | char readbuffer [ 2 ] ; |
taylornichols | 3:861e3433e5e1 | 120 | com.gets ( readbuffer , 2 ) ; // receive verification |
taylornichols | 3:861e3433e5e1 | 121 | |
taylornichols | 5:571cd0eea9be | 122 | #if LCD_ON |
taylornichols | 5:571cd0eea9be | 123 | lcd.locate ( 4 , 6 ) ; |
taylornichols | 5:571cd0eea9be | 124 | #endif |
taylornichols | 1:f531f70a27c3 | 125 | switch ( readbuffer [ 0 ] ) // parse verification |
taylornichols | 1:f531f70a27c3 | 126 | { |
taylornichols | 1:f531f70a27c3 | 127 | case '0' : // INVALID |
taylornichols | 1:f531f70a27c3 | 128 | led3 = ! led3 ; |
taylornichols | 5:571cd0eea9be | 129 | #if LCD_ON |
taylornichols | 5:571cd0eea9be | 130 | lcd.filled_rectangle ( 0 , 0 , 128 , 128 , RED ) ; |
taylornichols | 5:571cd0eea9be | 131 | lcd.locate ( 4 , 5 ) ; |
taylornichols | 5:571cd0eea9be | 132 | lcd.printf ( "INVALID PIN" ) ; |
taylornichols | 5:571cd0eea9be | 133 | wait_ms ( 1024 ) ; |
taylornichols | 5:571cd0eea9be | 134 | #endif |
taylornichols | 3:861e3433e5e1 | 135 | read_pin ( ) ; |
taylornichols | 1:f531f70a27c3 | 136 | break ; |
taylornichols | 1:f531f70a27c3 | 137 | |
taylornichols | 1:f531f70a27c3 | 138 | case '1' : // VALID |
taylornichols | 1:f531f70a27c3 | 139 | led4 = ! led4 ; |
taylornichols | 3:861e3433e5e1 | 140 | char buffer [ 6 ] ; |
taylornichols | 1:f531f70a27c3 | 141 | com.gets ( buffer , sizeof ( buffer ) ) ; |
taylornichols | 5:571cd0eea9be | 142 | #if LCD_ON |
taylornichols | 5:571cd0eea9be | 143 | lcd.filled_rectangle ( 0 , 0 , 128 , 128 , GREEN ) ; |
taylornichols | 5:571cd0eea9be | 144 | lcd.printf ( "Pour beer" ) ; |
taylornichols | 5:571cd0eea9be | 145 | // lcd.locate ( 4 , 8 ) ; |
taylornichols | 5:571cd0eea9be | 146 | lcd.locate ( 4 , 10 ) ; |
taylornichols | 5:571cd0eea9be | 147 | lcd.printf ( "Current owed: $%s" , buffer ) ; |
taylornichols | 5:571cd0eea9be | 148 | #endif |
taylornichols | 1:f531f70a27c3 | 149 | break ; |
taylornichols | 1:f531f70a27c3 | 150 | |
taylornichols | 1:f531f70a27c3 | 151 | default: // ERROR |
taylornichols | 5:571cd0eea9be | 152 | #if LCD_ON |
taylornichols | 5:571cd0eea9be | 153 | lcd.filled_rectangle ( 0 , 0 , 128 , 128 , YELLOW ) ; |
taylornichols | 5:571cd0eea9be | 154 | lcd.printf ( "TRANSMISSION ERROR" ) ; |
taylornichols | 5:571cd0eea9be | 155 | #endif |
taylornichols | 1:f531f70a27c3 | 156 | led2 = ! led2 ; |
taylornichols | 3:861e3433e5e1 | 157 | exit ( 1 ) ; |
taylornichols | 1:f531f70a27c3 | 158 | break ; |
taylornichols | 1:f531f70a27c3 | 159 | } |
taylornichols | 1:f531f70a27c3 | 160 | return ; |
taylornichols | 0:dd375f44efef | 161 | } |
taylornichols | 0:dd375f44efef | 162 | ++ pin_count ; |
taylornichols | 0:dd375f44efef | 163 | } |
taylornichols | 0:dd375f44efef | 164 | |
taylornichols | 0:dd375f44efef | 165 | int main ( ) |
taylornichols | 0:dd375f44efef | 166 | { |
taylornichols | 2:7d499335dda5 | 167 | pulse_interrupt.rise ( & pulse ) ; |
taylornichols | 0:dd375f44efef | 168 | interrupt.fall ( & keypad_callback ) ; |
taylornichols | 0:dd375f44efef | 169 | interrupt.mode ( PullUp ) ; |
taylornichols | 3:861e3433e5e1 | 170 | |
taylornichols | 3:861e3433e5e1 | 171 | read_pin ( ) ; |
taylornichols | 0:dd375f44efef | 172 | |
taylornichols | 0:dd375f44efef | 173 | while(1) |
taylornichols | 0:dd375f44efef | 174 | { |
taylornichols | 0:dd375f44efef | 175 | led1 = 1; |
taylornichols | 0:dd375f44efef | 176 | wait(0.2); |
taylornichols | 0:dd375f44efef | 177 | led1 = 0; |
taylornichols | 0:dd375f44efef | 178 | wait(0.2); |
taylornichols | 0:dd375f44efef | 179 | } |
taylornichols | 0:dd375f44efef | 180 | } |