Check this one ! mbos setup changed, WTX hor pos changed, no font style check, no checksum check

Dependencies:   4DGL MODSERIAL mbed mbos

Fork of CDU_Mbed_21 by Engravity-CDU

Committer:
WillemBraat
Date:
Mon Jul 21 14:24:15 2014 +0000
Revision:
11:d60c746c097c
Parent:
10:7e350a27f936
screen_handler.cpp
; Added external flag S_Exec in procedure . Inhibits screen updates when 1. S_Exec is declared in line 77 of FS2CDU_data_5.cpp
;
; keyboard2.cpp
; Line 40. Added Key_led=0
;
; keyboard2.cpp "
; Added switch value in CDU_SET_BGL_INTENSIT...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LvdK 7:6576a287e563 1 /* File: keyboard2.cpp W. Braat */
LvdK 7:6576a287e563 2 /* Keyboard chip TCA8418 control */
LvdK 7:6576a287e563 3
LvdK 7:6576a287e563 4 #include "mbed.h"
LvdK 7:6576a287e563 5 #include "MODSERIAL.h"
LvdK 7:6576a287e563 6 #include "keyboard.h"
LvdK 7:6576a287e563 7 #include "mbos.h"
LvdK 8:422544d24df3 8 #include "mbos_def2.h"
LvdK 7:6576a287e563 9
LvdK 7:6576a287e563 10 const int CDU_KB_ADRS = 0x68; //Base address TCA8418 keypad scanner
LvdK 7:6576a287e563 11 const int I2C_ACK = 0x00;
LvdK 7:6576a287e563 12 //const int NACK = 0x01;
LvdK 7:6576a287e563 13
LvdK 7:6576a287e563 14 extern int key_hit_ID;
LvdK 7:6576a287e563 15 extern mbos CDU_OS;
LvdK 7:6576a287e563 16 extern MODSERIAL SERIAL_DEBUG;
LvdK 7:6576a287e563 17
LvdK 7:6576a287e563 18 //CDU Keyboard communications KEYBOARD_INT
LvdK 7:6576a287e563 19 InterruptIn CDU_KB_INT( p5 ); //Set CDU keyboard interrupt line
LvdK 7:6576a287e563 20 I2C CDU_I2C(p28, p27); //I2C bus for keyboard/temp chip.
LvdK 7:6576a287e563 21
LvdK 7:6576a287e563 22 //CDU Keyboard LEDS
LvdK 7:6576a287e563 23 DigitalOut EXEC( p12 );
LvdK 7:6576a287e563 24 DigitalOut FAIL( p17 );
LvdK 7:6576a287e563 25 DigitalOut DSPY( p18 );
LvdK 7:6576a287e563 26 DigitalOut MSG( p19 );
LvdK 7:6576a287e563 27 DigitalOut OFST( p20 );
LvdK 7:6576a287e563 28
LvdK 7:6576a287e563 29 //CDU background lighting
LvdK 7:6576a287e563 30 AnalogIn BGL_POT( p16 ); //background light control potmeter
LvdK 7:6576a287e563 31 PwmOut BGL_LED( p21 ); //PWM output background lighting
LvdK 7:6576a287e563 32
LvdK 7:6576a287e563 33 // ---- added by LvdK : -----------------------------------------------------------
LvdK 7:6576a287e563 34 DigitalOut Key_led(LED2); // : LED 2 on Mbed board toggles when CDU key is pressed
LvdK 7:6576a287e563 35 // --------------------------------------------------------------------------------
LvdK 7:6576a287e563 36
LvdK 7:6576a287e563 37 void CDU_KB_COMM_INIT()
LvdK 7:6576a287e563 38 { //initialize communication with TCA84818
LvdK 7:6576a287e563 39 char cmd[2];
WillemBraat 11:d60c746c097c 40 Key_led = 0;
LvdK 7:6576a287e563 41 cmd[0] = REG_CFG; //pointer byte to CFG register
LvdK 7:6576a287e563 42 cmd[1] = 0x01; //data for CFG register KE_IEN set to 1
LvdK 7:6576a287e563 43 if ( CDU_I2C.write(CDU_KB_ADRS,cmd, 2) == I2C_ACK ) //initiate write cycle and check for ACK
LvdK 7:6576a287e563 44 {
LvdK 7:6576a287e563 45 //intialize all registers from TCA8418 here
LvdK 7:6576a287e563 46 cmd[0] = REG_INT_STAT; //pointer byte to Interrupt Status Register
LvdK 7:6576a287e563 47 cmd[1] = 0x01; //Reset KE-INT flag
LvdK 7:6576a287e563 48 CDU_I2C.write(CDU_KB_ADRS,cmd, 2 ); //Write to Interrupt Status Register from TCA4818
LvdK 7:6576a287e563 49
LvdK 7:6576a287e563 50 //Set TCA8418 to Keypad mode
LvdK 7:6576a287e563 51 cmd[0]=REG_KP_GPIO1; //KP_GIO1
LvdK 7:6576a287e563 52 cmd[1]=0xFF; //Set to Keypad mode
LvdK 7:6576a287e563 53 CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
LvdK 7:6576a287e563 54
LvdK 7:6576a287e563 55 cmd[0]=REG_KP_GPIO2; //KP_GIO2
LvdK 7:6576a287e563 56 cmd[1]=0xFF; //Set to Keypad mode
LvdK 7:6576a287e563 57 CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
LvdK 7:6576a287e563 58
LvdK 7:6576a287e563 59 cmd[0]=REG_KP_GPIO3; //KP_GIO3
LvdK 7:6576a287e563 60 cmd[1]=0xFF; //Set to Keypad mode
LvdK 7:6576a287e563 61 CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
LvdK 7:6576a287e563 62
LvdK 7:6576a287e563 63 }
LvdK 7:6576a287e563 64 else
LvdK 7:6576a287e563 65 {
LvdK 7:6576a287e563 66 //No response from TCA8418 keyboard chip
LvdK 7:6576a287e563 67 FAIL = 1; //Switch on FAIL indicator
LvdK 7:6576a287e563 68 }
LvdK 7:6576a287e563 69 }
LvdK 7:6576a287e563 70
LvdK 7:6576a287e563 71 void CDU_KB_GET_KEY()
LvdK 7:6576a287e563 72 {
LvdK 7:6576a287e563 73
LvdK 7:6576a287e563 74 Key_led = !Key_led; // : toggle LED 2
LvdK 7:6576a287e563 75
LvdK 7:6576a287e563 76 char cmd[2];
LvdK 7:6576a287e563 77 //Read interrupt status flag
LvdK 7:6576a287e563 78 cmd[0] = REG_INT_STAT; //pointer byte to Interrupt Status Register
LvdK 7:6576a287e563 79 CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
LvdK 7:6576a287e563 80 CDU_I2C.read(CDU_KB_ADRS, cmd, 1); //read key value
LvdK 7:6576a287e563 81
LvdK 7:6576a287e563 82 //Read Key Lock and Event Counter
LvdK 7:6576a287e563 83 cmd[0] = REG_KEY_LCK_EC; //pointer byte KEY_LCK_EC
LvdK 7:6576a287e563 84 CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
LvdK 7:6576a287e563 85 CDU_I2C.read(CDU_KB_ADRS, cmd, 1); //read key value
LvdK 7:6576a287e563 86
LvdK 7:6576a287e563 87 //Keypress --> read data from keybuffer
LvdK 7:6576a287e563 88 cmd[0] = REG_KEY_EVENT_A; //pointer to Key Event Register KEY_EVENT_A
LvdK 7:6576a287e563 89 CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
LvdK 7:6576a287e563 90 CDU_I2C.read(CDU_KB_ADRS, cmd, 2); //read key value (=2 words)
LvdK 7:6576a287e563 91
LvdK 7:6576a287e563 92 key_hit_ID = int(cmd[0]);
LvdK 8:422544d24df3 93
LvdK 8:422544d24df3 94 //SERIAL_DEBUG.printf("keynumber : %d,%d\r\n",key_hit_ID,cmd[1] ); // : TEST only !
LvdK 7:6576a287e563 95
LvdK 7:6576a287e563 96 //Reset interrupt flag
LvdK 7:6576a287e563 97 cmd[0] = REG_INT_STAT; //pointer byte to Interrupt Status Register
LvdK 7:6576a287e563 98 cmd[1] = 0x01; //Reset KE-INT flag
LvdK 7:6576a287e563 99 CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
LvdK 7:6576a287e563 100
LvdK 7:6576a287e563 101 //Read interrupt status flag
LvdK 7:6576a287e563 102 cmd[0] = 0x02; //pointer byte to Interrupt Status Register
LvdK 7:6576a287e563 103 CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
LvdK 7:6576a287e563 104 CDU_I2C.read(CDU_KB_ADRS, cmd, 1); //read interrupt value
LvdK 7:6576a287e563 105
LvdK 7:6576a287e563 106 if ( cmd[0] == 1 )
LvdK 7:6576a287e563 107 { //reset INT flag failed!
LvdK 7:6576a287e563 108 while (1)
LvdK 7:6576a287e563 109 {
LvdK 7:6576a287e563 110 cmd[0] = 0x04; //pointer to Key Event Register KEY_EVENT_A
LvdK 7:6576a287e563 111 CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
LvdK 7:6576a287e563 112 CDU_I2C.read(CDU_KB_ADRS, cmd, 2); //read key value (=2 words)
LvdK 7:6576a287e563 113
LvdK 7:6576a287e563 114 //Reset interrupt flag
LvdK 7:6576a287e563 115 cmd[0] = 0x02; //pointer byte to Interrupt Status Register
LvdK 7:6576a287e563 116 cmd[1] = 0x01; //Reset KE-INT flag
LvdK 7:6576a287e563 117 CDU_I2C.write(CDU_KB_ADRS,cmd, 2 );
LvdK 7:6576a287e563 118
LvdK 7:6576a287e563 119 //Read interrupt status flag
LvdK 7:6576a287e563 120 cmd[0] = 0x02; //pointer byte to Interrupt Status Register
LvdK 7:6576a287e563 121 CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
LvdK 7:6576a287e563 122 CDU_I2C.read(CDU_KB_ADRS, cmd, 1); //read interrupt value
LvdK 7:6576a287e563 123
LvdK 7:6576a287e563 124 if ( cmd[0] == 0 ) break;
LvdK 7:6576a287e563 125 }
LvdK 7:6576a287e563 126 }
LvdK 7:6576a287e563 127 }
LvdK 7:6576a287e563 128
LvdK 7:6576a287e563 129 void SET_KEY_EVENT()
LvdK 7:6576a287e563 130 {
LvdK 7:6576a287e563 131 // set KEY_EVENT to wakeup SEND_KEYMESSAGE_TASK :
LvdK 7:6576a287e563 132 CDU_OS.SetEvent(KEY_EVENT,SEND_KEYMESSAGE_TASK_ID );
LvdK 7:6576a287e563 133 }
LvdK 7:6576a287e563 134
LvdK 7:6576a287e563 135 void CDU_KB_INT_START()
LvdK 7:6576a287e563 136 {
LvdK 7:6576a287e563 137 CDU_KB_INT.mode( PullUp ); //Keyboard chip pulls this line to 0 on a keypress
LvdK 7:6576a287e563 138 CDU_KB_INT.fall(&SET_KEY_EVENT); //Bind function to handle interrupt
LvdK 7:6576a287e563 139 }
LvdK 7:6576a287e563 140
LvdK 7:6576a287e563 141 void CDU_SET_BGL_INTENSITY( int nVal=255 )
LvdK 7:6576a287e563 142 {
LvdK 7:6576a287e563 143 //This routine must be called 5-10x per second. Manual test to see what is pleasant to see
LvdK 7:6576a287e563 144 //AnalogIn BGL_POT( p15 ); //background light control potmeter. Returns a value between 0.0 and 1.0
LvdK 7:6576a287e563 145 //PwmOut BGL_LED( p21 ); //PWM output
LvdK 7:6576a287e563 146 //calculate required brightness in percentage from 0%-100%
LvdK 7:6576a287e563 147 //nVal 255 --> calculate brightness from potmeter value (default value if no parameter is passed)
LvdK 7:6576a287e563 148 //nVal = 0 --> switch off backlight
LvdK 7:6576a287e563 149 //nVal = 100 --> switch on backlight max
WillemBraat 9:9a5f1b389973 150
WillemBraat 10:7e350a27f936 151 static int LightOn = 1;
WillemBraat 10:7e350a27f936 152 static int FullBright = 0;
WillemBraat 9:9a5f1b389973 153
LvdK 7:6576a287e563 154 switch (nVal)
LvdK 7:6576a287e563 155 {
LvdK 7:6576a287e563 156 case 0:
LvdK 7:6576a287e563 157 {
LvdK 7:6576a287e563 158 //switch off backlighting
WillemBraat 9:9a5f1b389973 159 BGL_LED = 0.0 ;
WillemBraat 9:9a5f1b389973 160 LightOn = 0;
WillemBraat 11:d60c746c097c 161 FullBright = 0;
LvdK 7:6576a287e563 162 break;
LvdK 7:6576a287e563 163 }
LvdK 7:6576a287e563 164 case 100:
LvdK 7:6576a287e563 165 {
WillemBraat 11:d60c746c097c 166 //switch on backlighting to normal
WillemBraat 9:9a5f1b389973 167 BGL_LED = BGL_POT;
WillemBraat 9:9a5f1b389973 168 LightOn = 1;
WillemBraat 11:d60c746c097c 169 FullBright = 0;
LvdK 7:6576a287e563 170 break;
LvdK 7:6576a287e563 171 }
WillemBraat 11:d60c746c097c 172
WillemBraat 11:d60c746c097c 173 case 128:
WillemBraat 11:d60c746c097c 174 {
WillemBraat 11:d60c746c097c 175 //switch backlighting to MAX
WillemBraat 11:d60c746c097c 176 BGL_LED= 1;
WillemBraat 11:d60c746c097c 177 FullBright = 1;
WillemBraat 11:d60c746c097c 178 }
WillemBraat 11:d60c746c097c 179
LvdK 7:6576a287e563 180 case 255:
LvdK 7:6576a287e563 181 {
LvdK 7:6576a287e563 182 //calculate percentage from potmeter value
WillemBraat 11:d60c746c097c 183 if ( ( LightOn == 1) && (FullBright== 0) )
LvdK 7:6576a287e563 184 {
WillemBraat 9:9a5f1b389973 185 if ( BGL_POT < 0.01 )
WillemBraat 9:9a5f1b389973 186 {
WillemBraat 9:9a5f1b389973 187 BGL_LED = 0.0; //prevents flickering when low intensity
WillemBraat 9:9a5f1b389973 188 }
WillemBraat 9:9a5f1b389973 189 else
WillemBraat 9:9a5f1b389973 190 {
WillemBraat 9:9a5f1b389973 191 BGL_LED = BGL_POT;
WillemBraat 9:9a5f1b389973 192 }
LvdK 7:6576a287e563 193 }
WillemBraat 11:d60c746c097c 194 else if ( ( LightOn == 1) && (FullBright== 1) )
WillemBraat 11:d60c746c097c 195 {
WillemBraat 11:d60c746c097c 196 BGL_LED = 1;
WillemBraat 11:d60c746c097c 197
WillemBraat 11:d60c746c097c 198 }
LvdK 7:6576a287e563 199 }
LvdK 7:6576a287e563 200 }
WillemBraat 9:9a5f1b389973 201 }