Phil COVAL
/
rzr-example-mbed
Demo example on FRDM-64F https://ide.mbed.com/compiler/#nav:/rzr-example-mbed;
Diff: main.cpp
- Revision:
- 2:15bfd52937dc
- Parent:
- 1:b2103e99708c
- Child:
- 3:807a69c34b8b
--- a/main.cpp Wed Sep 16 15:00:51 2020 +0000 +++ b/main.cpp Thu Nov 19 23:07:40 2020 +0000 @@ -1,4 +1,4 @@ -/// SPDX-License-Indentifier: Apache-2.0 +/// SPDX-License-Indentifier: ISC /// Copyright: 2020+ Philippe Coval <https://purl.org/rzr> /// URL: https://os.mbed.com/users/rzrfreefr/code/rzr-example-mbed/ @@ -8,48 +8,163 @@ #include "USBKeyboard.h" #include "FXOS8700Q.h" -#define USB_ENABLED 1 -#define NET_ENABLED 1 +#define CONFIG_LOG 1 +//#define CONFIG_NET 1 +#define CONFIG_USB 1 + +#if defined(CONFIG_LOG) && CONFIG_LOG +# define logf printf +#else +# define logf if (false) printf +#endif + int main() { - printf("%s\n", __FILE__); - I2C i2c(PTE25, PTE24); - motion_data_units_t acc_data; - motion_data_counts_t acc_raw; + logf("#{ %s\n", __FILE__); + + DigitalOut led(LED1); + led = false; //on + + const float delay = 1000.f / 25; + + I2C i2c(PTE25, PTE24); + motion_data_units_t acc_data; + motion_data_counts_t acc_raw; + + float vorigin[] = {0, 0, 0}; + float vacc[] = {0, 0, 0}; + const int len = 16; + float vhist[len][3]; + + float vmean[] = {0, 0, 0}; + float vmin[] = {0, 0, 0}; + float vmax[] = {0, 0, 0}; + + float threshold = 1./3.; + float vthreshold[] = {threshold, threshold, threshold}; - FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board -#ifdef USB_ENABLED - printf("usb:\n"); - USBKeyboard usb; ///< https://os.mbed.com/docs/mbed-os/v6.2/apis/usbkeyboard.html +#if 1 + int keymap[3][2] = { + { LEFT_ARROW, RIGHT_ARROW }, + { DOWN_ARROW, UP_ARROW }, + { KEY_PAGE_DOWN, KEY_PAGE_UP } + }; +#else + int keymap[3][2] = { + { KEY_CTRL, KEY_RCTRL }, + { DOWN_ARROW, UP_ARROW }, + { KEY_PAGE_DOWN, KEY_PAGE_UP } + }; +#endif + + + FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board +#if defined(CONFIG_USB) && CONFIG_USB + logf("# usb:\n"); + USBKeyboard usb; ///< https://os.mbed.com/docs/mbed-os/v6.2/apis/usbkeyboard.html +#endif + +#if defined(CONFIG_NET) && CONFIG_NET + EthernetInterface eth; ///< https://os.mbed.com/docs/mbed-os/v6.2/apis/ethernet.html + eth.set_dhcp(true); + eth.connect(); + logf("\n# MAC address: %s\n", eth.get_mac_address()); + SocketAddress a; + eth.get_ip_address(&a); + char* addr = a.get_ip_address() || "127.0.0.1"; + logf("# IP address: %s\n", addr); #endif -#ifdef NET_ENABLED - EthernetInterface eth; ///< https://os.mbed.com/docs/mbed-os/v6.2/apis/ethernet.html - eth.set_dhcp(true); - eth.connect(); -#endif - - DigitalOut led(LED1); - - int16_t rmX, rmY, rmZ; - acc.enable(); - printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", acc.whoAmI()); - while (true) { + acc.enable(); acc.getAxis(acc_data); - printf("FXOS8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f ", acc_data.x, acc_data.y, acc_data.z); - printf("%s\n", __FILE__); - led = !led; -#ifdef NET_ENABLED - printf("\nClient IP Address is %s\n", eth.get_mac_address()); - SocketAddress a; - eth.get_ip_address(&a); - printf("IP address: %s\n", a.get_ip_address() ? a.get_ip_address() : "None"); + vorigin[0] = acc_data.x; + vorigin[1] = acc_data.y; + vorigin[2] = acc_data.z; + + logf("\r\n\n# FXOS8700Q: Who Am I= %X\r\n", acc.whoAmI()); + + int key = 0; + for(int iter=0; iter<len; ++iter) { + if (false) led = !led; + acc.getAxis(acc_data); + vacc[0] = acc_data.x - vorigin[0]; + vacc[1] = acc_data.y - vorigin[1]; + vacc[2] = acc_data.z - vorigin[2]; + logf("{\"accel\":[%.2f,%.2f,%.2f]}\r\n", vacc[0], vacc[1], vacc[2]); + +#if defined(CONFIG_USB) && CONFIG_USB + usb.connect(); + + if (key == 0) { + for(int i=2; i>=0; i--) { + if (vacc[i] > vthreshold[i]) { + key = keymap[i][0]; + } else if (vacc[i] < -vthreshold[i]) { + key = keymap[i][1]; + } + } + + if (key != 0) { + led = false; //on + logf("# key: %0X\n", key); + if (!true) { + usb.key_code(key, key); + } else { + usb.key_code(key); + } + } + iter=0; + continue; + } #endif -#ifdef USB_ENABLED - usb.connect(); - usb.printf("# \n"); -#endif - ThisThread::sleep_for(1000.f); - } -} + + for (int i=0; i<3; i++) { + vhist[iter][i] = vacc[i]; + } + + if (iter==len-1) { + led = false; // heart beat + led = true; + + for (int i=0; i<3; i++) { + vmean[i] = 0; + vmin[i] = vmax[i] = vhist[iter][i]; + } + + for (iter=0; iter<len; iter++) { + for (int i=0; i<3; i++) { + vmean[i] += vhist[iter][i]; + if (vmin[i] > vhist[iter][i]) { + vmin[i] = vhist[iter][i]; + } + if (vmax[i] < vhist[iter][i]) { + vmax[i] = vhist[iter][i]; + } + } + } + for (int i=0; i<3; i++) { + vmean[i] /= len; + } + + bool reset = true; + for (int k=0; k<3; k++) { + if (abs(vmax[k] - vmin[k]) > threshold*.8) { + reset &= false; + } + } + if (reset) { + logf("\r\n# Reset origin\n"); + vorigin[0] = acc_data.x; + vorigin[1] = acc_data.y; + vorigin[2] = acc_data.z; + + key = 0; // unbounce + led = true; //off + } + iter=0; + } + ThisThread::sleep_for(delay); + } + logf("#} %s\n", __FILE__); +} \ No newline at end of file