Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ExioMcp23s17Keypad4x4.h
00001 /* 00002 * MCP23S17 Keypad 4x4 class 00003 * 00004 * 2016.11.02 created 00005 */ 00006 00007 #ifndef _EXIOMCP23S17Keypad4x4_ 00008 #define _EXIOMCP23S17Keypad4x4_ 00009 00010 #include "mbed.h" 00011 #include "ExioMcp23s17.h" 00012 00013 #define IO_DIRECTION (0b11110000) 00014 #define IO_PULLUP (0b00000000) 00015 #define IO_POLARITY (0b00000000) 00016 00017 class ExioMcp23s17Keypad4x4 { 00018 public: 00019 ExioMcp23s17Keypad4x4(ExioMcp23s17* _pDevice, ExioPort _port) : 00020 pDevice(_pDevice), 00021 port(_port), 00022 keyVal(-1), 00023 keyBuffer(-1) 00024 { 00025 pDevice->ioDirection(port, IO_DIRECTION); 00026 pDevice->ioPullup(port, IO_PULLUP); 00027 pDevice->ioPolarity(port, IO_POLARITY); 00028 pDevice->writePort(port, 0x00); 00029 } 00030 00031 ~ExioMcp23s17Keypad4x4() 00032 { 00033 } 00034 00035 void setSampleFrequency(int i) { ticker.attach_us(this, &ExioMcp23s17Keypad4x4::callback, i); } 00036 00037 int read() { return keyVal; } 00038 00039 private: 00040 //------------------------------------------------------ 00041 // keyScan1(): 押されているキー 00042 // return: キースキャンの結果 0..15 00043 // 押されていない場合は -1 00044 //------------------------------------------------------ 00045 int keyScan1() 00046 { 00047 uint8_t data; 00048 for (int i = 0; i < 4; i++) { 00049 pDevice->bit(GPIOA_ADDR + port, i + 1, 1); 00050 data = pDevice->readPort(port); 00051 pDevice->bit(GPIOA_ADDR + port, i + 1, 0); 00052 uint8_t rv = data >> 4; 00053 if (rv) { 00054 uint8_t lv = 0; 00055 if (rv & 0b0001) lv = 0; 00056 else if (rv & 0b0010) lv = 1; 00057 else if (rv & 0b0100) lv = 2; 00058 else if (rv & 0b1000) lv = 3; 00059 00060 return (i << 2) | lv; 00061 } 00062 } 00063 return -1; 00064 } 00065 00066 // The Ticker callback function 00067 void callback() 00068 { 00069 //チャタリング防止 00070 int v = keyScan1(); 00071 if (v == keyBuffer) { 00072 keyVal = v; 00073 } 00074 keyBuffer = v; 00075 } 00076 00077 Ticker ticker; 00078 ExioMcp23s17* pDevice; 00079 ExioPort port; 00080 int keyVal; 00081 int keyBuffer; 00082 }; 00083 00084 #endif //_MCP23S17Keypad4x4_
Generated on Thu Jul 14 2022 16:20:00 by
1.7.2