os/kernelhwsrv/bsptemplate/asspandvariant/template_variant/specific/keyboard.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// template\template_variant\specific\keyboard.cpp
sl@0
    15
// Access to Template polled keyboard
sl@0
    16
// The code here implements a simple polled keyboard driver.
sl@0
    17
// This is an alternative to the interrupt-driven driver in keyboard_interrupt.cpp.
sl@0
    18
// This example assumes that we have a non-intelligent keyboard
sl@0
    19
// consisting of a number of i/o lines arranged in a grid.
sl@0
    20
// You can use this code as a starting point and modify it to suit
sl@0
    21
// your hardware.
sl@0
    22
// 
sl@0
    23
//
sl@0
    24
sl@0
    25
#include <template_assp.h>
sl@0
    26
#include "platform.h"
sl@0
    27
#include <kernel/kpower.h>
sl@0
    28
#include <e32keys.h>
sl@0
    29
sl@0
    30
sl@0
    31
sl@0
    32
// The TKeyboardState class is used to encapsulate the state of 
sl@0
    33
// the keyboard. i.e which keys are currently being pressed.
sl@0
    34
// To determine which keys are being pressed, typically a voltage
sl@0
    35
// is applied to each row in turn (or column, depending on the hardware) 
sl@0
    36
// and the output is read resulting in a bitmask for each row.
sl@0
    37
//
sl@0
    38
// For example, the keys could be arranged as follows (where a '1' indicates
sl@0
    39
// that a key is currently being pressed :
sl@0
    40
// EXAMPLE ONLY
sl@0
    41
//
sl@0
    42
//																						Translated
sl@0
    43
//				Column#	0	1	2	3	4	5	6	7	8	9	A	B	C	D	E	F	KeyCode
sl@0
    44
//			Row#	
sl@0
    45
//			6			0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	60	to	6F
sl@0
    46
//			5			0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	50	to	5F
sl@0
    47
//			4			0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	40	to	4F
sl@0
    48
//			3			0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	30	to	3F
sl@0
    49
//	Input->	2			0	0	0	1	0	0	0	0	1	0	0	0	0	0	0	0	20	to	2F
sl@0
    50
//			1			0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	10	to	1F
sl@0
    51
//			0			0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	00	to	0F	
sl@0
    52
//	
sl@0
    53
//	output->			0	0	0	1	0	0	0	0	1	0	0	0	0	0	0	0	
sl@0
    54
//
sl@0
    55
// TO DO: (mandadory)
sl@0
    56
// Modify TKeyboardState (or provide an alternative) to model the 
sl@0
    57
// real keyboard state
sl@0
    58
//
sl@0
    59
// EXAMPLE ONLY
sl@0
    60
class TKeyboardState
sl@0
    61
	{
sl@0
    62
public:
sl@0
    63
sl@0
    64
	enum TDimensions
sl@0
    65
	{
sl@0
    66
	KRows = 7,
sl@0
    67
	KColumns = 16
sl@0
    68
	};
sl@0
    69
sl@0
    70
public:
sl@0
    71
	TKeyboardState();
sl@0
    72
	void Clear();
sl@0
    73
	TBool IsKeyReady();
sl@0
    74
	TUint32 GetKeyCode();
sl@0
    75
	TKeyboardState operator&(const TKeyboardState& aState);
sl@0
    76
	TKeyboardState operator|(const TKeyboardState& aState);
sl@0
    77
	TKeyboardState operator~();
sl@0
    78
sl@0
    79
public:
sl@0
    80
	TUint32 iKeyBitMask[KRows];
sl@0
    81
	};
sl@0
    82
sl@0
    83
/**
sl@0
    84
Constructor
sl@0
    85
*/
sl@0
    86
TKeyboardState::TKeyboardState()
sl@0
    87
	{
sl@0
    88
	Clear();
sl@0
    89
	}
sl@0
    90
sl@0
    91
/**
sl@0
    92
Clears the array of bitmasks 
sl@0
    93
*/
sl@0
    94
void TKeyboardState::Clear()
sl@0
    95
	{
sl@0
    96
	for (TInt row=0; row<KRows; row++)
sl@0
    97
		iKeyBitMask[row] = 0;
sl@0
    98
	}
sl@0
    99
sl@0
   100
/**
sl@0
   101
Determines whether any keys are being pressed by examining the 
sl@0
   102
array of bitmasks to determine whether any bits are set
sl@0
   103
sl@0
   104
@return	ETrue if one or more keys are being pressed
sl@0
   105
*/
sl@0
   106
TBool TKeyboardState::IsKeyReady()
sl@0
   107
	{
sl@0
   108
	for (TInt row=0; row<KRows; row++)
sl@0
   109
		{
sl@0
   110
		if (iKeyBitMask[row] != 0)
sl@0
   111
			return ETrue;
sl@0
   112
		}
sl@0
   113
sl@0
   114
	return EFalse;
sl@0
   115
	}
sl@0
   116
sl@0
   117
/**
sl@0
   118
Scans the array of bitmasks and returns a keycode representing
sl@0
   119
the first bit that it finds that is on.
sl@0
   120
E.g. :
sl@0
   121
if the first bit on the first row is set, then 1 is returned,
sl@0
   122
if the third bit on the first row is set, then 3 is returned. etc.
sl@0
   123
sl@0
   124
Once a bit is found it is cleared to avoid reading it again.
sl@0
   125
sl@0
   126
NB Before calling this function, IsKeyReady() should be called 
sl@0
   127
to determine whether a key code is available.
sl@0
   128
sl@0
   129
@return	a 32-bit keycode representing a key that is currently pressed
sl@0
   130
*/
sl@0
   131
sl@0
   132
TUint32 TKeyboardState::GetKeyCode()
sl@0
   133
	{
sl@0
   134
	TInt keyNum = 0;
sl@0
   135
	for (TInt row=0; row<KRows; row++)
sl@0
   136
		{
sl@0
   137
		TUint32 bitMask = 1;
sl@0
   138
		for (TInt col=0; col<KColumns; col++)
sl@0
   139
			{
sl@0
   140
			if (iKeyBitMask[row] & bitMask)
sl@0
   141
				{
sl@0
   142
				iKeyBitMask[row] &= ~bitMask;
sl@0
   143
				return keyNum;
sl@0
   144
				}
sl@0
   145
			bitMask<<= 1;
sl@0
   146
			keyNum++;
sl@0
   147
			}
sl@0
   148
		}
sl@0
   149
	return 0;
sl@0
   150
	}
sl@0
   151
sl@0
   152
/**
sl@0
   153
Perform a bitwise AND between two TKeyboardState objects
sl@0
   154
by AND-ing together all the 32-bit integers
sl@0
   155
sl@0
   156
@return	a new instance of a TKeyboardState object containing the result
sl@0
   157
*/
sl@0
   158
TKeyboardState TKeyboardState::operator&(const TKeyboardState& aState)
sl@0
   159
	{
sl@0
   160
	TKeyboardState state = *this;
sl@0
   161
sl@0
   162
	for (TInt row=0; row<KRows; row++)
sl@0
   163
		state.iKeyBitMask[row]&= aState.iKeyBitMask[row];;
sl@0
   164
sl@0
   165
	return state;
sl@0
   166
	}
sl@0
   167
sl@0
   168
/**
sl@0
   169
Perform a bitwise OR between two TKeyboardState objects
sl@0
   170
by OR-ing together all the 32-bit integers
sl@0
   171
sl@0
   172
@return	a new instance of a TKeyboardState object containing the result
sl@0
   173
*/
sl@0
   174
TKeyboardState TKeyboardState::operator|(const TKeyboardState& aState)
sl@0
   175
	{
sl@0
   176
	TKeyboardState state = *this;
sl@0
   177
sl@0
   178
	for (TInt row=0; row<KRows; row++)
sl@0
   179
		state.iKeyBitMask[row]|= aState.iKeyBitMask[row];;
sl@0
   180
sl@0
   181
	return state;
sl@0
   182
	}
sl@0
   183
sl@0
   184
/**
sl@0
   185
Perform a bitwise NOT (one's complement) of a KeyboardState object
sl@0
   186
by NOT-ing all the 32-bit integers
sl@0
   187
sl@0
   188
@return	a new instance of a TKeyboardState object containing the result
sl@0
   189
*/
sl@0
   190
TKeyboardState TKeyboardState::operator~()
sl@0
   191
	{
sl@0
   192
	TKeyboardState state = *this;
sl@0
   193
sl@0
   194
	for (TInt row=0; row<KRows; row++)
sl@0
   195
		state.iKeyBitMask[row] = ~state.iKeyBitMask[row];
sl@0
   196
sl@0
   197
	return state;
sl@0
   198
	}
sl@0
   199
sl@0
   200
//
sl@0
   201
//
sl@0
   202
// TO DO: (optional)
sl@0
   203
//
sl@0
   204
// Modify this conversion table to suit your keyboard layout
sl@0
   205
// EXAMPLE ONLY
sl@0
   206
//
sl@0
   207
sl@0
   208
const TUint8 convertCode[] =
sl@0
   209
	{
sl@0
   210
//Row 0 (bottom row)
sl@0
   211
	EStdKeyLeftAlt		,	EStdKeyHash			,	EStdKeyNull			,	EStdKeyLeftCtrl				,
sl@0
   212
	EStdKeyLeftFunc		,	EStdKeyEscape		,	'1'					,	'2'							,
sl@0
   213
	'9'					,	'0'					,	EStdKeyMinus		,	EStdKeyEquals				,
sl@0
   214
	EStdKeyNull			,	EStdKeyBackspace	,	EStdKeyNull			,	EStdKeyNull					,
sl@0
   215
//Row 1
sl@0
   216
	EStdKeyNull			,	EStdKeyBackSlash	,	EStdKeyLeftShift	,	EStdKeyNull					,
sl@0
   217
	EStdKeyNull			,	EStdKeyDelete		,	EStdKeyNull			,	'T'							,
sl@0
   218
	'Y'					,	'U'					,	'I'					,	 EStdKeyEnter				,
sl@0
   219
	EStdKeyRightShift	,	EStdKeyDownArrow	,	EStdKeyNull			,	EStdKeyNull					,
sl@0
   220
//Row 2
sl@0
   221
	EStdKeyNull			,	EStdKeyTab			,	EStdKeyNull			,	 EStdKeyNull				,
sl@0
   222
	EStdKeyNull			,	'Q'					,	'W'					,	'E'							,
sl@0
   223
	'R'					,	'O'					,	'P'					,	EStdKeySquareBracketLeft	,
sl@0
   224
	EStdKeyNull			,	EStdKeySquareBracketRight,EStdKeyNull		,	EStdKeyNull					,
sl@0
   225
//Row 3
sl@0
   226
	EStdKeyNull			,	'Z'					,	EStdKeyNull			,	EStdKeyNull					,
sl@0
   227
	EStdKeyNull			,	EStdKeyCapsLock		,	EStdKeyNull			,	EStdKeyNull					,
sl@0
   228
	'K'					,	'L'					,	EStdKeySemiColon	,	EStdKeySingleQuote			,
sl@0
   229
	EStdKeyNull			,	EStdKeyUpArrow		,	EStdKeyNull			,	EStdKeyNull					,
sl@0
   230
//Row 4
sl@0
   231
	EStdKeyNull			,	EStdKeyTab			,	EStdKeyNull			,	EStdKeyNull,
sl@0
   232
	EStdKeyNull			,	'Q'					,	'W'					,	'E'							,
sl@0
   233
	'R'					,	'O'					,	'P'					,	EStdKeySquareBracketLeft	,
sl@0
   234
	EStdKeyNull			,	EStdKeySquareBracketRight,	EStdKeyNull		,	EStdKeyNull					,
sl@0
   235
//Row 5
sl@0
   236
	EStdKeyNull			,	'X'					,	EStdKeyNull			,	EStdKeyNull					,
sl@0
   237
	EStdKeyNull			,	'C'					,	'V'					,	'B'							,
sl@0
   238
	'N'					,	'M'					,	EStdKeyComma		,	EStdKeyFullStop				,
sl@0
   239
	EStdKeyNull			,	EStdKeySpace		,	EStdKeyNull			,	EStdKeyNull					,
sl@0
   240
//Row 6
sl@0
   241
	EStdKeyNull			,	EStdKeyNull			,	EStdKeyNull			,	EStdKeyNull					,
sl@0
   242
	EStdKeyNull			,	'3'					,	'4'					,	'5'							,
sl@0
   243
	'6'					,	'7'					,	'8'					,	EStdKeyMenu					,
sl@0
   244
	EStdKeyNull			,	EStdKeyRightArrow	,	EStdKeyNull			,	EStdKeyNull					
sl@0
   245
	};
sl@0
   246
sl@0
   247
sl@0
   248
sl@0
   249
sl@0
   250
// EXAMPLE ONLY
sl@0
   251
const TKeyboard	KConfigKeyboardType = EKeyboard_Full;
sl@0
   252
const TInt KConfigKeyboardDeviceKeys = 0;
sl@0
   253
const TInt KConfigKeyboardAppsKeys = 0;
sl@0
   254
sl@0
   255
sl@0
   256
//
sl@0
   257
// TO DO: (optional)
sl@0
   258
//
sl@0
   259
// Set the keyboard scan rate in milliseconds
sl@0
   260
//
sl@0
   261
sl@0
   262
// EXAMPLE ONLY
sl@0
   263
const TInt KScanRate = 50;	// poll every 1/20 of a second (i.e. every 50 milliseconds)
sl@0
   264
sl@0
   265
sl@0
   266
_LIT(KLitKeyboard,"Keyboard");
sl@0
   267
sl@0
   268
sl@0
   269
//
sl@0
   270
// TO DO: (optional)
sl@0
   271
//
sl@0
   272
// Add any private functions and data you require
sl@0
   273
//
sl@0
   274
NONSHARABLE_CLASS(DKeyboardTemplate) : public DPowerHandler
sl@0
   275
	{
sl@0
   276
public:
sl@0
   277
	DKeyboardTemplate();
sl@0
   278
	TInt Create();
sl@0
   279
	
sl@0
   280
	// from DPowerHandler
sl@0
   281
	void PowerUp();
sl@0
   282
	void PowerDown(TPowerState);
sl@0
   283
sl@0
   284
private:
sl@0
   285
	static void HandleMessage(TAny* aPtr);
sl@0
   286
	void HandleMsg(TMessageBase* aMsg);
sl@0
   287
	
sl@0
   288
	static TInt HalFunction(TAny* aPtr, TInt aFunction, TAny* a1, TAny* a2);
sl@0
   289
	TInt HalFunction(TInt aFunction, TAny* a1, TAny* a2);
sl@0
   290
	
sl@0
   291
	static void PowerUpDfcFn(TAny* aPtr);
sl@0
   292
	void PowerUpDfc();
sl@0
   293
	
sl@0
   294
	static void PowerDownDfcFn(TAny* aPtr);
sl@0
   295
	void PowerDownDfc();
sl@0
   296
sl@0
   297
	static void TimerCallback(TAny* aDriver);
sl@0
   298
	static void TimerDfcFn(TAny* aDriver);
sl@0
   299
	void Poll();
sl@0
   300
sl@0
   301
	void KeyboardInfo(TKeyboardInfoV01& aInfo);
sl@0
   302
sl@0
   303
	void KeyboardOn();
sl@0
   304
	void KeyboardOff();
sl@0
   305
	void KeyboardPowerUp();
sl@0
   306
sl@0
   307
private:
sl@0
   308
	TDfcQue* iDfcQ;
sl@0
   309
	TMessageQue iMsgQ;	
sl@0
   310
	TDfc iPowerUpDfc;
sl@0
   311
	TDfc iPowerDownDfc;	
sl@0
   312
	TBool iKeyboardOn;
sl@0
   313
	NTimer iTimer;
sl@0
   314
	TInt iTimerTicks;
sl@0
   315
	TDfc iTimerDfc;
sl@0
   316
sl@0
   317
	// a bitmask indicating which keys were pressed down on the last timer tick
sl@0
   318
	TKeyboardState iKeyStateLast;
sl@0
   319
sl@0
   320
	// a bitmask indicating the set of keys for which we have sent an EKeyDown event
sl@0
   321
	TKeyboardState iKeysDown;			
sl@0
   322
	};
sl@0
   323
sl@0
   324
/**
sl@0
   325
constructor
sl@0
   326
*/
sl@0
   327
DKeyboardTemplate::DKeyboardTemplate()
sl@0
   328
	:	DPowerHandler(KLitKeyboard), 
sl@0
   329
		iMsgQ(HandleMessage, this, NULL, 1),
sl@0
   330
		iPowerUpDfc(PowerUpDfcFn, this, 6),
sl@0
   331
		iPowerDownDfc(PowerDownDfcFn, this, 7),
sl@0
   332
		iTimer(&DKeyboardTemplate::TimerCallback, (TAny*) this),
sl@0
   333
		iTimerDfc(TimerDfcFn, this, 1)
sl@0
   334
	{
sl@0
   335
	// Convert the scan rate from milliseconds to nanokernel ticks (normally 1/64 of a second)
sl@0
   336
	iTimerTicks = NKern::TimerTicks(KScanRate);
sl@0
   337
	}
sl@0
   338
sl@0
   339
/**
sl@0
   340
Second-phase constructor 
sl@0
   341
Assigns queues for all the DFCs and starts the keyboard-polling timer
sl@0
   342
sl@0
   343
Called by factory function at ordinal 0
sl@0
   344
*/
sl@0
   345
TInt DKeyboardTemplate::Create()
sl@0
   346
	{
sl@0
   347
	iDfcQ=Kern::DfcQue0();
sl@0
   348
sl@0
   349
	iKeyboardOn = EFalse;	
sl@0
   350
sl@0
   351
	// install the HAL function
sl@0
   352
	TInt r = Kern::AddHalEntry(EHalGroupKeyboard, DKeyboardTemplate::HalFunction, this);
sl@0
   353
	if (r != KErrNone)
sl@0
   354
		return r;
sl@0
   355
sl@0
   356
	iTimerDfc.SetDfcQ(iDfcQ);
sl@0
   357
sl@0
   358
	iPowerUpDfc.SetDfcQ(iDfcQ);
sl@0
   359
	iPowerDownDfc.SetDfcQ(iDfcQ);
sl@0
   360
	iMsgQ.SetDfcQ(iDfcQ);
sl@0
   361
	iMsgQ.Receive();
sl@0
   362
sl@0
   363
	// install the power handler
sl@0
   364
	Add();
sl@0
   365
sl@0
   366
	// Power up the device and start the timer
sl@0
   367
	KeyboardPowerUp();
sl@0
   368
sl@0
   369
	return r;
sl@0
   370
	}
sl@0
   371
sl@0
   372
/**
sl@0
   373
Calback for the keyboard-polling timer
sl@0
   374
Called in the context of an ISR
sl@0
   375
sl@0
   376
@param	aPtr A pointer to an instance of DKeyboardTemplate
sl@0
   377
*/
sl@0
   378
void DKeyboardTemplate::TimerCallback(TAny *aPtr)
sl@0
   379
	{
sl@0
   380
	// schedule a DFC
sl@0
   381
	DKeyboardTemplate& k=*(DKeyboardTemplate*)aPtr;
sl@0
   382
	k.iTimerDfc.Add();
sl@0
   383
	}
sl@0
   384
sl@0
   385
sl@0
   386
/**
sl@0
   387
DFC scheduled by the keyboard-polling timer when it expires
sl@0
   388
sl@0
   389
@param	aPtr A pointer to an instance of DKeyboardTemplate
sl@0
   390
*/
sl@0
   391
void DKeyboardTemplate::TimerDfcFn(TAny* aPtr)
sl@0
   392
	{
sl@0
   393
	((DKeyboardTemplate*)aPtr)->Poll();
sl@0
   394
	}
sl@0
   395
sl@0
   396
sl@0
   397
/**
sl@0
   398
Reads scan codes from the keyboard until there are none left
sl@0
   399
Called from the keyboard-polling timer's DFC
sl@0
   400
*/
sl@0
   401
void DKeyboardTemplate::Poll()
sl@0
   402
	{
sl@0
   403
	__KTRACE_OPT(KHARDWARE,Kern::Printf("DKeyboardTemplate::EventDfc"));
sl@0
   404
sl@0
   405
	
sl@0
   406
	TKeyboardState keyState;
sl@0
   407
sl@0
   408
	//
sl@0
   409
	// TO DO: (mandatory)
sl@0
   410
	// Read new key state into the array of bitmasks in keyState
sl@0
   411
	// This typically involves applying a voltage to each row from 0 to KRows-1, 
sl@0
   412
	// reading the output state of the i/o lines at every step 
sl@0
   413
	// - this represents the keys that are pressed on each row -
sl@0
   414
	// and storing the output of each row as a bitmask into keyState.iKeyBitMask[n], 
sl@0
   415
	// where n = the row being accessed
sl@0
   416
	//
sl@0
   417
sl@0
   418
	// To enable a simple de-bouncing algorithm, 
sl@0
   419
	// work out which keys have been pressed down for at least two timer 
sl@0
   420
	// ticks by AND-ing together the last bitmask with the current bitmask
sl@0
   421
	TKeyboardState keysStillDown =  keyState & iKeyStateLast;
sl@0
   422
	
sl@0
   423
sl@0
   424
	// Similarly, work out which keys have been "un-pressed" for at least two timer 
sl@0
   425
	// ticks by AND-ing together the one's complement of the last bitmask with the 
sl@0
   426
	// one's complement of the current bitmask and 
sl@0
   427
	// then AND-ing this with the set of keys for which we have sent an EKeyDown 
sl@0
   428
	// event to give the set of keys for which we need to send an EKeyUp event
sl@0
   429
	TKeyboardState keysStillUp =  (~keyState & ~iKeyStateLast) & iKeysDown;
sl@0
   430
sl@0
   431
	// save the current state for next time
sl@0
   432
	iKeyStateLast = keyState;
sl@0
   433
sl@0
   434
	// update the set of keys for which we have sent an EKeyDown event
sl@0
   435
	iKeysDown = iKeysDown | keysStillDown;
sl@0
   436
	iKeysDown = iKeysDown & ~keysStillUp;
sl@0
   437
sl@0
   438
	// process all the key-down events
sl@0
   439
	while (keysStillDown.IsKeyReady())						// while there are keys we haven't processed
sl@0
   440
		{
sl@0
   441
		TRawEvent e;
sl@0
   442
		TUint keyCode = keysStillDown.GetKeyCode();			// Read keycodes from bitmask 
sl@0
   443
sl@0
   444
		__KTRACE_OPT(KHARDWARE,Kern::Printf("EKeyDown: #%02x\n",keyCode));
sl@0
   445
sl@0
   446
		//
sl@0
   447
		// TO DO: (mandatory)
sl@0
   448
		//
sl@0
   449
		// Convert from hardware scancode to EPOC scancode and send the scancode as an event (key pressed or released)
sl@0
   450
		// as per below EXAMPLE ONLY:
sl@0
   451
		//
sl@0
   452
		__ASSERT_DEBUG(keyCode < (sizeof(convertCode) / sizeof(TUint8)), Kern::Fault("Keyboard", __LINE__));
sl@0
   453
		TUint8 stdKey = convertCode[keyCode];
sl@0
   454
		
sl@0
   455
		e.Set(TRawEvent::EKeyDown, stdKey, 0);
sl@0
   456
		Kern::AddEvent(e);
sl@0
   457
		}
sl@0
   458
sl@0
   459
	// process all the key-up events
sl@0
   460
	while (keysStillUp.IsKeyReady())						// while there are keys we haven't processed
sl@0
   461
		{
sl@0
   462
		TRawEvent e;
sl@0
   463
		TUint keyCode = keysStillUp.GetKeyCode();			// Read keycodes from bitmask 
sl@0
   464
sl@0
   465
		__KTRACE_OPT(KHARDWARE,Kern::Printf("EKeyUp: #%02x\n",keyCode));
sl@0
   466
sl@0
   467
		//
sl@0
   468
		// TO DO: (mandatory)
sl@0
   469
		//
sl@0
   470
		// Convert from hardware scancode to EPOC scancode and send the scancode as an event (key pressed or released)
sl@0
   471
		// as per below EXAMPLE ONLY:
sl@0
   472
		//
sl@0
   473
		__ASSERT_DEBUG(keyCode < (sizeof(convertCode) / sizeof(TUint8)), Kern::Fault("Keyboard", __LINE__));
sl@0
   474
		TUint8 stdKey = convertCode[keyCode];
sl@0
   475
sl@0
   476
		e.Set(TRawEvent::EKeyUp, stdKey, 0);
sl@0
   477
		Kern::AddEvent(e);
sl@0
   478
		}
sl@0
   479
sl@0
   480
	// start the timer again
sl@0
   481
	iTimer.OneShot(iTimerTicks);
sl@0
   482
	}
sl@0
   483
sl@0
   484
sl@0
   485
sl@0
   486
/**
sl@0
   487
Notifies the peripheral of system power up.
sl@0
   488
Called by the power manager during a transition from standby.
sl@0
   489
Schedules a DFC to handle the power up.
sl@0
   490
*/
sl@0
   491
void DKeyboardTemplate::PowerUp()
sl@0
   492
	{
sl@0
   493
	iPowerUpDfc.Enque();
sl@0
   494
	}
sl@0
   495
sl@0
   496
sl@0
   497
/**
sl@0
   498
static DFC to handle powering up the keyboard
sl@0
   499
sl@0
   500
@param	aPtr A pointer to an instance of DKeyboardTemplate
sl@0
   501
*/
sl@0
   502
void DKeyboardTemplate::PowerUpDfcFn(TAny* aPtr)
sl@0
   503
	{
sl@0
   504
	((DKeyboardTemplate*)aPtr)->PowerUpDfc();
sl@0
   505
	}
sl@0
   506
sl@0
   507
sl@0
   508
/**
sl@0
   509
DFC to handle powering up the keyboard
sl@0
   510
*/
sl@0
   511
void DKeyboardTemplate::PowerUpDfc()
sl@0
   512
	{
sl@0
   513
	__KTRACE_OPT(KPOWER, Kern::Printf("DKeyboardTemplate::PowerUpDfc()"));
sl@0
   514
	KeyboardOn();
sl@0
   515
sl@0
   516
	// Indicate to power handle that powered up is complete
sl@0
   517
	PowerUpDone();
sl@0
   518
	}
sl@0
   519
sl@0
   520
/**
sl@0
   521
Powers up the keyboard
sl@0
   522
May be called as a result of a power transition or from the HAL
sl@0
   523
*/
sl@0
   524
void DKeyboardTemplate::KeyboardOn()
sl@0
   525
	{
sl@0
   526
	__KTRACE_OPT(KPOWER,Kern::Printf("DKeyboardTemplate::KeyboardOn() iKeyboardOn=%d", iKeyboardOn));
sl@0
   527
sl@0
   528
	if (!iKeyboardOn)	// make sure we don't initialize more than once
sl@0
   529
		KeyboardPowerUp();
sl@0
   530
	}
sl@0
   531
sl@0
   532
/**
sl@0
   533
Powers up the keyboard
sl@0
   534
Assumes that the keyboard is currently powered off
sl@0
   535
*/
sl@0
   536
void DKeyboardTemplate::KeyboardPowerUp()
sl@0
   537
	{
sl@0
   538
	__KTRACE_OPT(KPOWER,Kern::Printf("DKeyboardTemplate::KeyboardPowerUp()"));
sl@0
   539
sl@0
   540
	iKeyboardOn = ETrue;
sl@0
   541
sl@0
   542
	iKeyStateLast.Clear();
sl@0
   543
	iKeysDown.Clear();
sl@0
   544
sl@0
   545
	// Send key up events for EStdKeyOff (Fn+Esc) event 
sl@0
   546
	TRawEvent e;
sl@0
   547
	e.Set(TRawEvent::EKeyUp,EStdKeyEscape,0);
sl@0
   548
	Kern::AddEvent(e);
sl@0
   549
	e.Set(TRawEvent::EKeyUp,EStdKeyLeftFunc,0);
sl@0
   550
	Kern::AddEvent(e);
sl@0
   551
sl@0
   552
	// Start the periodic tick for the selected rate.
sl@0
   553
	// This will call TimerCallback() in the context of an ISR
sl@0
   554
	iTimer.OneShot(iTimerTicks);
sl@0
   555
	}
sl@0
   556
sl@0
   557
sl@0
   558
/**
sl@0
   559
Requests keyboard to power down.
sl@0
   560
Called by the power manager during a transition to standby or power off
sl@0
   561
Schedules a DFC to handle the power up.
sl@0
   562
sl@0
   563
@param aPowerState the current power state
sl@0
   564
*/
sl@0
   565
void DKeyboardTemplate::PowerDown(TPowerState)
sl@0
   566
	{
sl@0
   567
	iPowerDownDfc.Enque();
sl@0
   568
	}
sl@0
   569
sl@0
   570
/**
sl@0
   571
static DFC to handle powering down the keyboard
sl@0
   572
sl@0
   573
@param	aPtr A pointer to an instance of DKeyboardTemplate
sl@0
   574
*/
sl@0
   575
void DKeyboardTemplate::PowerDownDfcFn(TAny* aPtr)
sl@0
   576
	{
sl@0
   577
	((DKeyboardTemplate*)aPtr)->PowerDownDfc();
sl@0
   578
	}
sl@0
   579
sl@0
   580
/**
sl@0
   581
DFC to handle powering down the keyboard
sl@0
   582
*/
sl@0
   583
void DKeyboardTemplate::PowerDownDfc()
sl@0
   584
	{
sl@0
   585
	__KTRACE_OPT(KPOWER, Kern::Printf("DKeyboardTemplate::PowerDownDfc()"));
sl@0
   586
	KeyboardOff();
sl@0
   587
	PowerDownDone();
sl@0
   588
	}
sl@0
   589
sl@0
   590
/**
sl@0
   591
Powers down the keyboard
sl@0
   592
May be called as a result of a power transition or from the HAL
sl@0
   593
*/
sl@0
   594
void DKeyboardTemplate::KeyboardOff()
sl@0
   595
	{
sl@0
   596
	__KTRACE_OPT(KPOWER,Kern::Printf("DKeyboardTemplate::KeyboardOff() iKeyboardOn=%d", iKeyboardOn));
sl@0
   597
sl@0
   598
	// cancel the keyboard-polling timer
sl@0
   599
	iTimerDfc.Cancel();
sl@0
   600
	iTimer.Cancel();
sl@0
   601
sl@0
   602
	iKeyboardOn = EFalse;
sl@0
   603
	}
sl@0
   604
sl@0
   605
sl@0
   606
/**
sl@0
   607
static message handler for processing power up/down messages 
sl@0
   608
posted internally from HalFunction()
sl@0
   609
sl@0
   610
@param	aPtr A pointer to an instance of DKeyboardTemplate
sl@0
   611
*/
sl@0
   612
void DKeyboardTemplate::HandleMessage(TAny* aPtr)
sl@0
   613
	{
sl@0
   614
	DKeyboardTemplate& h=*(DKeyboardTemplate*)aPtr;
sl@0
   615
	TMessageBase* pM=h.iMsgQ.iMessage;
sl@0
   616
	if (pM)
sl@0
   617
		h.HandleMsg(pM);
sl@0
   618
	}
sl@0
   619
sl@0
   620
/**
sl@0
   621
Message handler for processing power up/down messages 
sl@0
   622
posted internally from HalFunction()
sl@0
   623
sl@0
   624
param	aMsg A message indicating whether to power the keyboard on or off
sl@0
   625
*/
sl@0
   626
void DKeyboardTemplate::HandleMsg(TMessageBase* aMsg)
sl@0
   627
	{
sl@0
   628
	if (aMsg->iValue)
sl@0
   629
		KeyboardOn();
sl@0
   630
	else
sl@0
   631
		KeyboardOff();
sl@0
   632
	aMsg->Complete(KErrNone,ETrue);
sl@0
   633
	}
sl@0
   634
sl@0
   635
sl@0
   636
/**
sl@0
   637
Retrieves information about the keyboard
sl@0
   638
Called from HalFunction()
sl@0
   639
sl@0
   640
@param	aInfo a caller-supplied class which on return contains information about the keyboard
sl@0
   641
*/
sl@0
   642
void DKeyboardTemplate::KeyboardInfo(TKeyboardInfoV01& aInfo)
sl@0
   643
	{
sl@0
   644
	__KTRACE_OPT(KEXTENSION,Kern::Printf("DKeyboardTemplate::KeyboardInfo"));
sl@0
   645
	aInfo.iKeyboardType=KConfigKeyboardType;
sl@0
   646
	aInfo.iDeviceKeys=KConfigKeyboardDeviceKeys;
sl@0
   647
	aInfo.iAppsKeys=KConfigKeyboardAppsKeys;
sl@0
   648
	}
sl@0
   649
sl@0
   650
sl@0
   651
/**
sl@0
   652
HAL handler function
sl@0
   653
sl@0
   654
@param	aPtr a pointer to an instance of DLcdPowerHandler
sl@0
   655
@param	aFunction the function number
sl@0
   656
@param	a1 an arbitrary parameter
sl@0
   657
@param	a2 an arbitrary parameter
sl@0
   658
*/
sl@0
   659
TInt DKeyboardTemplate::HalFunction(TAny* aPtr, TInt aFunction, TAny* a1, TAny* a2)
sl@0
   660
	{
sl@0
   661
	DKeyboardTemplate* pH=(DKeyboardTemplate*)aPtr;
sl@0
   662
	return pH->HalFunction(aFunction,a1,a2);
sl@0
   663
	}
sl@0
   664
sl@0
   665
sl@0
   666
/**
sl@0
   667
a HAL entry handling function for HAL group attribute EHalGroupKeyboard
sl@0
   668
sl@0
   669
@param	a1 an arbitrary argument
sl@0
   670
@param	a2 an arbitrary argument
sl@0
   671
@return	KErrNone if successful
sl@0
   672
*/
sl@0
   673
TInt DKeyboardTemplate::HalFunction(TInt aFunction, TAny* a1, TAny* a2)
sl@0
   674
	{
sl@0
   675
	TInt r=KErrNone;
sl@0
   676
sl@0
   677
	__KTRACE_OPT(KEXTENSION,Kern::Printf("DKeyboardTemplate::HalFunction %d", aFunction));
sl@0
   678
	
sl@0
   679
	switch(aFunction)
sl@0
   680
		{
sl@0
   681
		case EKeyboardHalKeyboardInfo:
sl@0
   682
			{
sl@0
   683
			TPckgBuf<TKeyboardInfoV01> kPckg;
sl@0
   684
			KeyboardInfo(kPckg());
sl@0
   685
			Kern::InfoCopy(*(TDes8*)a1,kPckg);
sl@0
   686
			break;
sl@0
   687
			}
sl@0
   688
sl@0
   689
		case EKeyboardHalSetKeyboardState:
sl@0
   690
			{
sl@0
   691
			if(!Kern::CurrentThreadHasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked by Hal function EKeyboardHalSetKeyboardState")))
sl@0
   692
				return KErrPermissionDenied;
sl@0
   693
			if ((TBool)a1)
sl@0
   694
				{
sl@0
   695
				TThreadMessage& m=Kern::Message();
sl@0
   696
				m.iValue = ETrue;
sl@0
   697
				m.SendReceive(&iMsgQ);		// send a message and block Client thread until keyboard has been powered up
sl@0
   698
				}
sl@0
   699
			else
sl@0
   700
				{
sl@0
   701
				TThreadMessage& m=Kern::Message();
sl@0
   702
				m.iValue = EFalse;
sl@0
   703
				m.SendReceive(&iMsgQ);		// send a message and block Client thread until keyboard has been powered down
sl@0
   704
				}
sl@0
   705
			}
sl@0
   706
			break;
sl@0
   707
sl@0
   708
		case EKeyboardHalKeyboardState:
sl@0
   709
			kumemput32(a1, &iKeyboardOn, sizeof(TBool));
sl@0
   710
			break;
sl@0
   711
		
sl@0
   712
		default:
sl@0
   713
			r=KErrNotSupported;
sl@0
   714
			break;
sl@0
   715
		}
sl@0
   716
	return r;
sl@0
   717
	}
sl@0
   718
sl@0
   719
sl@0
   720
sl@0
   721
DECLARE_STANDARD_EXTENSION()
sl@0
   722
	{
sl@0
   723
	__KTRACE_OPT(KEXTENSION,Kern::Printf("Starting keyboard driver"));
sl@0
   724
sl@0
   725
	// create keyboard driver
sl@0
   726
	TInt r=KErrNoMemory;
sl@0
   727
	DKeyboardTemplate* pK=new DKeyboardTemplate;
sl@0
   728
	if (pK)
sl@0
   729
		r=pK->Create();
sl@0
   730
sl@0
   731
	__KTRACE_OPT(KEXTENSION,Kern::Printf("Returns %d",r));
sl@0
   732
	return r;
sl@0
   733
	}