C++ file for display control

Dependencies:   4DGL mbed ConfigFile

Fork of 4DGLtest by Stephane ROCHON

Committer:
WillemBraat
Date:
Wed Jul 16 19:15:40 2014 +0000
Revision:
11:a5b0d98794c0
Parent:
10:5706b75d40fa
Rewritten function CDU_DSP_CSS()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WillemBraat 3:f7bce78b04c1 1 /* Keyboard chip TCA8418 control */
WillemBraat 3:f7bce78b04c1 2 #include "mbed.h"
WillemBraat 3:f7bce78b04c1 3 #include "keyboard.h"
WillemBraat 3:f7bce78b04c1 4
WillemBraat 9:311b6676272d 5 const int CDU_KB_ADRS = 0x68; //Base addrees TCA8418 keypad scanner
WillemBraat 10:5706b75d40fa 6 const int I2C_ACK = 0x00;
WillemBraat 10:5706b75d40fa 7 //const int I2C_ NACK = 0x01;
WillemBraat 9:311b6676272d 8
WillemBraat 3:f7bce78b04c1 9 extern int key_hit_ID;
WillemBraat 3:f7bce78b04c1 10 //extern mbos CDU_OS;
WillemBraat 3:f7bce78b04c1 11
WillemBraat 3:f7bce78b04c1 12 //CDU Keyboard communications KEYBOARD_INT
WillemBraat 3:f7bce78b04c1 13 InterruptIn CDU_KB_INT( p5 ); //Set CDU keyboard interrupt line.
WillemBraat 6:904d00252480 14 I2C CDU_I2C(p28, p27); //I2C bus for keyboard/temp chip.
WillemBraat 3:f7bce78b04c1 15
WillemBraat 8:dd258f9e24b0 16 //CDU Keyboard LEDS
WillemBraat 3:f7bce78b04c1 17 DigitalOut EXEC( p12 );
WillemBraat 3:f7bce78b04c1 18 DigitalOut FAIL( p17 );
WillemBraat 3:f7bce78b04c1 19 DigitalOut DSPY( p18 );
WillemBraat 3:f7bce78b04c1 20 DigitalOut MSG( p19 );
WillemBraat 3:f7bce78b04c1 21 DigitalOut OFST( p20 );
WillemBraat 3:f7bce78b04c1 22
WillemBraat 3:f7bce78b04c1 23 //CDU background lighting
WillemBraat 6:904d00252480 24 AnalogIn BGL_POT( p16 ); //background light control potmeter
WillemBraat 7:779c5b8d3b14 25 PwmOut BGL_LED( p21 ); //PWM output background lighting
WillemBraat 3:f7bce78b04c1 26
WillemBraat 3:f7bce78b04c1 27 void CDU_KB_COMM_INIT()
WillemBraat 3:f7bce78b04c1 28 { //initialize communication with TCA84818
WillemBraat 9:311b6676272d 29 char cmd[2];
WillemBraat 9:311b6676272d 30 cmd[0] = REG_CFG; //pointer byte to CFG register
WillemBraat 9:311b6676272d 31 cmd[1] = 0x01; //data for CFG register KE_IEN set to 1
WillemBraat 10:5706b75d40fa 32 if ( CDU_I2C.write(CDU_KB_ADRS,cmd, 2) == I2C_ACK ) //initiate write cycle and check for ACK
WillemBraat 9:311b6676272d 33 {
WillemBraat 9:311b6676272d 34 //intialize all registers from TCA8418 here
WillemBraat 9:311b6676272d 35 cmd[0] = REG_INT_STAT; //pointer byte to Interrupt Status Register
WillemBraat 9:311b6676272d 36 cmd[1] = 0x01; //Reset KE-INT flag
WillemBraat 9:311b6676272d 37 CDU_I2C.write(CDU_KB_ADRS,cmd, 2 ); //Write to Interrupt Status Register from TCA4818
WillemBraat 9:311b6676272d 38
WillemBraat 9:311b6676272d 39 //Set TCA8418 to Keypad mode
WillemBraat 9:311b6676272d 40 cmd[0]=REG_KP_GPIO1; //KP_GIO1
WillemBraat 9:311b6676272d 41 cmd[1]=0xFF; //Set to Keypad mode
WillemBraat 9:311b6676272d 42 CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
WillemBraat 9:311b6676272d 43
WillemBraat 9:311b6676272d 44 cmd[0]=REG_KP_GPIO2; //KP_GIO2
WillemBraat 9:311b6676272d 45 cmd[1]=0xFF; //Set to Keypad mode
WillemBraat 9:311b6676272d 46 CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
WillemBraat 9:311b6676272d 47
WillemBraat 9:311b6676272d 48 cmd[0]=REG_KP_GPIO3; //KP_GIO3
WillemBraat 9:311b6676272d 49 cmd[1]=0xFF; //Set to Keypad mode
WillemBraat 9:311b6676272d 50 CDU_I2C.write(CDU_KB_ADRS,cmd, 2);
WillemBraat 9:311b6676272d 51
WillemBraat 9:311b6676272d 52 }
WillemBraat 9:311b6676272d 53 else
WillemBraat 9:311b6676272d 54 {
WillemBraat 9:311b6676272d 55 //No response from TCA8418 keyboard chip
WillemBraat 9:311b6676272d 56 FAIL = 1; //Switch on FAIL indicator
WillemBraat 9:311b6676272d 57 }
WillemBraat 3:f7bce78b04c1 58 }
WillemBraat 3:f7bce78b04c1 59
WillemBraat 6:904d00252480 60 void CDU_KB_GET_KEY()
WillemBraat 3:f7bce78b04c1 61 {
WillemBraat 9:311b6676272d 62 EXEC = 1;//flash EXEC leds when key pressed....
WillemBraat 9:311b6676272d 63 wait( 0.1 );
WillemBraat 9:311b6676272d 64 EXEC = 0;
WillemBraat 9:311b6676272d 65
WillemBraat 9:311b6676272d 66 char cmd[2];
WillemBraat 9:311b6676272d 67
WillemBraat 9:311b6676272d 68 //Read interrupt status flag
WillemBraat 9:311b6676272d 69 cmd[0] = REG_INT_STAT; //pointer byte to Interrupt Status Register
WillemBraat 9:311b6676272d 70 CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
WillemBraat 9:311b6676272d 71 CDU_I2C.read(CDU_KB_ADRS, cmd, 1); //read key value
WillemBraat 9:311b6676272d 72
WillemBraat 9:311b6676272d 73 //Read Key Lock and Event Counter
WillemBraat 9:311b6676272d 74 cmd[0] = REG_KEY_LCK_EC; //pointer byte KEY_LCK_EC
WillemBraat 9:311b6676272d 75 CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
WillemBraat 9:311b6676272d 76 CDU_I2C.read(CDU_KB_ADRS, cmd, 1); //read key value
WillemBraat 9:311b6676272d 77
WillemBraat 9:311b6676272d 78 //Keypress --> read data from keybuffer
WillemBraat 9:311b6676272d 79 cmd[0] = REG_KEY_EVENT_A; //pointer to Key Event Register KEY_EVENT_A
WillemBraat 9:311b6676272d 80 CDU_I2C.write(CDU_KB_ADRS, cmd, 1); //initiate read cycle
WillemBraat 9:311b6676272d 81 CDU_I2C.read(CDU_KB_ADRS, cmd, 2); //read key value (=2 words)
WillemBraat 9:311b6676272d 82
WillemBraat 9:311b6676272d 83 //Reset interrupt flag
WillemBraat 9:311b6676272d 84 cmd[0] = REG_INT_STAT; //pointer byte to Interrupt Status Register
WillemBraat 9:311b6676272d 85 cmd[1] = 0x01; //Reset KE-INT flag
WillemBraat 9:311b6676272d 86 CDU_I2C.write(CDU_KB_ADRS,cmd, 2 );
WillemBraat 9:311b6676272d 87
WillemBraat 3:f7bce78b04c1 88 //CDU_OS.SetEvent(KEY_EVENT,SEND_KEYMESSAGE_TASK_ID ); //Set event key to wakeup task
WillemBraat 9:311b6676272d 89
WillemBraat 9:311b6676272d 90 }
WillemBraat 3:f7bce78b04c1 91
WillemBraat 3:f7bce78b04c1 92 void CDU_KB_INT_START()
WillemBraat 3:f7bce78b04c1 93 {
WillemBraat 7:779c5b8d3b14 94 CDU_KB_INT.mode( PullUp ); //Keyboard chip pulls this line to 0 on a keypress
WillemBraat 7:779c5b8d3b14 95 CDU_KB_INT.fall(&CDU_KB_GET_KEY); //Bind function to interrupt handler
WillemBraat 3:f7bce78b04c1 96 }
WillemBraat 3:f7bce78b04c1 97
WillemBraat 7:779c5b8d3b14 98 void CDU_SET_BGL_INTENSITY( int nVal=255 )
WillemBraat 3:f7bce78b04c1 99 {
WillemBraat 8:dd258f9e24b0 100 //This routine must be called 5-10x per second. Manual test to see what is pleasant to see
WillemBraat 3:f7bce78b04c1 101 //AnalogIn BGL_POT( p15 ); //background light control potmeter. Returns a value between 0.0 and 1.0
WillemBraat 3:f7bce78b04c1 102 //PwmOut BGL_LED( p21 ); //PWM output
WillemBraat 3:f7bce78b04c1 103 //calculate required brightness in percentage from 0%-100%
WillemBraat 7:779c5b8d3b14 104 //nVal 255 --> calculate brightness from potmeter value (default value if no parameter is passed)
WillemBraat 3:f7bce78b04c1 105 //nVal = 0 --> switch off backlight
WillemBraat 3:f7bce78b04c1 106 //nVal = 100 --> switch on backlight max
WillemBraat 3:f7bce78b04c1 107
WillemBraat 3:f7bce78b04c1 108 switch (nVal)
WillemBraat 3:f7bce78b04c1 109 {
WillemBraat 3:f7bce78b04c1 110 case 0:
WillemBraat 3:f7bce78b04c1 111 {
WillemBraat 3:f7bce78b04c1 112 //switch off backlighting
WillemBraat 3:f7bce78b04c1 113 BGL_LED.pulsewidth( 0.0 );
WillemBraat 3:f7bce78b04c1 114 break;
WillemBraat 3:f7bce78b04c1 115 }
WillemBraat 3:f7bce78b04c1 116 case 100:
WillemBraat 3:f7bce78b04c1 117 {
WillemBraat 3:f7bce78b04c1 118 //switch on backlighting
WillemBraat 3:f7bce78b04c1 119 BGL_LED.pulsewidth( 100.0 );
WillemBraat 3:f7bce78b04c1 120 break;
WillemBraat 3:f7bce78b04c1 121 }
WillemBraat 3:f7bce78b04c1 122 case 255:
WillemBraat 3:f7bce78b04c1 123 {
WillemBraat 3:f7bce78b04c1 124 //calculate percentage from potmeter value
WillemBraat 9:311b6676272d 125 if ( BGL_POT < 0.01 )
WillemBraat 9:311b6676272d 126 {
WillemBraat 9:311b6676272d 127 BGL_LED = 0.0; //prevents flickering when low intensity
WillemBraat 9:311b6676272d 128 }
WillemBraat 9:311b6676272d 129 else
WillemBraat 9:311b6676272d 130 {
WillemBraat 9:311b6676272d 131 BGL_LED = BGL_POT;
WillemBraat 9:311b6676272d 132 }
WillemBraat 3:f7bce78b04c1 133 }
WillemBraat 3:f7bce78b04c1 134 }
WillemBraat 3:f7bce78b04c1 135 }
WillemBraat 9:311b6676272d 136