voor willem test

Dependencies:   4DGL MODSERIAL mbed mbos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers keyboard2.cpp Source File

keyboard2.cpp

00001 /* File: keyboard2.cpp   W. Braat */
00002 /* Keyboard chip TCA8418 control */
00003 
00004 #include "mbed.h"
00005 #include "MODSERIAL.h"
00006 #include "keyboard.h"
00007 #include "mbos.h"
00008 #include "mbos_def.h"
00009  
00010 const int CDU_KB_ADRS  = 0x68;  //Base address TCA8418 keypad scanner
00011 const int I2C_ACK  = 0x00;
00012 //const int NACK = 0x01;
00013  
00014 extern int key_hit_ID;
00015 extern mbos CDU_OS;
00016 extern MODSERIAL SERIAL_DEBUG;
00017  
00018 //CDU Keyboard communications KEYBOARD_INT
00019 InterruptIn CDU_KB_INT( p5 );  //Set CDU keyboard interrupt line
00020 I2C CDU_I2C(p28, p27);         //I2C bus for keyboard/temp chip.
00021  
00022 //CDU Keyboard LEDS
00023 DigitalOut EXEC( p12 );
00024 DigitalOut FAIL( p17 );
00025 DigitalOut DSPY( p18 ); 
00026 DigitalOut  MSG( p19 );
00027 DigitalOut OFST( p20 );
00028  
00029 //CDU background lighting
00030 AnalogIn BGL_POT( p16 ); //background light control potmeter
00031 PwmOut BGL_LED( p21 );   //PWM output background lighting
00032 
00033 // ---- added by LvdK : -----------------------------------------------------------
00034 DigitalOut Key_led(LED2); // : LED 2 on Mbed board toggles when CDU key is pressed
00035 // --------------------------------------------------------------------------------
00036  
00037 void CDU_KB_COMM_INIT()
00038 {   //initialize communication with TCA84818
00039     char cmd[2];
00040     
00041     cmd[0] = REG_CFG; //pointer byte to CFG register
00042     cmd[1] = 0x01; //data for CFG register KE_IEN set to 1
00043     if ( CDU_I2C.write(CDU_KB_ADRS,cmd, 2) == I2C_ACK ) //initiate write cycle and check for ACK
00044     {
00045         //intialize all registers from TCA8418 here
00046         cmd[0] = REG_INT_STAT; //pointer byte to Interrupt Status Register
00047         cmd[1] = 0x01; //Reset KE-INT flag      
00048         CDU_I2C.write(CDU_KB_ADRS,cmd, 2 );  //Write to Interrupt Status Register from TCA4818 
00049         
00050         //Set TCA8418 to Keypad mode
00051         cmd[0]=REG_KP_GPIO1; //KP_GIO1
00052         cmd[1]=0xFF; //Set to Keypad mode
00053         CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
00054        
00055         cmd[0]=REG_KP_GPIO2; //KP_GIO2
00056         cmd[1]=0xFF; //Set to Keypad mode
00057         CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
00058       
00059         cmd[0]=REG_KP_GPIO3; //KP_GIO3
00060         cmd[1]=0xFF; //Set to Keypad mode
00061         CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
00062        
00063     }
00064     else
00065     {
00066         //No response from TCA8418 keyboard chip
00067         FAIL = 1; //Switch on FAIL indicator
00068     }  
00069 }
00070  
00071 void CDU_KB_GET_KEY()
00072 {
00073         
00074     Key_led = !Key_led;  // : toggle LED 2
00075           
00076     char cmd[2];
00077     //Read interrupt status flag
00078     cmd[0] = REG_INT_STAT; //pointer byte to Interrupt Status Register
00079     CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
00080     CDU_I2C.read(CDU_KB_ADRS, cmd, 1);  //read key value
00081     
00082     //Read Key Lock and Event Counter
00083     cmd[0] = REG_KEY_LCK_EC; //pointer byte KEY_LCK_EC
00084     CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
00085     CDU_I2C.read(CDU_KB_ADRS, cmd, 1);  //read key value
00086           
00087     //Keypress --> read data from keybuffer
00088     cmd[0] = REG_KEY_EVENT_A; //pointer to Key Event Register KEY_EVENT_A
00089     CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
00090     CDU_I2C.read(CDU_KB_ADRS, cmd, 2);  //read key value (=2 words)
00091     
00092     key_hit_ID =  int(cmd[0]);
00093         SERIAL_DEBUG.printf("keynumber : %d,%d\r\n",key_hit_ID,cmd[1] ); // : TEST only !
00094  
00095     //Reset interrupt flag
00096     cmd[0] = REG_INT_STAT; //pointer byte to Interrupt Status Register
00097     cmd[1] = 0x01; //Reset KE-INT flag
00098     CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
00099     
00100      //Read interrupt status flag   
00101     cmd[0] = 0x02; //pointer byte to Interrupt Status Register
00102     CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
00103     CDU_I2C.read(CDU_KB_ADRS, cmd, 1);  //read interrupt value
00104    
00105     if ( cmd[0] == 1 )
00106     { //reset INT flag failed!
00107         while (1)
00108         {
00109             cmd[0] = 0x04; //pointer to Key Event Register KEY_EVENT_A
00110             CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
00111             CDU_I2C.read(CDU_KB_ADRS, cmd, 2);  //read key value (=2 words)
00112        
00113             //Reset interrupt flag
00114             cmd[0] = 0x02; //pointer byte to Interrupt Status Register
00115             cmd[1] = 0x01; //Reset KE-INT flag
00116             CDU_I2C.write(CDU_KB_ADRS,cmd, 2 );
00117    
00118             //Read interrupt status flag   
00119             cmd[0] = 0x02; //pointer byte to Interrupt Status Register
00120             CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
00121             CDU_I2C.read(CDU_KB_ADRS, cmd, 1);  //read interrupt value
00122            
00123             if ( cmd[0] == 0 ) break;
00124         }   
00125     }
00126 } 
00127  
00128 void SET_KEY_EVENT()
00129 {
00130     // set KEY_EVENT to wakeup SEND_KEYMESSAGE_TASK :
00131     CDU_OS.SetEvent(KEY_EVENT,SEND_KEYMESSAGE_TASK_ID );
00132 }
00133 
00134 void CDU_KB_INT_START()
00135 {
00136     CDU_KB_INT.mode( PullUp );          //Keyboard chip pulls this line to 0 on a keypress
00137     CDU_KB_INT.fall(&SET_KEY_EVENT);   //Bind function to handle interrupt
00138 }
00139 
00140 void CDU_SET_BGL_INTENSITY( int nVal=255 )
00141 {
00142     //This routine must be called 5-10x per second. Manual test to see what is pleasant to see
00143     //AnalogIn BGL_POT( p15 ); //background light control potmeter. Returns a value between 0.0 and 1.0
00144     //PwmOut BGL_LED( p21 );   //PWM output
00145     //calculate required brightness in percentage from 0%-100%
00146     //nVal 255     --> calculate brightness from potmeter value (default value if no parameter is passed)
00147     //nVal = 0     --> switch off backlight
00148     //nVal = 100   --> switch on backlight max
00149  
00150     switch (nVal) 
00151     {
00152         case 0:
00153         {
00154             //switch off backlighting
00155             BGL_LED.pulsewidth( 0.0 );
00156             break;
00157         }
00158         case 100:
00159         {
00160             //switch on backlighting
00161             BGL_LED.pulsewidth( 100.0 );
00162             break;
00163         }
00164         case 255:
00165         {
00166             //calculate percentage from potmeter value
00167         if ( BGL_POT < 0.01 )
00168             {
00169                 BGL_LED = 0.0; //prevents flickering when low intensity
00170             }
00171         else
00172             {
00173                 BGL_LED = BGL_POT; 
00174             }
00175         }
00176     }
00177 }