Carlos Quintana / Mbed OS Event_Queue1

Dependencies:   TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Keypad.h"
00003 #include "TextLCD.h"
00004 #include "rtos.h"
00005 //<>
00006 
00007 AnalogIn Canal0(A0); //PTB2
00008 AnalogIn Canal1(A1); //PTB3
00009 
00010 DigitalOut   bjtUnd(PTB11,1);
00011 DigitalOut   bjtDec(PTC11,1);
00012 DigitalOut   led(LED_BLUE,1);
00013 DigitalOut   led1(PTA2);
00014 DigitalOut   led2(PTC4);
00015 
00016 DigitalIn    btnInc(PTE25);
00017 DigitalIn    btnDcr(PTE24);
00018 
00019 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00020 EventQueue queue2(32 * EVENTS_EVENT_SIZE);
00021 
00022 InterruptIn btn1(PTB10);
00023 
00024 Keypad  kpad(PTD0,PTD1,PTD2,PTD3, PTC17,PTB9,PTA1,PTB23);
00025 
00026 PortOut  OutC(PortC, 0x000103A3);
00027 
00028 TextLCD LCD(PTC2, PTC3, PTD0, PTD1, PTD2, PTD3); //rs, en, D4, D5, D6, D7
00029 
00030 Thread   th_btn1;
00031 Thread   th_LCD;
00032 Thread   th_Mux;
00033 
00034 Ticker   tk_cuenta; 
00035 Ticker   tk_blink;
00036 Ticker   tk_LCD;
00037 
00038 unsigned char unidades=0, decenas=0;
00039 unsigned int val_display[10] = {0x80, 0x0102A2, 0x0120, 0x010020, 0x010202,
00040                                 0x010001, 0x01, 0x0100A2, 0x00, 0x010000};
00041 char key;
00042 char user[7];
00043 char mask[7];
00044 char clave[] = "6D1#8A";
00045 int released = 1;
00046 int cnt1 = 0;
00047 int i = 0;
00048 int j = 0;
00049 int CH0_Val;
00050 int CH1_Val;
00051 float CH0_Volt;
00052 float CH1_Volt;
00053 
00054 void Clear_Str(){
00055     for (i=0;i<=6;i++){ user[i] = '\0';}
00056     for (i=0;i<=6;i++){ mask[i] = '\0';}  
00057     i = 0; 
00058 }
00059 
00060 void LCD_Show(){
00061     switch (cnt1){
00062         case 0: LCD.locate(0,0);
00063                 LCD.printf("CH0 Valor: %5d",CH0_Val);
00064                 LCD.locate(0,1);
00065                 LCD.printf("CH1 Valor: %5d",CH1_Val);
00066                 break;
00067                     
00068         case 1: LCD.locate(0,0);
00069                 LCD.printf("CH0 Volts: %.3f",CH0_Volt);
00070                 LCD.locate(0,1);
00071                 LCD.printf("CH1 Volts: %.3f",CH1_Volt);
00072                 break;
00073                     
00074         case 2: LCD.locate(0,0);
00075                 LCD.printf("   Led2 State:  ");
00076                 LCD.locate(0,1);
00077                 if (!led2){ LCD.printf("    SHUT OFF    ");}
00078                 else { LCD.printf("    ACTIVATE    ");}
00079                 break;
00080                 
00081         case 3: LCD.locate(0,0);
00082                 LCD.printf("    PASSWORD:   ");
00083                 LCD.locate(0,1);
00084                 LCD.printf("    ");
00085                 LCD.locate(5,1);
00086                 j = 0;
00087                 while (mask[j]=='*'){
00088                     LCD.putc(mask[j]);
00089                     j ++;    
00090                 }
00091                 LCD.locate(11,1);
00092                 LCD.printf("      ");
00093                 break;
00094                 
00095         case 4: if (strcmp(user,clave)==0){ 
00096                     LCD.locate(0,0);
00097                     LCD.printf("     PUERTA      ");
00098                     LCD.locate(0,1);
00099                     LCD.printf("     ABIERTA!    "); 
00100                     led1 = 1;
00101                     wait(2);
00102                     led1 = 0;
00103                 }
00104                 else { LCD.locate(0,0);
00105                        LCD.printf("     CLAVE      ");
00106                        LCD.locate(0,1);
00107                        LCD.printf("   INCORRECTA   ");
00108                        wait(2);
00109                 }
00110                 LCD.cls();
00111                 Clear_Str();
00112                 cnt1 = 0; 
00113                 wait_ms(100);
00114                 break;              
00115     }
00116 }
00117 
00118 void ISR_led2(){
00119     wait_ms(100);
00120     led2 = !led2;
00121 }
00122 
00123 void Blink_led(){
00124     led = !led;
00125 }
00126 
00127 void multiplexado_display()
00128 {
00129     while(1){
00130         OutC.write(val_display[unidades]);
00131         bjtUnd = 0;
00132         bjtDec = 1;
00133         wait_ms(10);
00134         OutC.write(val_display[decenas]);
00135         bjtUnd = 1;
00136         bjtDec = 0;
00137         wait_ms(10);
00138     }       
00139 }
00140 
00141 void contador_up_down()
00142 {
00143     if (!btnInc){
00144         unidades ++;
00145         if (unidades==10){
00146             unidades = 0;
00147             decenas ++;
00148             if (decenas==10){
00149                 decenas = 0;
00150             }
00151         }
00152     }
00153     else { 
00154         if (!btnDcr){
00155             unidades --;
00156             if (unidades==0xFF){
00157                 unidades = 9;
00158                 decenas --;
00159                 if (decenas==0xFF){
00160                     decenas = 9;
00161                 }
00162             }
00163         }
00164     }
00165 }
00166 
00167 int main()
00168 {   
00169     LCD.cls(); 
00170 
00171     th_LCD.start(callback(&queue, &EventQueue::dispatch_forever));
00172     tk_LCD.attach(queue.event(LCD_Show), 0.5);
00173     
00174     btn1.mode(PullUp);
00175     th_btn1.start(callback(&queue2, &EventQueue::dispatch_forever));
00176     btn1.rise(queue2.event(ISR_led2));
00177     
00178     tk_blink.attach(&Blink_led, 0.25);
00179     
00180     th_Mux.start(multiplexado_display);
00181     tk_cuenta.attach(&contador_up_down, 0.25);
00182     
00183     led2 = 0;
00184     Clear_Str();
00185     
00186     while (1) 
00187     {
00188         CH0_Val = Canal0.read_u16();
00189         CH1_Val = Canal1.read_u16();
00190         CH0_Volt = Canal0.read_u16()*3.3/65536;
00191         CH1_Volt = Canal1.read_u16()*3.3/65536;
00192         
00193         key = kpad.ReadKey();
00194 /*
00195         if (key == '\0'){ released = 1;}
00196         if ((key!='\0')&&(released == 1)){
00197             LCD.locate(0,0);
00198             LCD.printf("%c",key);
00199             printf("%c\n",key);
00200             released = 0;
00201         }
00202         led = !led;
00203         wait_ms(100);
00204 */      
00205         if (key == '\0'){ released = 1;}
00206         if ((key!='\0')&&(released == 1)){
00207             if (key == 'C'){
00208                 LCD.cls();
00209                 cnt1 ++;
00210                 if (cnt1 == 3){ i = 0;}
00211                 if (cnt1 == 4){
00212                     cnt1 = 0;
00213                     Clear_Str();
00214                 }
00215                 key = '\0';
00216                 released = 0;
00217             }
00218             else { if (cnt1 == 3){
00219                        if (i == 6){
00220                            if (key == 'D'){ cnt1 = 4;}
00221                        }
00222                        else { user[i] = key;
00223                               mask[i] = '*';
00224                               i ++;
00225                        }
00226                    }
00227             }
00228             key = '\0';
00229             released = 0;
00230         }
00231         wait_ms(100);  
00232     }      
00233 }
00234 
00235 /*
00236 DigitalOut led1(PTA2);
00237 InterruptIn sw(SW2);
00238 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00239 Thread t;
00240 
00241 void rise_handler(void) {
00242     printf("rise_handler in context %p\r\n", Thread::gettid());
00243     // Toggle LED
00244     led1 = !led1;
00245 }
00246 
00247 void fall_handler(void) {
00248     printf("fall_handler in context %p\r\n", Thread::gettid());
00249     // Toggle LED
00250     led1 = !led1;
00251 }
00252 
00253 int main() {
00254     // Start the event queue
00255     t.start(callback(&queue, &EventQueue::dispatch_forever));
00256     printf("Starting in context %p\r\n", Thread::gettid());
00257     // The 'rise' handler will execute in IRQ context
00258     sw.rise(rise_handler);
00259     // The 'fall' handler will execute in the context of thread 't'
00260     sw.fall(queue.event(fall_handler));
00261 }
00262 */