Phil COVAL
/
rzr-example-mbed
Demo example on FRDM-64F https://ide.mbed.com/compiler/#nav:/rzr-example-mbed;
main.cpp@2:15bfd52937dc, 2020-11-19 (annotated)
- Committer:
- rzrfreefr
- Date:
- Thu Nov 19 23:07:40 2020 +0000
- Revision:
- 2:15bfd52937dc
- Parent:
- 1:b2103e99708c
- Child:
- 3:807a69c34b8b
Implement basic keyboard,; ; But unfortunately it will not work as expected due to press/release timestamp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rzrfreefr | 2:15bfd52937dc | 1 | /// SPDX-License-Indentifier: ISC |
rzrfreefr | 0:16519bf92477 | 2 | /// Copyright: 2020+ Philippe Coval <https://purl.org/rzr> |
rzrfreefr | 1:b2103e99708c | 3 | /// URL: https://os.mbed.com/users/rzrfreefr/code/rzr-example-mbed/ |
rzrfreefr | 0:16519bf92477 | 4 | |
rzrfreefr | 0:16519bf92477 | 5 | #include "mbed.h" |
rzrfreefr | 0:16519bf92477 | 6 | |
rzrfreefr | 0:16519bf92477 | 7 | #include "EthernetInterface.h" |
rzrfreefr | 0:16519bf92477 | 8 | #include "USBKeyboard.h" |
rzrfreefr | 0:16519bf92477 | 9 | #include "FXOS8700Q.h" |
rzrfreefr | 0:16519bf92477 | 10 | |
rzrfreefr | 2:15bfd52937dc | 11 | #define CONFIG_LOG 1 |
rzrfreefr | 2:15bfd52937dc | 12 | //#define CONFIG_NET 1 |
rzrfreefr | 2:15bfd52937dc | 13 | #define CONFIG_USB 1 |
rzrfreefr | 2:15bfd52937dc | 14 | |
rzrfreefr | 2:15bfd52937dc | 15 | #if defined(CONFIG_LOG) && CONFIG_LOG |
rzrfreefr | 2:15bfd52937dc | 16 | # define logf printf |
rzrfreefr | 2:15bfd52937dc | 17 | #else |
rzrfreefr | 2:15bfd52937dc | 18 | # define logf if (false) printf |
rzrfreefr | 2:15bfd52937dc | 19 | #endif |
rzrfreefr | 2:15bfd52937dc | 20 | |
rzrfreefr | 0:16519bf92477 | 21 | |
rzrfreefr | 0:16519bf92477 | 22 | int main() |
rzrfreefr | 0:16519bf92477 | 23 | { |
rzrfreefr | 2:15bfd52937dc | 24 | logf("#{ %s\n", __FILE__); |
rzrfreefr | 2:15bfd52937dc | 25 | |
rzrfreefr | 2:15bfd52937dc | 26 | DigitalOut led(LED1); |
rzrfreefr | 2:15bfd52937dc | 27 | led = false; //on |
rzrfreefr | 2:15bfd52937dc | 28 | |
rzrfreefr | 2:15bfd52937dc | 29 | const float delay = 1000.f / 25; |
rzrfreefr | 2:15bfd52937dc | 30 | |
rzrfreefr | 2:15bfd52937dc | 31 | I2C i2c(PTE25, PTE24); |
rzrfreefr | 2:15bfd52937dc | 32 | motion_data_units_t acc_data; |
rzrfreefr | 2:15bfd52937dc | 33 | motion_data_counts_t acc_raw; |
rzrfreefr | 2:15bfd52937dc | 34 | |
rzrfreefr | 2:15bfd52937dc | 35 | float vorigin[] = {0, 0, 0}; |
rzrfreefr | 2:15bfd52937dc | 36 | float vacc[] = {0, 0, 0}; |
rzrfreefr | 2:15bfd52937dc | 37 | const int len = 16; |
rzrfreefr | 2:15bfd52937dc | 38 | float vhist[len][3]; |
rzrfreefr | 2:15bfd52937dc | 39 | |
rzrfreefr | 2:15bfd52937dc | 40 | float vmean[] = {0, 0, 0}; |
rzrfreefr | 2:15bfd52937dc | 41 | float vmin[] = {0, 0, 0}; |
rzrfreefr | 2:15bfd52937dc | 42 | float vmax[] = {0, 0, 0}; |
rzrfreefr | 2:15bfd52937dc | 43 | |
rzrfreefr | 2:15bfd52937dc | 44 | float threshold = 1./3.; |
rzrfreefr | 2:15bfd52937dc | 45 | float vthreshold[] = {threshold, threshold, threshold}; |
rzrfreefr | 0:16519bf92477 | 46 | |
rzrfreefr | 2:15bfd52937dc | 47 | #if 1 |
rzrfreefr | 2:15bfd52937dc | 48 | int keymap[3][2] = { |
rzrfreefr | 2:15bfd52937dc | 49 | { LEFT_ARROW, RIGHT_ARROW }, |
rzrfreefr | 2:15bfd52937dc | 50 | { DOWN_ARROW, UP_ARROW }, |
rzrfreefr | 2:15bfd52937dc | 51 | { KEY_PAGE_DOWN, KEY_PAGE_UP } |
rzrfreefr | 2:15bfd52937dc | 52 | }; |
rzrfreefr | 2:15bfd52937dc | 53 | #else |
rzrfreefr | 2:15bfd52937dc | 54 | int keymap[3][2] = { |
rzrfreefr | 2:15bfd52937dc | 55 | { KEY_CTRL, KEY_RCTRL }, |
rzrfreefr | 2:15bfd52937dc | 56 | { DOWN_ARROW, UP_ARROW }, |
rzrfreefr | 2:15bfd52937dc | 57 | { KEY_PAGE_DOWN, KEY_PAGE_UP } |
rzrfreefr | 2:15bfd52937dc | 58 | }; |
rzrfreefr | 2:15bfd52937dc | 59 | #endif |
rzrfreefr | 2:15bfd52937dc | 60 | |
rzrfreefr | 2:15bfd52937dc | 61 | |
rzrfreefr | 2:15bfd52937dc | 62 | FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board |
rzrfreefr | 2:15bfd52937dc | 63 | #if defined(CONFIG_USB) && CONFIG_USB |
rzrfreefr | 2:15bfd52937dc | 64 | logf("# usb:\n"); |
rzrfreefr | 2:15bfd52937dc | 65 | USBKeyboard usb; ///< https://os.mbed.com/docs/mbed-os/v6.2/apis/usbkeyboard.html |
rzrfreefr | 2:15bfd52937dc | 66 | #endif |
rzrfreefr | 2:15bfd52937dc | 67 | |
rzrfreefr | 2:15bfd52937dc | 68 | #if defined(CONFIG_NET) && CONFIG_NET |
rzrfreefr | 2:15bfd52937dc | 69 | EthernetInterface eth; ///< https://os.mbed.com/docs/mbed-os/v6.2/apis/ethernet.html |
rzrfreefr | 2:15bfd52937dc | 70 | eth.set_dhcp(true); |
rzrfreefr | 2:15bfd52937dc | 71 | eth.connect(); |
rzrfreefr | 2:15bfd52937dc | 72 | logf("\n# MAC address: %s\n", eth.get_mac_address()); |
rzrfreefr | 2:15bfd52937dc | 73 | SocketAddress a; |
rzrfreefr | 2:15bfd52937dc | 74 | eth.get_ip_address(&a); |
rzrfreefr | 2:15bfd52937dc | 75 | char* addr = a.get_ip_address() || "127.0.0.1"; |
rzrfreefr | 2:15bfd52937dc | 76 | logf("# IP address: %s\n", addr); |
rzrfreefr | 0:16519bf92477 | 77 | #endif |
rzrfreefr | 0:16519bf92477 | 78 | |
rzrfreefr | 2:15bfd52937dc | 79 | acc.enable(); |
rzrfreefr | 0:16519bf92477 | 80 | acc.getAxis(acc_data); |
rzrfreefr | 2:15bfd52937dc | 81 | vorigin[0] = acc_data.x; |
rzrfreefr | 2:15bfd52937dc | 82 | vorigin[1] = acc_data.y; |
rzrfreefr | 2:15bfd52937dc | 83 | vorigin[2] = acc_data.z; |
rzrfreefr | 2:15bfd52937dc | 84 | |
rzrfreefr | 2:15bfd52937dc | 85 | logf("\r\n\n# FXOS8700Q: Who Am I= %X\r\n", acc.whoAmI()); |
rzrfreefr | 2:15bfd52937dc | 86 | |
rzrfreefr | 2:15bfd52937dc | 87 | int key = 0; |
rzrfreefr | 2:15bfd52937dc | 88 | for(int iter=0; iter<len; ++iter) { |
rzrfreefr | 2:15bfd52937dc | 89 | if (false) led = !led; |
rzrfreefr | 2:15bfd52937dc | 90 | acc.getAxis(acc_data); |
rzrfreefr | 2:15bfd52937dc | 91 | vacc[0] = acc_data.x - vorigin[0]; |
rzrfreefr | 2:15bfd52937dc | 92 | vacc[1] = acc_data.y - vorigin[1]; |
rzrfreefr | 2:15bfd52937dc | 93 | vacc[2] = acc_data.z - vorigin[2]; |
rzrfreefr | 2:15bfd52937dc | 94 | logf("{\"accel\":[%.2f,%.2f,%.2f]}\r\n", vacc[0], vacc[1], vacc[2]); |
rzrfreefr | 2:15bfd52937dc | 95 | |
rzrfreefr | 2:15bfd52937dc | 96 | #if defined(CONFIG_USB) && CONFIG_USB |
rzrfreefr | 2:15bfd52937dc | 97 | usb.connect(); |
rzrfreefr | 2:15bfd52937dc | 98 | |
rzrfreefr | 2:15bfd52937dc | 99 | if (key == 0) { |
rzrfreefr | 2:15bfd52937dc | 100 | for(int i=2; i>=0; i--) { |
rzrfreefr | 2:15bfd52937dc | 101 | if (vacc[i] > vthreshold[i]) { |
rzrfreefr | 2:15bfd52937dc | 102 | key = keymap[i][0]; |
rzrfreefr | 2:15bfd52937dc | 103 | } else if (vacc[i] < -vthreshold[i]) { |
rzrfreefr | 2:15bfd52937dc | 104 | key = keymap[i][1]; |
rzrfreefr | 2:15bfd52937dc | 105 | } |
rzrfreefr | 2:15bfd52937dc | 106 | } |
rzrfreefr | 2:15bfd52937dc | 107 | |
rzrfreefr | 2:15bfd52937dc | 108 | if (key != 0) { |
rzrfreefr | 2:15bfd52937dc | 109 | led = false; //on |
rzrfreefr | 2:15bfd52937dc | 110 | logf("# key: %0X\n", key); |
rzrfreefr | 2:15bfd52937dc | 111 | if (!true) { |
rzrfreefr | 2:15bfd52937dc | 112 | usb.key_code(key, key); |
rzrfreefr | 2:15bfd52937dc | 113 | } else { |
rzrfreefr | 2:15bfd52937dc | 114 | usb.key_code(key); |
rzrfreefr | 2:15bfd52937dc | 115 | } |
rzrfreefr | 2:15bfd52937dc | 116 | } |
rzrfreefr | 2:15bfd52937dc | 117 | iter=0; |
rzrfreefr | 2:15bfd52937dc | 118 | continue; |
rzrfreefr | 2:15bfd52937dc | 119 | } |
rzrfreefr | 0:16519bf92477 | 120 | #endif |
rzrfreefr | 2:15bfd52937dc | 121 | |
rzrfreefr | 2:15bfd52937dc | 122 | for (int i=0; i<3; i++) { |
rzrfreefr | 2:15bfd52937dc | 123 | vhist[iter][i] = vacc[i]; |
rzrfreefr | 2:15bfd52937dc | 124 | } |
rzrfreefr | 2:15bfd52937dc | 125 | |
rzrfreefr | 2:15bfd52937dc | 126 | if (iter==len-1) { |
rzrfreefr | 2:15bfd52937dc | 127 | led = false; // heart beat |
rzrfreefr | 2:15bfd52937dc | 128 | led = true; |
rzrfreefr | 2:15bfd52937dc | 129 | |
rzrfreefr | 2:15bfd52937dc | 130 | for (int i=0; i<3; i++) { |
rzrfreefr | 2:15bfd52937dc | 131 | vmean[i] = 0; |
rzrfreefr | 2:15bfd52937dc | 132 | vmin[i] = vmax[i] = vhist[iter][i]; |
rzrfreefr | 2:15bfd52937dc | 133 | } |
rzrfreefr | 2:15bfd52937dc | 134 | |
rzrfreefr | 2:15bfd52937dc | 135 | for (iter=0; iter<len; iter++) { |
rzrfreefr | 2:15bfd52937dc | 136 | for (int i=0; i<3; i++) { |
rzrfreefr | 2:15bfd52937dc | 137 | vmean[i] += vhist[iter][i]; |
rzrfreefr | 2:15bfd52937dc | 138 | if (vmin[i] > vhist[iter][i]) { |
rzrfreefr | 2:15bfd52937dc | 139 | vmin[i] = vhist[iter][i]; |
rzrfreefr | 2:15bfd52937dc | 140 | } |
rzrfreefr | 2:15bfd52937dc | 141 | if (vmax[i] < vhist[iter][i]) { |
rzrfreefr | 2:15bfd52937dc | 142 | vmax[i] = vhist[iter][i]; |
rzrfreefr | 2:15bfd52937dc | 143 | } |
rzrfreefr | 2:15bfd52937dc | 144 | } |
rzrfreefr | 2:15bfd52937dc | 145 | } |
rzrfreefr | 2:15bfd52937dc | 146 | for (int i=0; i<3; i++) { |
rzrfreefr | 2:15bfd52937dc | 147 | vmean[i] /= len; |
rzrfreefr | 2:15bfd52937dc | 148 | } |
rzrfreefr | 2:15bfd52937dc | 149 | |
rzrfreefr | 2:15bfd52937dc | 150 | bool reset = true; |
rzrfreefr | 2:15bfd52937dc | 151 | for (int k=0; k<3; k++) { |
rzrfreefr | 2:15bfd52937dc | 152 | if (abs(vmax[k] - vmin[k]) > threshold*.8) { |
rzrfreefr | 2:15bfd52937dc | 153 | reset &= false; |
rzrfreefr | 2:15bfd52937dc | 154 | } |
rzrfreefr | 2:15bfd52937dc | 155 | } |
rzrfreefr | 2:15bfd52937dc | 156 | if (reset) { |
rzrfreefr | 2:15bfd52937dc | 157 | logf("\r\n# Reset origin\n"); |
rzrfreefr | 2:15bfd52937dc | 158 | vorigin[0] = acc_data.x; |
rzrfreefr | 2:15bfd52937dc | 159 | vorigin[1] = acc_data.y; |
rzrfreefr | 2:15bfd52937dc | 160 | vorigin[2] = acc_data.z; |
rzrfreefr | 2:15bfd52937dc | 161 | |
rzrfreefr | 2:15bfd52937dc | 162 | key = 0; // unbounce |
rzrfreefr | 2:15bfd52937dc | 163 | led = true; //off |
rzrfreefr | 2:15bfd52937dc | 164 | } |
rzrfreefr | 2:15bfd52937dc | 165 | iter=0; |
rzrfreefr | 2:15bfd52937dc | 166 | } |
rzrfreefr | 2:15bfd52937dc | 167 | ThisThread::sleep_for(delay); |
rzrfreefr | 2:15bfd52937dc | 168 | } |
rzrfreefr | 2:15bfd52937dc | 169 | logf("#} %s\n", __FILE__); |
rzrfreefr | 2:15bfd52937dc | 170 | } |