First Update
@@ -1,201 +0,0 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -1,56 +0,0 @@
|
||||
# Flipper Air Mouse
|
||||
|
||||
## Brief
|
||||
|
||||
> "You can turn anything into an air mouse if you're brave enough"
|
||||
|
||||
— Piper, a.k.a. Pez
|
||||
|
||||
Naturally, the quote above applies to [Flipper](https://flipperzero.one/) as well.
|
||||
|
||||
## What?
|
||||
|
||||
The app allows you to turn your Flipper into a USB or Bluetooth air mouse (you do need an extra module, see the Hardware section below)...
|
||||
|
||||
Using it is really simple:
|
||||
* Connect the Flipper via a USB cable and pick `USB`, or pick `Bluetooth` and pair it with your PC;
|
||||
* Hold the Flipper in your hand with the buttons pointing towards the screen;
|
||||
* Wave your Flipper like you don't care to move the cursor;
|
||||
* Up button for Left mouse click;
|
||||
* Down button for Right mouse click;
|
||||
* Center button for Middle mouse click;
|
||||
* Use calibration menu option if you notice significant drift (place your Flipper onto a level surface, make sure it doesn't move, run this option, wait 2 seconds, done).
|
||||
|
||||
See early prototype [in action](https://www.youtube.com/watch?v=DdxAmmsYfMA).
|
||||
|
||||
## Hardware
|
||||
|
||||
The custom module is using Bosch BMI160 accelerometer/gyroscope chip connected via I2C.
|
||||
|
||||
Take a look into the [schematic](https://github.com/ginkage/FlippAirMouse/tree/main/schematic) folder for Gerber, BOM and CPL files, so you can order directly from JLCPCB.
|
||||
|
||||
Original idea:
|
||||
|
||||

|
||||
|
||||
Expectation:
|
||||
|
||||

|
||||
|
||||
Reality:
|
||||
|
||||

|
||||
|
||||
|
||||
## Software
|
||||
|
||||
The code is based on the original Bosch [driver](https://github.com/BoschSensortec/BMI160_driver/) and an orientation tracking implementation from the [Cardboard](https://github.com/googlevr/cardboard/tree/master/sdk/sensors) project
|
||||
|
||||
If you're familiar with Flipper applications, start in the [firmware](https://github.com/flipperdevices/flipperzero-firmware) checkout folder and do the following:
|
||||
```
|
||||
cd applications/plugins
|
||||
git clone https://github.com/ginkage/FlippAirMouse
|
||||
cd ../..
|
||||
./fbt fap_air_mouse
|
||||
```
|
||||
If you're not familiar with those, just grab a `fap` file from Releases.
|
||||
@@ -1,156 +0,0 @@
|
||||
#include "air_mouse.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <dolphin/dolphin.h>
|
||||
|
||||
#include "tracking/imu/imu.h"
|
||||
|
||||
#define TAG "AirMouseApp"
|
||||
|
||||
enum AirMouseSubmenuIndex {
|
||||
AirMouseSubmenuIndexBtMouse,
|
||||
AirMouseSubmenuIndexUsbMouse,
|
||||
AirMouseSubmenuIndexCalibration,
|
||||
};
|
||||
|
||||
void air_mouse_submenu_callback(void* context, uint32_t index) {
|
||||
furi_assert(context);
|
||||
AirMouse* app = context;
|
||||
if(index == AirMouseSubmenuIndexBtMouse) {
|
||||
app->view_id = AirMouseViewBtMouse;
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, AirMouseViewBtMouse);
|
||||
} else if(index == AirMouseSubmenuIndexUsbMouse) {
|
||||
app->view_id = AirMouseViewUsbMouse;
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, AirMouseViewUsbMouse);
|
||||
} else if(index == AirMouseSubmenuIndexCalibration) {
|
||||
app->view_id = AirMouseViewCalibration;
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, AirMouseViewCalibration);
|
||||
}
|
||||
}
|
||||
|
||||
void air_mouse_dialog_callback(DialogExResult result, void* context) {
|
||||
furi_assert(context);
|
||||
AirMouse* app = context;
|
||||
if(result == DialogExResultLeft) {
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_NONE); // Exit
|
||||
} else if(result == DialogExResultRight) {
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, app->view_id); // Show last view
|
||||
} else if(result == DialogExResultCenter) {
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, AirMouseViewSubmenu); // Menu
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t air_mouse_exit_confirm_view(void* context) {
|
||||
UNUSED(context);
|
||||
return AirMouseViewExitConfirm;
|
||||
}
|
||||
|
||||
uint32_t air_mouse_exit(void* context) {
|
||||
UNUSED(context);
|
||||
return VIEW_NONE;
|
||||
}
|
||||
|
||||
AirMouse* air_mouse_app_alloc() {
|
||||
AirMouse* app = malloc(sizeof(AirMouse));
|
||||
|
||||
// Gui
|
||||
app->gui = furi_record_open(RECORD_GUI);
|
||||
|
||||
// View dispatcher
|
||||
app->view_dispatcher = view_dispatcher_alloc();
|
||||
view_dispatcher_enable_queue(app->view_dispatcher);
|
||||
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
|
||||
|
||||
// Submenu view
|
||||
app->submenu = submenu_alloc();
|
||||
submenu_add_item(
|
||||
app->submenu, "Bluetooth", AirMouseSubmenuIndexBtMouse, air_mouse_submenu_callback, app);
|
||||
submenu_add_item(
|
||||
app->submenu, "USB", AirMouseSubmenuIndexUsbMouse, air_mouse_submenu_callback, app);
|
||||
submenu_add_item(
|
||||
app->submenu,
|
||||
"Calibration",
|
||||
AirMouseSubmenuIndexCalibration,
|
||||
air_mouse_submenu_callback,
|
||||
app);
|
||||
view_set_previous_callback(submenu_get_view(app->submenu), air_mouse_exit);
|
||||
view_dispatcher_add_view(
|
||||
app->view_dispatcher, AirMouseViewSubmenu, submenu_get_view(app->submenu));
|
||||
|
||||
// Dialog view
|
||||
app->dialog = dialog_ex_alloc();
|
||||
dialog_ex_set_result_callback(app->dialog, air_mouse_dialog_callback);
|
||||
dialog_ex_set_context(app->dialog, app);
|
||||
dialog_ex_set_left_button_text(app->dialog, "Exit");
|
||||
dialog_ex_set_right_button_text(app->dialog, "Stay");
|
||||
dialog_ex_set_center_button_text(app->dialog, "Menu");
|
||||
dialog_ex_set_header(app->dialog, "Close Current App?", 16, 12, AlignLeft, AlignTop);
|
||||
view_dispatcher_add_view(
|
||||
app->view_dispatcher, AirMouseViewExitConfirm, dialog_ex_get_view(app->dialog));
|
||||
|
||||
// Bluetooth view
|
||||
app->bt_mouse = bt_mouse_alloc(app->view_dispatcher);
|
||||
view_set_previous_callback(bt_mouse_get_view(app->bt_mouse), air_mouse_exit_confirm_view);
|
||||
view_dispatcher_add_view(
|
||||
app->view_dispatcher, AirMouseViewBtMouse, bt_mouse_get_view(app->bt_mouse));
|
||||
|
||||
// USB view
|
||||
app->usb_mouse = usb_mouse_alloc(app->view_dispatcher);
|
||||
view_set_previous_callback(usb_mouse_get_view(app->usb_mouse), air_mouse_exit_confirm_view);
|
||||
view_dispatcher_add_view(
|
||||
app->view_dispatcher, AirMouseViewUsbMouse, usb_mouse_get_view(app->usb_mouse));
|
||||
|
||||
// Calibration view
|
||||
app->calibration = calibration_alloc(app->view_dispatcher);
|
||||
view_set_previous_callback(
|
||||
calibration_get_view(app->calibration), air_mouse_exit_confirm_view);
|
||||
view_dispatcher_add_view(
|
||||
app->view_dispatcher, AirMouseViewCalibration, calibration_get_view(app->calibration));
|
||||
|
||||
app->view_id = AirMouseViewSubmenu;
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, app->view_id);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
void air_mouse_app_free(AirMouse* app) {
|
||||
furi_assert(app);
|
||||
|
||||
// Free views
|
||||
view_dispatcher_remove_view(app->view_dispatcher, AirMouseViewSubmenu);
|
||||
submenu_free(app->submenu);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, AirMouseViewExitConfirm);
|
||||
dialog_ex_free(app->dialog);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, AirMouseViewBtMouse);
|
||||
bt_mouse_free(app->bt_mouse);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, AirMouseViewUsbMouse);
|
||||
usb_mouse_free(app->usb_mouse);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, AirMouseViewCalibration);
|
||||
calibration_free(app->calibration);
|
||||
view_dispatcher_free(app->view_dispatcher);
|
||||
|
||||
// Close records
|
||||
furi_record_close(RECORD_GUI);
|
||||
app->gui = NULL;
|
||||
|
||||
// Free rest
|
||||
free(app);
|
||||
}
|
||||
|
||||
int32_t air_mouse_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
AirMouse* app = air_mouse_app_alloc();
|
||||
if(!imu_begin()) {
|
||||
air_mouse_app_free(app);
|
||||
return -1;
|
||||
}
|
||||
|
||||
DOLPHIN_DEED(DolphinDeedPluginStart);
|
||||
view_dispatcher_run(app->view_dispatcher);
|
||||
|
||||
imu_end();
|
||||
air_mouse_app_free(app);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/gui.h>
|
||||
#include <gui/view.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
#include <gui/modules/submenu.h>
|
||||
#include <gui/modules/dialog_ex.h>
|
||||
|
||||
#include "views/bt_mouse.h"
|
||||
#include "views/usb_mouse.h"
|
||||
#include "views/calibration.h"
|
||||
|
||||
typedef struct {
|
||||
Gui* gui;
|
||||
ViewDispatcher* view_dispatcher;
|
||||
Submenu* submenu;
|
||||
DialogEx* dialog;
|
||||
BtMouse* bt_mouse;
|
||||
UsbMouse* usb_mouse;
|
||||
Calibration* calibration;
|
||||
uint32_t view_id;
|
||||
} AirMouse;
|
||||
|
||||
typedef enum {
|
||||
AirMouseViewSubmenu,
|
||||
AirMouseViewBtMouse,
|
||||
AirMouseViewUsbMouse,
|
||||
AirMouseViewCalibration,
|
||||
AirMouseViewExitConfirm,
|
||||
} AirMouseView;
|
||||
@@ -1,9 +0,0 @@
|
||||
App(
|
||||
appid="Air_Mouse",
|
||||
name="[BMI160] Air Mouse",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="air_mouse_app",
|
||||
stack_size=10 * 1024,
|
||||
fap_icon="mouse_10px.png",
|
||||
fap_category="NeedsTesting",
|
||||
)
|
||||
|
Before Width: | Height: | Size: 1.6 KiB |
@@ -1,85 +0,0 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
#define TAG "tracker"
|
||||
|
||||
#include "calibration_data.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
// Student's distribution T value for 95% (two-sided) confidence interval.
|
||||
static const double Tn = 1.960;
|
||||
|
||||
// Number of samples (degrees of freedom) for the corresponding T values.
|
||||
static const int Nn = 200;
|
||||
|
||||
void CalibrationData::reset()
|
||||
{
|
||||
complete = false;
|
||||
count = 0;
|
||||
sum = Vector::Zero();
|
||||
sumSq = Vector::Zero();
|
||||
mean = Vector::Zero();
|
||||
median = Vector::Zero();
|
||||
sigma = Vector::Zero();
|
||||
delta = Vector::Zero();
|
||||
xData.clear();
|
||||
yData.clear();
|
||||
zData.clear();
|
||||
}
|
||||
|
||||
bool CalibrationData::add(Vector& data)
|
||||
{
|
||||
if (complete) {
|
||||
return true;
|
||||
}
|
||||
|
||||
xData.push_back(data[0]);
|
||||
yData.push_back(data[1]);
|
||||
zData.push_back(data[2]);
|
||||
|
||||
sum += data;
|
||||
sumSq += data * data;
|
||||
count++;
|
||||
|
||||
if (count >= Nn) {
|
||||
calcDelta();
|
||||
complete = true;
|
||||
}
|
||||
|
||||
return complete;
|
||||
}
|
||||
|
||||
static inline double medianOf(std::vector<double>& list)
|
||||
{
|
||||
std::sort(list.begin(), list.end());
|
||||
int count = list.size();
|
||||
int middle = count / 2;
|
||||
return (count % 2 == 1) ? list[middle] : (list[middle - 1] + list[middle]) / 2.0l;
|
||||
}
|
||||
|
||||
void CalibrationData::calcDelta()
|
||||
{
|
||||
median.Set(medianOf(xData), medianOf(yData), medianOf(zData));
|
||||
|
||||
mean = sum / count;
|
||||
Vector m2 = mean * mean;
|
||||
Vector d = sumSq / count - m2;
|
||||
Vector s2 = (d * count) / (count - 1);
|
||||
sigma = Vector(std::sqrt(d[0]), std::sqrt(d[1]), std::sqrt(d[2]));
|
||||
Vector s = Vector(std::sqrt(s2[0]), std::sqrt(s2[1]), std::sqrt(s2[2]));
|
||||
delta = s * Tn / std::sqrt((double)count);
|
||||
Vector low = mean - delta;
|
||||
Vector high = mean + delta;
|
||||
|
||||
FURI_LOG_I(TAG,
|
||||
"M[x] = { %f ... %f } // median = %f // avg = %f // delta = %f // sigma = %f",
|
||||
low[0], high[0], median[0], mean[0], delta[0], sigma[0]);
|
||||
FURI_LOG_I(TAG,
|
||||
"M[y] = { %f ... %f } // median = %f // avg = %f // delta = %f // sigma = %f",
|
||||
low[1], high[1], median[1], mean[1], delta[1], sigma[1]);
|
||||
FURI_LOG_I(TAG,
|
||||
"M[z] = { %f ... %f } // median = %f // avg = %f // delta = %f // sigma = %f",
|
||||
low[2], high[2], median[2], mean[2], delta[2], sigma[2]);
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <toolbox/saved_struct.h>
|
||||
#include <storage/storage.h>
|
||||
#include <vector>
|
||||
|
||||
#include "util/vector.h"
|
||||
|
||||
#define CALIBRATION_DATA_VER (1)
|
||||
#define CALIBRATION_DATA_FILE_NAME ".calibration.data"
|
||||
#define CALIBRATION_DATA_PATH INT_PATH(CALIBRATION_DATA_FILE_NAME)
|
||||
#define CALIBRATION_DATA_MAGIC (0x23)
|
||||
|
||||
#define CALIBRATION_DATA_SAVE(x) \
|
||||
saved_struct_save( \
|
||||
CALIBRATION_DATA_PATH, \
|
||||
(x), \
|
||||
sizeof(CalibrationMedian), \
|
||||
CALIBRATION_DATA_MAGIC, \
|
||||
CALIBRATION_DATA_VER)
|
||||
|
||||
#define CALIBRATION_DATA_LOAD(x) \
|
||||
saved_struct_load( \
|
||||
CALIBRATION_DATA_PATH, \
|
||||
(x), \
|
||||
sizeof(CalibrationMedian), \
|
||||
CALIBRATION_DATA_MAGIC, \
|
||||
CALIBRATION_DATA_VER)
|
||||
|
||||
typedef struct {
|
||||
double x;
|
||||
double y;
|
||||
double z;
|
||||
} CalibrationMedian;
|
||||
|
||||
typedef cardboard::Vector3 Vector;
|
||||
|
||||
/**
|
||||
* Helper class to gather some stats and store the calibration data. Right now it calculates a lot
|
||||
* more stats than actually needed. Some of them are used for logging the sensors quality (and
|
||||
* filing bugs), other may be required in the future, e.g. for bias.
|
||||
*/
|
||||
class CalibrationData {
|
||||
public:
|
||||
/**
|
||||
* Check if the sensors were calibrated before.
|
||||
*
|
||||
* @return {@code true} if calibration data is available, or {@code false} otherwise.
|
||||
*/
|
||||
bool isComplete() {
|
||||
return complete;
|
||||
}
|
||||
|
||||
/** Prepare to collect new calibration data. */
|
||||
void reset();
|
||||
|
||||
/**
|
||||
* Retrieve the median gyroscope readings.
|
||||
*
|
||||
* @return Three-axis median vector.
|
||||
*/
|
||||
Vector getMedian() {
|
||||
return median;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the mean gyroscope readings.
|
||||
*
|
||||
* @return Three-axis mean vector.
|
||||
*/
|
||||
Vector getMean() {
|
||||
return mean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the standard deviation of gyroscope readings.
|
||||
*
|
||||
* @return Three-axis standard deviation vector.
|
||||
*/
|
||||
Vector getSigma() {
|
||||
return sigma;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the confidence interval size of gyroscope readings.
|
||||
*
|
||||
* @return Three-axis confidence interval size vector.
|
||||
*/
|
||||
Vector getDelta() {
|
||||
return delta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new gyroscope reading to the stats.
|
||||
*
|
||||
* @param data gyroscope values vector.
|
||||
* @return {@code true} if we now have enough data for calibration, or {@code false} otherwise.
|
||||
*/
|
||||
bool add(Vector& data);
|
||||
|
||||
private:
|
||||
// Calculates the confidence interval (mean +- delta) and some other related values, like
|
||||
// standard deviation, etc. See https://en.wikipedia.org/wiki/Student%27s_t-distribution
|
||||
void calcDelta();
|
||||
|
||||
int count;
|
||||
bool complete;
|
||||
Vector sum;
|
||||
Vector sumSq;
|
||||
Vector mean;
|
||||
Vector median;
|
||||
Vector sigma;
|
||||
Vector delta;
|
||||
std::vector<double> xData;
|
||||
std::vector<double> yData;
|
||||
std::vector<double> zData;
|
||||
};
|
||||
@@ -1,992 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Bosch Sensortec GmbH. All rights reserved.
|
||||
*
|
||||
* BSD-3-Clause
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @file bmi160.h
|
||||
* @date 2021-10-05
|
||||
* @version v3.9.2
|
||||
*
|
||||
*/
|
||||
|
||||
/*!
|
||||
* @defgroup bmi160 BMI160
|
||||
*/
|
||||
|
||||
#ifndef BMI160_H_
|
||||
#define BMI160_H_
|
||||
|
||||
/*************************** C++ guard macro *****************************/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "bmi160_defs.h"
|
||||
#ifdef __KERNEL__
|
||||
#include <bmi160_math.h>
|
||||
#else
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
/*********************** User function prototypes ************************/
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiInit Initialization
|
||||
* @brief Initialize the sensor and device structure
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiInit
|
||||
* \page bmi160_api_bmi160_init bmi160_init
|
||||
* \code
|
||||
* int8_t bmi160_init(struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API is the entry point for sensor.It performs
|
||||
* the selection of I2C/SPI read mechanism according to the
|
||||
* selected interface and reads the chip-id of bmi160 sensor.
|
||||
*
|
||||
* @param[in,out] dev : Structure instance of bmi160_dev
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_init(struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiRegs Registers
|
||||
* @brief Read data from the given register address of sensor
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiRegs
|
||||
* \page bmi160_api_bmi160_get_regs bmi160_get_regs
|
||||
* \code
|
||||
* int8_t bmi160_get_regs(uint8_t reg_addr, uint8_t *data, uint16_t len, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API reads the data from the given register address of sensor.
|
||||
*
|
||||
* @param[in] reg_addr : Register address from where the data to be read
|
||||
* @param[out] data : Pointer to data buffer to store the read data.
|
||||
* @param[in] len : No of bytes of data to be read.
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @note For most of the registers auto address increment applies, with the
|
||||
* exception of a few special registers, which trap the address. For e.g.,
|
||||
* Register address - 0x24(BMI160_FIFO_DATA_ADDR)
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t
|
||||
bmi160_get_regs(uint8_t reg_addr, uint8_t* data, uint16_t len, const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiRegs
|
||||
* \page bmi160_api_bmi160_set_regs bmi160_set_regs
|
||||
* \code
|
||||
* int8_t bmi160_set_regs(uint8_t reg_addr, uint8_t *data, uint16_t len, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API writes the given data to the register address
|
||||
* of sensor.
|
||||
*
|
||||
* @param[in] reg_addr : Register address from where the data to be written.
|
||||
* @param[in] data : Pointer to data buffer which is to be written
|
||||
* in the sensor.
|
||||
* @param[in] len : No of bytes of data to write..
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t
|
||||
bmi160_set_regs(uint8_t reg_addr, uint8_t* data, uint16_t len, const struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiSoftreset Soft reset
|
||||
* @brief Perform soft reset of the sensor
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiSoftreset
|
||||
* \page bmi160_api_bmi160_soft_reset bmi160_soft_reset
|
||||
* \code
|
||||
* int8_t bmi160_soft_reset(struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API resets and restarts the device.
|
||||
* All register values are overwritten with default parameters.
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_soft_reset(struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiConfig Configuration
|
||||
* @brief Configuration of the sensor
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiConfig
|
||||
* \page bmi160_api_bmi160_set_sens_conf bmi160_set_sens_conf
|
||||
* \code
|
||||
* int8_t bmi160_set_sens_conf(struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API configures the power mode, range and bandwidth
|
||||
* of sensor.
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_set_sens_conf(struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiConfig
|
||||
* \page bmi160_api_bmi160_get_sens_conf bmi160_get_sens_conf
|
||||
* \code
|
||||
* int8_t bmi160_get_sens_conf(struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API gets accel and gyro configurations.
|
||||
*
|
||||
* @param[out] dev : Structure instance of bmi160_dev.
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_get_sens_conf(struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiPowermode Power mode
|
||||
* @brief Set / Get power mode of the sensor
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiPowermode
|
||||
* \page bmi160_api_bmi160_set_power_mode bmi160_set_power_mode
|
||||
* \code
|
||||
* int8_t bmi160_set_power_mode(struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API sets the power mode of the sensor.
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_set_power_mode(struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiPowermode
|
||||
* \page bmi160_api_bmi160_get_power_mode bmi160_get_power_mode
|
||||
* \code
|
||||
* int8_t bmi160_get_power_mode(struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API gets the power mode of the sensor.
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_get_power_mode(struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiData Sensor Data
|
||||
* @brief Read sensor data
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiData
|
||||
* \page bmi160_api_bmi160_get_sensor_data bmi160_get_sensor_data
|
||||
* \code
|
||||
* int8_t bmi160_get_sensor_data(uint8_t select_sensor,
|
||||
* struct bmi160_sensor_data *accel,
|
||||
* struct bmi160_sensor_data *gyro,
|
||||
* const struct bmi160_dev *dev);
|
||||
*
|
||||
* \endcode
|
||||
* @details This API reads sensor data, stores it in
|
||||
* the bmi160_sensor_data structure pointer passed by the user.
|
||||
* The user can ask for accel data ,gyro data or both sensor
|
||||
* data using bmi160_select_sensor enum
|
||||
*
|
||||
* @param[in] select_sensor : enum to choose accel,gyro or both sensor data
|
||||
* @param[out] accel : Structure pointer to store accel data
|
||||
* @param[out] gyro : Structure pointer to store gyro data
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_get_sensor_data(
|
||||
uint8_t select_sensor,
|
||||
struct bmi160_sensor_data* accel,
|
||||
struct bmi160_sensor_data* gyro,
|
||||
const struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiInt Interrupt configuration
|
||||
* @brief Set interrupt configuration of the sensor
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiInt
|
||||
* \page bmi160_api_bmi160_set_int_config bmi160_set_int_config
|
||||
* \code
|
||||
* int8_t bmi160_set_int_config(struct bmi160_int_settg *int_config, struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API configures the necessary interrupt based on
|
||||
* the user settings in the bmi160_int_settg structure instance.
|
||||
*
|
||||
* @param[in] int_config : Structure instance of bmi160_int_settg.
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_set_int_config(struct bmi160_int_settg* int_config, struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiStepC Step counter
|
||||
* @brief Step counter operations
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiStepC
|
||||
* \page bmi160_api_bmi160_set_step_counter bmi160_set_step_counter
|
||||
* \code
|
||||
* int8_t bmi160_set_step_counter(uint8_t step_cnt_enable, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API enables the step counter feature.
|
||||
*
|
||||
* @param[in] step_cnt_enable : value to enable or disable
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_set_step_counter(uint8_t step_cnt_enable, const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiStepC
|
||||
* \page bmi160_api_bmi160_read_step_counter bmi160_read_step_counter
|
||||
* \code
|
||||
* int8_t bmi160_read_step_counter(uint16_t *step_val, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API reads the step counter value.
|
||||
*
|
||||
* @param[in] step_val : Pointer to store the step counter value.
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_read_step_counter(uint16_t* step_val, const struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiAux Auxiliary sensor
|
||||
* @brief Auxiliary sensor operations
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiAux
|
||||
* \page bmi160_api_bmi160_aux_read bmi160_aux_read
|
||||
* \code
|
||||
* int8_t bmi160_aux_read(uint8_t reg_addr, uint8_t *aux_data, uint16_t len, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API reads the mention no of byte of data from the given
|
||||
* register address of auxiliary sensor.
|
||||
*
|
||||
* @param[in] reg_addr : Address of register to read.
|
||||
* @param[in] aux_data : Pointer to store the read data.
|
||||
* @param[in] len : No of bytes to read.
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_aux_read(
|
||||
uint8_t reg_addr,
|
||||
uint8_t* aux_data,
|
||||
uint16_t len,
|
||||
const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiAux
|
||||
* \page bmi160_api_bmi160_aux_write bmi160_aux_write
|
||||
* \code
|
||||
* int8_t bmi160_aux_write(uint8_t reg_addr, uint8_t *aux_data, uint16_t len, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API writes the mention no of byte of data to the given
|
||||
* register address of auxiliary sensor.
|
||||
*
|
||||
* @param[in] reg_addr : Address of register to write.
|
||||
* @param[in] aux_data : Pointer to write data.
|
||||
* @param[in] len : No of bytes to write.
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_aux_write(
|
||||
uint8_t reg_addr,
|
||||
uint8_t* aux_data,
|
||||
uint16_t len,
|
||||
const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiAux
|
||||
* \page bmi160_api_bmi160_aux_init bmi160_aux_init
|
||||
* \code
|
||||
* int8_t bmi160_aux_init(const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API initialize the auxiliary sensor
|
||||
* in order to access it.
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
* @note : Refer user guide for detailed info.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_aux_init(const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiAux
|
||||
* \page bmi160_api_bmi160_set_aux_auto_mode bmi160_set_aux_auto_mode
|
||||
* \code
|
||||
* int8_t bmi160_set_aux_auto_mode(uint8_t *data_addr, struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API is used to setup the auxiliary sensor of bmi160 in auto mode
|
||||
* Thus enabling the auto update of 8 bytes of data from auxiliary sensor
|
||||
* to BMI160 register address 0x04 to 0x0B
|
||||
*
|
||||
* @param[in] data_addr : Starting address of aux. sensor's data register
|
||||
* (BMI160 registers 0x04 to 0x0B will be updated
|
||||
* with 8 bytes of data from auxiliary sensor
|
||||
* starting from this register address.)
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @note : Set the value of auxiliary polling rate by setting
|
||||
* dev->aux_cfg.aux_odr to the required value from the table
|
||||
* before calling this API
|
||||
*
|
||||
*@verbatim
|
||||
* dev->aux_cfg.aux_odr | Auxiliary ODR (Hz)
|
||||
* -----------------------|-----------------------
|
||||
* BMI160_AUX_ODR_0_78HZ | 25/32
|
||||
* BMI160_AUX_ODR_1_56HZ | 25/16
|
||||
* BMI160_AUX_ODR_3_12HZ | 25/8
|
||||
* BMI160_AUX_ODR_6_25HZ | 25/4
|
||||
* BMI160_AUX_ODR_12_5HZ | 25/2
|
||||
* BMI160_AUX_ODR_25HZ | 25
|
||||
* BMI160_AUX_ODR_50HZ | 50
|
||||
* BMI160_AUX_ODR_100HZ | 100
|
||||
* BMI160_AUX_ODR_200HZ | 200
|
||||
* BMI160_AUX_ODR_400HZ | 400
|
||||
* BMI160_AUX_ODR_800HZ | 800
|
||||
*@endverbatim
|
||||
*
|
||||
* @note : Other values of dev->aux_cfg.aux_odr are reserved and not for use
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_set_aux_auto_mode(uint8_t* data_addr, struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiAux
|
||||
* \page bmi160_api_bmi160_config_aux_mode bmi160_config_aux_mode
|
||||
* \code
|
||||
* int8_t bmi160_config_aux_mode(const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API configures the 0x4C register and settings like
|
||||
* Auxiliary sensor manual enable/ disable and aux burst read length.
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_config_aux_mode(const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiAux
|
||||
* \page bmi160_api_bmi160_read_aux_data_auto_mode bmi160_read_aux_data_auto_mode
|
||||
* \code
|
||||
* int8_t bmi160_read_aux_data_auto_mode(uint8_t *aux_data, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API is used to read the raw uncompensated auxiliary sensor
|
||||
* data of 8 bytes from BMI160 register address 0x04 to 0x0B
|
||||
*
|
||||
* @param[in] aux_data : Pointer to user array of length 8 bytes
|
||||
* Ensure that the aux_data array is of
|
||||
* length 8 bytes
|
||||
* @param[in] dev : Structure instance of bmi160_dev
|
||||
*
|
||||
* @retval zero -> Success / -ve value -> Error
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_read_aux_data_auto_mode(uint8_t* aux_data, const struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiSelfTest Self test
|
||||
* @brief Perform self test of the sensor
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiSelfTest
|
||||
* \page bmi160_api_bmi160_perform_self_test bmi160_perform_self_test
|
||||
* \code
|
||||
* int8_t bmi160_perform_self_test(uint8_t select_sensor, struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This is used to perform self test of accel/gyro of the BMI160 sensor
|
||||
*
|
||||
* @param[in] select_sensor : enum to choose accel or gyro for self test
|
||||
* @param[in] dev : Structure instance of bmi160_dev
|
||||
*
|
||||
* @note self test can be performed either for accel/gyro at any instant.
|
||||
*
|
||||
*@verbatim
|
||||
* value of select_sensor | Inference
|
||||
*----------------------------------|--------------------------------
|
||||
* BMI160_ACCEL_ONLY | Accel self test enabled
|
||||
* BMI160_GYRO_ONLY | Gyro self test enabled
|
||||
* BMI160_BOTH_ACCEL_AND_GYRO | NOT TO BE USED
|
||||
*@endverbatim
|
||||
*
|
||||
* @note The return value of this API gives us the result of self test.
|
||||
*
|
||||
* @note Performing self test does soft reset of the sensor, User can
|
||||
* set the desired settings after performing the self test.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval BMI160_OK Self test success
|
||||
* @retval BMI160_W_GYRO_SELF_TEST_FAIL Gyro self test fail
|
||||
* @retval BMI160_W_ACCEl_SELF_TEST_FAIL Accel self test fail
|
||||
*/
|
||||
int8_t bmi160_perform_self_test(uint8_t select_sensor, struct bmi160_dev* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiFIFO FIFO
|
||||
* @brief FIFO operations of the sensor
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiFIFO
|
||||
* \page bmi160_api_bmi160_get_fifo_data bmi160_get_fifo_data
|
||||
* \code
|
||||
* int8_t bmi160_get_fifo_data(struct bmi160_dev const *dev);
|
||||
* \endcode
|
||||
* @details This API reads data from the fifo buffer.
|
||||
*
|
||||
* @note User has to allocate the FIFO buffer along with
|
||||
* corresponding fifo length from his side before calling this API
|
||||
* as mentioned in the readme.md
|
||||
*
|
||||
* @note User must specify the number of bytes to read from the FIFO in
|
||||
* dev->fifo->length , It will be updated by the number of bytes actually
|
||||
* read from FIFO after calling this API
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval Zero Success
|
||||
* @retval Negative Error
|
||||
*/
|
||||
int8_t bmi160_get_fifo_data(struct bmi160_dev const* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiFIFO
|
||||
* \page bmi160_api_bmi160_set_fifo_flush bmi160_set_fifo_flush
|
||||
* \code
|
||||
* int8_t bmi160_set_fifo_flush(const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API writes fifo_flush command to command register.This
|
||||
* action clears all data in the Fifo without changing fifo configuration
|
||||
* settings.
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*
|
||||
*/
|
||||
int8_t bmi160_set_fifo_flush(const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiFIFO
|
||||
* \page bmi160_api_bmi160_set_fifo_config bmi160_set_fifo_config
|
||||
* \code
|
||||
* int8_t bmi160_set_fifo_config(uint8_t config, uint8_t enable, struct bmi160_dev const *dev);
|
||||
* \endcode
|
||||
* @details This API sets the FIFO configuration in the sensor.
|
||||
*
|
||||
* @param[in] config : variable used to specify the FIFO
|
||||
* configurations which are to be enabled or disabled in the sensor.
|
||||
*
|
||||
* @note : User can set either set one or more or all FIFO configurations
|
||||
* by ORing the below mentioned macros.
|
||||
*
|
||||
*@verbatim
|
||||
* config | Value
|
||||
* ------------------------|---------------------------
|
||||
* BMI160_FIFO_TIME | 0x02
|
||||
* BMI160_FIFO_TAG_INT2 | 0x04
|
||||
* BMI160_FIFO_TAG_INT1 | 0x08
|
||||
* BMI160_FIFO_HEADER | 0x10
|
||||
* BMI160_FIFO_AUX | 0x20
|
||||
* BMI160_FIFO_ACCEL | 0x40
|
||||
* BMI160_FIFO_GYRO | 0x80
|
||||
*@endverbatim
|
||||
*
|
||||
* @param[in] enable : Parameter used to enable or disable the above
|
||||
* FIFO configuration
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return status of bus communication result
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*
|
||||
*/
|
||||
int8_t bmi160_set_fifo_config(uint8_t config, uint8_t enable, struct bmi160_dev const* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiFIFO
|
||||
* \page bmi160_api_bmi160_set_fifo_down bmi160_set_fifo_down
|
||||
* \code
|
||||
* int8_t bmi160_set_fifo_down(uint8_t fifo_down, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API is used to configure the down sampling ratios of
|
||||
* the accel and gyro data for FIFO.Also, it configures filtered or
|
||||
* pre-filtered data for the fifo for accel and gyro.
|
||||
*
|
||||
* @param[in] fifo_down : variable used to specify the FIFO down
|
||||
* configurations which are to be enabled or disabled in the sensor.
|
||||
*
|
||||
* @note The user must select one among the following macros to
|
||||
* select down-sampling ratio for accel
|
||||
*
|
||||
*@verbatim
|
||||
* config | Value
|
||||
* -------------------------------------|---------------------------
|
||||
* BMI160_ACCEL_FIFO_DOWN_ZERO | 0x00
|
||||
* BMI160_ACCEL_FIFO_DOWN_ONE | 0x10
|
||||
* BMI160_ACCEL_FIFO_DOWN_TWO | 0x20
|
||||
* BMI160_ACCEL_FIFO_DOWN_THREE | 0x30
|
||||
* BMI160_ACCEL_FIFO_DOWN_FOUR | 0x40
|
||||
* BMI160_ACCEL_FIFO_DOWN_FIVE | 0x50
|
||||
* BMI160_ACCEL_FIFO_DOWN_SIX | 0x60
|
||||
* BMI160_ACCEL_FIFO_DOWN_SEVEN | 0x70
|
||||
*@endverbatim
|
||||
*
|
||||
* @note The user must select one among the following macros to
|
||||
* select down-sampling ratio for gyro
|
||||
*
|
||||
*@verbatim
|
||||
* config | Value
|
||||
* -------------------------------------|---------------------------
|
||||
* BMI160_GYRO_FIFO_DOWN_ZERO | 0x00
|
||||
* BMI160_GYRO_FIFO_DOWN_ONE | 0x01
|
||||
* BMI160_GYRO_FIFO_DOWN_TWO | 0x02
|
||||
* BMI160_GYRO_FIFO_DOWN_THREE | 0x03
|
||||
* BMI160_GYRO_FIFO_DOWN_FOUR | 0x04
|
||||
* BMI160_GYRO_FIFO_DOWN_FIVE | 0x05
|
||||
* BMI160_GYRO_FIFO_DOWN_SIX | 0x06
|
||||
* BMI160_GYRO_FIFO_DOWN_SEVEN | 0x07
|
||||
*@endverbatim
|
||||
*
|
||||
* @note The user can enable filtered accel data by the following macro
|
||||
*
|
||||
*@verbatim
|
||||
* config | Value
|
||||
* -------------------------------------|---------------------------
|
||||
* BMI160_ACCEL_FIFO_FILT_EN | 0x80
|
||||
*@endverbatim
|
||||
*
|
||||
* @note The user can enable filtered gyro data by the following macro
|
||||
*
|
||||
*@verbatim
|
||||
* config | Value
|
||||
* -------------------------------------|---------------------------
|
||||
* BMI160_GYRO_FIFO_FILT_EN | 0x08
|
||||
*@endverbatim
|
||||
*
|
||||
* @note : By ORing the above mentioned macros, the user can select
|
||||
* the required FIFO down config settings
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return status of bus communication result
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*
|
||||
*/
|
||||
int8_t bmi160_set_fifo_down(uint8_t fifo_down, const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiFIFO
|
||||
* \page bmi160_api_bmi160_set_fifo_wm bmi160_set_fifo_wm
|
||||
* \code
|
||||
* int8_t bmi160_set_fifo_wm(uint8_t fifo_wm, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API sets the FIFO watermark level in the sensor.
|
||||
*
|
||||
* @note The FIFO watermark is issued when the FIFO fill level is
|
||||
* equal or above the watermark level and units of watermark is 4 bytes.
|
||||
*
|
||||
* @param[in] fifo_wm : Variable used to set the FIFO water mark level
|
||||
* @param[in] dev : Structure instance of bmi160_dev
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*
|
||||
*/
|
||||
int8_t bmi160_set_fifo_wm(uint8_t fifo_wm, const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiFIFO
|
||||
* \page bmi160_api_bmi160_extract_accel bmi160_extract_accel
|
||||
* \code
|
||||
* int8_t bmi160_extract_accel(struct bmi160_sensor_data *accel_data, uint8_t *accel_length, struct bmi160_dev const
|
||||
**dev);
|
||||
* \endcode
|
||||
* @details This API parses and extracts the accelerometer frames from
|
||||
* FIFO data read by the "bmi160_get_fifo_data" API and stores it in
|
||||
* the "accel_data" structure instance.
|
||||
*
|
||||
* @note The bmi160_extract_accel API should be called only after
|
||||
* reading the FIFO data by calling the bmi160_get_fifo_data() API.
|
||||
*
|
||||
* @param[out] accel_data : Structure instance of bmi160_sensor_data
|
||||
* where the accelerometer data in FIFO is stored.
|
||||
* @param[in,out] accel_length : Number of valid accelerometer frames
|
||||
* (x,y,z axes data) read out from fifo.
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @note accel_length is updated with the number of valid accelerometer
|
||||
* frames extracted from fifo (1 accel frame = 6 bytes) at the end of
|
||||
* execution of this API.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*
|
||||
*/
|
||||
int8_t bmi160_extract_accel(
|
||||
struct bmi160_sensor_data* accel_data,
|
||||
uint8_t* accel_length,
|
||||
struct bmi160_dev const* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiFIFO
|
||||
* \page bmi160_api_bmi160_extract_gyro bmi160_extract_gyro
|
||||
* \code
|
||||
* int8_t bmi160_extract_gyro(struct bmi160_sensor_data *gyro_data, uint8_t *gyro_length, struct bmi160_dev const *dev);
|
||||
* \endcode
|
||||
* @details This API parses and extracts the gyro frames from
|
||||
* FIFO data read by the "bmi160_get_fifo_data" API and stores it in
|
||||
* the "gyro_data" structure instance.
|
||||
*
|
||||
* @note The bmi160_extract_gyro API should be called only after
|
||||
* reading the FIFO data by calling the bmi160_get_fifo_data() API.
|
||||
*
|
||||
* @param[out] gyro_data : Structure instance of bmi160_sensor_data
|
||||
* where the gyro data in FIFO is stored.
|
||||
* @param[in,out] gyro_length : Number of valid gyro frames
|
||||
* (x,y,z axes data) read out from fifo.
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @note gyro_length is updated with the number of valid gyro
|
||||
* frames extracted from fifo (1 gyro frame = 6 bytes) at the end of
|
||||
* execution of this API.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*
|
||||
*/
|
||||
int8_t bmi160_extract_gyro(
|
||||
struct bmi160_sensor_data* gyro_data,
|
||||
uint8_t* gyro_length,
|
||||
struct bmi160_dev const* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiFIFO
|
||||
* \page bmi160_api_bmi160_extract_aux bmi160_extract_aux
|
||||
* \code
|
||||
* int8_t bmi160_extract_aux(struct bmi160_aux_data *aux_data, uint8_t *aux_len, struct bmi160_dev const *dev);
|
||||
* \endcode
|
||||
* @details This API parses and extracts the aux frames from
|
||||
* FIFO data read by the "bmi160_get_fifo_data" API and stores it in
|
||||
* the bmi160_aux_data structure instance.
|
||||
*
|
||||
* @note The bmi160_extract_aux API should be called only after
|
||||
* reading the FIFO data by calling the bmi160_get_fifo_data() API.
|
||||
*
|
||||
* @param[out] aux_data : Structure instance of bmi160_aux_data
|
||||
* where the aux data in FIFO is stored.
|
||||
* @param[in,out] aux_len : Number of valid aux frames (8bytes)
|
||||
* read out from FIFO.
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @note aux_len is updated with the number of valid aux
|
||||
* frames extracted from fifo (1 aux frame = 8 bytes) at the end of
|
||||
* execution of this API.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*
|
||||
*/
|
||||
int8_t bmi160_extract_aux(
|
||||
struct bmi160_aux_data* aux_data,
|
||||
uint8_t* aux_len,
|
||||
struct bmi160_dev const* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiFOC FOC
|
||||
* @brief Start FOC of accel and gyro sensors
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiFOC
|
||||
* \page bmi160_api_bmi160_start_foc bmi160_start_foc
|
||||
* \code
|
||||
* int8_t bmi160_start_foc(const struct bmi160_foc_conf *foc_conf,
|
||||
* \endcode
|
||||
* @details This API starts the FOC of accel and gyro
|
||||
*
|
||||
* @note FOC should not be used in low-power mode of sensor
|
||||
*
|
||||
* @note Accel FOC targets values of +1g , 0g , -1g
|
||||
* Gyro FOC always targets value of 0 dps
|
||||
*
|
||||
* @param[in] foc_conf : Structure instance of bmi160_foc_conf which
|
||||
* has the FOC configuration
|
||||
* @param[in,out] offset : Structure instance to store Offset
|
||||
* values read from sensor
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @note Pre-requisites for triggering FOC in accel , Set the following,
|
||||
* Enable the acc_off_en
|
||||
* Ex : foc_conf.acc_off_en = BMI160_ENABLE;
|
||||
*
|
||||
* Set the desired target values of FOC to each axes (x,y,z) by using the
|
||||
* following macros
|
||||
* - BMI160_FOC_ACCEL_DISABLED
|
||||
* - BMI160_FOC_ACCEL_POSITIVE_G
|
||||
* - BMI160_FOC_ACCEL_NEGATIVE_G
|
||||
* - BMI160_FOC_ACCEL_0G
|
||||
*
|
||||
* Ex : foc_conf.foc_acc_x = BMI160_FOC_ACCEL_0G;
|
||||
* foc_conf.foc_acc_y = BMI160_FOC_ACCEL_0G;
|
||||
* foc_conf.foc_acc_z = BMI160_FOC_ACCEL_POSITIVE_G;
|
||||
*
|
||||
* @note Pre-requisites for triggering FOC in gyro ,
|
||||
* Set the following parameters,
|
||||
*
|
||||
* Ex : foc_conf.foc_gyr_en = BMI160_ENABLE;
|
||||
* foc_conf.gyro_off_en = BMI160_ENABLE;
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*/
|
||||
int8_t bmi160_start_foc(
|
||||
const struct bmi160_foc_conf* foc_conf,
|
||||
struct bmi160_offsets* offset,
|
||||
struct bmi160_dev const* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiOffsets Offsets
|
||||
* @brief Set / Get offset values of accel and gyro sensors
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiOffsets
|
||||
* \page bmi160_api_bmi160_get_offsets bmi160_get_offsets
|
||||
* \code
|
||||
* int8_t bmi160_get_offsets(struct bmi160_offsets *offset, const struct bmi160_dev *dev);
|
||||
* \endcode
|
||||
* @details This API reads and stores the offset values of accel and gyro
|
||||
*
|
||||
* @param[in,out] offset : Structure instance of bmi160_offsets in which
|
||||
* the offset values are read and stored
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*/
|
||||
int8_t bmi160_get_offsets(struct bmi160_offsets* offset, const struct bmi160_dev* dev);
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiOffsets
|
||||
* \page bmi160_api_bmi160_set_offsets bmi160_set_offsets
|
||||
* \code
|
||||
* int8_t bmi160_set_offsets(const struct bmi160_foc_conf *foc_conf,
|
||||
* const struct bmi160_offsets *offset,
|
||||
* struct bmi160_dev const *dev);
|
||||
* \endcode
|
||||
* @details This API writes the offset values of accel and gyro to
|
||||
* the sensor but these values will be reset on POR or soft reset.
|
||||
*
|
||||
* @param[in] foc_conf : Structure instance of bmi160_foc_conf which
|
||||
* has the FOC configuration
|
||||
* @param[in] offset : Structure instance in which user updates offset
|
||||
* values which are to be written in the sensor
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @note Offsets can be set by user like offset->off_acc_x = 10;
|
||||
* where 1LSB = 3.9mg and for gyro 1LSB = 0.061degrees/second
|
||||
*
|
||||
* @note BMI160 offset values for xyz axes of accel should be within range of
|
||||
* BMI160_ACCEL_MIN_OFFSET (-128) to BMI160_ACCEL_MAX_OFFSET (127)
|
||||
*
|
||||
* @note BMI160 offset values for xyz axes of gyro should be within range of
|
||||
* BMI160_GYRO_MIN_OFFSET (-512) to BMI160_GYRO_MAX_OFFSET (511)
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*/
|
||||
int8_t bmi160_set_offsets(
|
||||
const struct bmi160_foc_conf* foc_conf,
|
||||
const struct bmi160_offsets* offset,
|
||||
struct bmi160_dev const* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiNVM NVM
|
||||
* @brief Write image registers values to NVM
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiNVM
|
||||
* \page bmi160_api_bmi160_update_nvm bmi160_update_nvm
|
||||
* \code
|
||||
* int8_t bmi160_update_nvm(struct bmi160_dev const *dev);
|
||||
* \endcode
|
||||
* @details This API writes the image registers values to NVM which is
|
||||
* stored even after POR or soft reset
|
||||
*
|
||||
* @param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*/
|
||||
int8_t bmi160_update_nvm(struct bmi160_dev const* dev);
|
||||
|
||||
/**
|
||||
* \ingroup bmi160
|
||||
* \defgroup bmi160ApiInts Interrupt status
|
||||
* @brief Read interrupt status from the sensor
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \ingroup bmi160ApiInts
|
||||
* \page bmi160_api_bmi160_get_int_status bmi160_get_int_status
|
||||
* \code
|
||||
* int8_t bmi160_get_int_status(enum bmi160_int_status_sel int_status_sel,
|
||||
* union bmi160_int_status *int_status,
|
||||
* struct bmi160_dev const *dev);
|
||||
* \endcode
|
||||
* @details This API gets the interrupt status from the sensor.
|
||||
*
|
||||
* @param[in] int_status_sel : Enum variable to select either individual or all the
|
||||
* interrupt status bits.
|
||||
* @param[in] int_status : pointer variable to get the interrupt status
|
||||
* from the sensor.
|
||||
* param[in] dev : Structure instance of bmi160_dev.
|
||||
*
|
||||
* @return Result of API execution status
|
||||
* @retval 0 -> Success
|
||||
* @retval Any non zero value -> Fail
|
||||
*/
|
||||
int8_t bmi160_get_int_status(
|
||||
enum bmi160_int_status_sel int_status_sel,
|
||||
union bmi160_int_status* int_status,
|
||||
struct bmi160_dev const* dev);
|
||||
|
||||
/*************************** C++ guard macro *****************************/
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BMI160_H_ */
|
||||
@@ -1,29 +0,0 @@
|
||||
#include "imu.h"
|
||||
#include <furi_hal.h>
|
||||
|
||||
bool bmi160_begin();
|
||||
int bmi160_read(double* vec);
|
||||
|
||||
bool lsm6ds3trc_begin();
|
||||
void lsm6ds3trc_end();
|
||||
int lsm6ds3trc_read(double* vec);
|
||||
|
||||
bool imu_begin() {
|
||||
furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
|
||||
bool ret = bmi160_begin(); // lsm6ds3trc_begin();
|
||||
furi_hal_i2c_release(&furi_hal_i2c_handle_external);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void imu_end() {
|
||||
// furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
|
||||
// lsm6ds3trc_end();
|
||||
// furi_hal_i2c_release(&furi_hal_i2c_handle_external);
|
||||
}
|
||||
|
||||
int imu_read(double* vec) {
|
||||
furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
|
||||
int ret = bmi160_read(vec); // lsm6ds3trc_read(vec);
|
||||
furi_hal_i2c_release(&furi_hal_i2c_handle_external);
|
||||
return ret;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ACC_DATA_READY (1 << 0)
|
||||
#define GYR_DATA_READY (1 << 1)
|
||||
|
||||
bool imu_begin();
|
||||
void imu_end();
|
||||
int imu_read(double* vec);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,88 +0,0 @@
|
||||
#include "bmi160.h"
|
||||
|
||||
#include <furi_hal.h>
|
||||
|
||||
#include "imu.h"
|
||||
|
||||
#define TAG "BMI160"
|
||||
|
||||
#define BMI160_DEV_ADDR (0x69 << 1)
|
||||
|
||||
static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
|
||||
static const double G = 9.81;
|
||||
|
||||
struct bmi160_dev bmi160dev;
|
||||
struct bmi160_sensor_data bmi160_accel;
|
||||
struct bmi160_sensor_data bmi160_gyro;
|
||||
|
||||
int8_t bmi160_write_i2c(uint8_t dev_addr, uint8_t reg_addr, uint8_t* data, uint16_t len) {
|
||||
if(furi_hal_i2c_write_mem(&furi_hal_i2c_handle_external, dev_addr, reg_addr, data, len, 50))
|
||||
return BMI160_OK;
|
||||
return BMI160_E_COM_FAIL;
|
||||
}
|
||||
|
||||
int8_t bmi160_read_i2c(uint8_t dev_addr, uint8_t reg_addr, uint8_t* read_data, uint16_t len) {
|
||||
if(furi_hal_i2c_read_mem(&furi_hal_i2c_handle_external, dev_addr, reg_addr, read_data, len, 50))
|
||||
return BMI160_OK;
|
||||
return BMI160_E_COM_FAIL;
|
||||
}
|
||||
|
||||
bool bmi160_begin() {
|
||||
FURI_LOG_I(TAG, "Init BMI160");
|
||||
|
||||
if(!furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, BMI160_DEV_ADDR, 50)) {
|
||||
FURI_LOG_E(TAG, "Device not ready!");
|
||||
return false;
|
||||
}
|
||||
|
||||
FURI_LOG_I(TAG, "Device ready!");
|
||||
|
||||
bmi160dev.id = BMI160_DEV_ADDR;
|
||||
bmi160dev.intf = BMI160_I2C_INTF;
|
||||
bmi160dev.read = bmi160_read_i2c;
|
||||
bmi160dev.write = bmi160_write_i2c;
|
||||
bmi160dev.delay_ms = furi_delay_ms;
|
||||
|
||||
if(bmi160_init(&bmi160dev) != BMI160_OK) {
|
||||
FURI_LOG_E(TAG, "Initialization failure!");
|
||||
FURI_LOG_E(TAG, "Chip ID 0x%X", bmi160dev.chip_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
bmi160dev.accel_cfg.odr = BMI160_ACCEL_ODR_400HZ;
|
||||
bmi160dev.accel_cfg.range = BMI160_ACCEL_RANGE_4G;
|
||||
bmi160dev.accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4;
|
||||
bmi160dev.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
|
||||
bmi160dev.gyro_cfg.odr = BMI160_GYRO_ODR_400HZ;
|
||||
bmi160dev.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
|
||||
bmi160dev.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;
|
||||
bmi160dev.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
|
||||
|
||||
if(bmi160_set_sens_conf(&bmi160dev) != BMI160_OK) {
|
||||
FURI_LOG_E(TAG, "Initialization failure!");
|
||||
FURI_LOG_E(TAG, "Chip ID 0x%X", bmi160dev.chip_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
FURI_LOG_I(TAG, "Initialization success!");
|
||||
FURI_LOG_I(TAG, "Chip ID 0x%X", bmi160dev.chip_id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int bmi160_read(double* vec) {
|
||||
if(bmi160_get_sensor_data(
|
||||
(BMI160_ACCEL_SEL | BMI160_GYRO_SEL), &bmi160_accel, &bmi160_gyro, &bmi160dev) !=
|
||||
BMI160_OK) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
vec[0] = ((double)bmi160_accel.x * 4 / 32768) * G;
|
||||
vec[1] = ((double)bmi160_accel.y * 4 / 32768) * G;
|
||||
vec[2] = ((double)bmi160_accel.z * 4 / 32768) * G;
|
||||
vec[3] = ((double)bmi160_gyro.x * 2000 / 32768) * DEG_TO_RAD;
|
||||
vec[4] = ((double)bmi160_gyro.y * 2000 / 32768) * DEG_TO_RAD;
|
||||
vec[5] = ((double)bmi160_gyro.z * 2000 / 32768) * DEG_TO_RAD;
|
||||
|
||||
return ACC_DATA_READY | GYR_DATA_READY;
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
#include "lsm6ds3tr_c_reg.h"
|
||||
|
||||
#include <furi_hal.h>
|
||||
|
||||
#include "imu.h"
|
||||
|
||||
#define TAG "LSM6DS3TR-C"
|
||||
|
||||
#define LSM6DS3_ADDRESS (0x6A << 1)
|
||||
|
||||
static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
|
||||
|
||||
stmdev_ctx_t lsm6ds3trc_ctx;
|
||||
|
||||
int32_t lsm6ds3trc_write_i2c(void* handle, uint8_t reg_addr, const uint8_t* data, uint16_t len) {
|
||||
if(furi_hal_i2c_write_mem(handle, LSM6DS3_ADDRESS, reg_addr, (uint8_t*)data, len, 50))
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int32_t lsm6ds3trc_read_i2c(void* handle, uint8_t reg_addr, uint8_t* read_data, uint16_t len) {
|
||||
if(furi_hal_i2c_read_mem(handle, LSM6DS3_ADDRESS, reg_addr, read_data, len, 50)) return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool lsm6ds3trc_begin() {
|
||||
FURI_LOG_I(TAG, "Init LSM6DS3TR-C");
|
||||
|
||||
if(!furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, LSM6DS3_ADDRESS, 50)) {
|
||||
FURI_LOG_E(TAG, "Not ready");
|
||||
return false;
|
||||
}
|
||||
|
||||
lsm6ds3trc_ctx.write_reg = lsm6ds3trc_write_i2c;
|
||||
lsm6ds3trc_ctx.read_reg = lsm6ds3trc_read_i2c;
|
||||
lsm6ds3trc_ctx.mdelay = furi_delay_ms;
|
||||
lsm6ds3trc_ctx.handle = &furi_hal_i2c_handle_external;
|
||||
|
||||
uint8_t whoami;
|
||||
lsm6ds3tr_c_device_id_get(&lsm6ds3trc_ctx, &whoami);
|
||||
if(whoami != LSM6DS3TR_C_ID) {
|
||||
FURI_LOG_I(TAG, "Unknown model: %x", (int)whoami);
|
||||
return false;
|
||||
}
|
||||
|
||||
lsm6ds3tr_c_reset_set(&lsm6ds3trc_ctx, PROPERTY_ENABLE);
|
||||
uint8_t rst = PROPERTY_ENABLE;
|
||||
while(rst) lsm6ds3tr_c_reset_get(&lsm6ds3trc_ctx, &rst);
|
||||
|
||||
lsm6ds3tr_c_block_data_update_set(&lsm6ds3trc_ctx, PROPERTY_ENABLE);
|
||||
lsm6ds3tr_c_fifo_mode_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_BYPASS_MODE);
|
||||
|
||||
lsm6ds3tr_c_xl_data_rate_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_XL_ODR_104Hz);
|
||||
lsm6ds3tr_c_xl_full_scale_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_4g);
|
||||
lsm6ds3tr_c_xl_lp1_bandwidth_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_XL_LP1_ODR_DIV_4);
|
||||
|
||||
lsm6ds3tr_c_gy_data_rate_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_GY_ODR_104Hz);
|
||||
lsm6ds3tr_c_gy_full_scale_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_2000dps);
|
||||
lsm6ds3tr_c_gy_power_mode_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_GY_HIGH_PERFORMANCE);
|
||||
lsm6ds3tr_c_gy_band_pass_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_LP2_ONLY);
|
||||
|
||||
FURI_LOG_I(TAG, "Init OK");
|
||||
return true;
|
||||
}
|
||||
|
||||
void lsm6ds3trc_end() {
|
||||
lsm6ds3tr_c_xl_data_rate_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_XL_ODR_OFF);
|
||||
lsm6ds3tr_c_gy_data_rate_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_GY_ODR_OFF);
|
||||
}
|
||||
|
||||
int lsm6ds3trc_read(double* vec) {
|
||||
int ret = 0;
|
||||
int16_t data[3];
|
||||
lsm6ds3tr_c_reg_t reg;
|
||||
lsm6ds3tr_c_status_reg_get(&lsm6ds3trc_ctx, ®.status_reg);
|
||||
|
||||
if(reg.status_reg.xlda) {
|
||||
lsm6ds3tr_c_acceleration_raw_get(&lsm6ds3trc_ctx, data);
|
||||
vec[2] = (double)lsm6ds3tr_c_from_fs2g_to_mg(data[0]) / 1000;
|
||||
vec[0] = (double)lsm6ds3tr_c_from_fs2g_to_mg(data[1]) / 1000;
|
||||
vec[1] = (double)lsm6ds3tr_c_from_fs2g_to_mg(data[2]) / 1000;
|
||||
ret |= ACC_DATA_READY;
|
||||
}
|
||||
|
||||
if(reg.status_reg.gda) {
|
||||
lsm6ds3tr_c_angular_rate_raw_get(&lsm6ds3trc_ctx, data);
|
||||
vec[5] = (double)lsm6ds3tr_c_from_fs2000dps_to_mdps(data[0]) * DEG_TO_RAD / 1000;
|
||||
vec[3] = (double)lsm6ds3tr_c_from_fs2000dps_to_mdps(data[1]) * DEG_TO_RAD / 1000;
|
||||
vec[4] = (double)lsm6ds3tr_c_from_fs2000dps_to_mdps(data[2]) * DEG_TO_RAD / 1000;
|
||||
ret |= GYR_DATA_READY;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
#include "main_loop.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
#include "imu/imu.h"
|
||||
#include "orientation_tracker.h"
|
||||
#include "calibration_data.h"
|
||||
|
||||
#define TAG "tracker"
|
||||
|
||||
static const float CURSOR_SPEED = 1024.0 / (M_PI / 4);
|
||||
static const float STABILIZE_BIAS = 16.0;
|
||||
|
||||
float g_yaw = 0;
|
||||
float g_pitch = 0;
|
||||
float g_dYaw = 0;
|
||||
float g_dPitch = 0;
|
||||
bool firstRead = true;
|
||||
bool stabilize = true;
|
||||
CalibrationData calibration;
|
||||
cardboard::OrientationTracker tracker(10000000l); // 10 ms / 100 Hz
|
||||
uint64_t ippms, ippms2;
|
||||
|
||||
static inline float clamp(float val)
|
||||
{
|
||||
while (val <= -M_PI) {
|
||||
val += 2 * M_PI;
|
||||
}
|
||||
while (val >= M_PI) {
|
||||
val -= 2 * M_PI;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline float highpass(float oldVal, float newVal)
|
||||
{
|
||||
if (!stabilize) {
|
||||
return newVal;
|
||||
}
|
||||
float delta = clamp(oldVal - newVal);
|
||||
float alpha = (float) std::max(0.0, 1 - std::pow(std::fabs(delta) * CURSOR_SPEED / STABILIZE_BIAS, 3.0));
|
||||
return newVal + alpha * delta;
|
||||
}
|
||||
|
||||
void sendCurrentState(MouseMoveCallback mouse_move)
|
||||
{
|
||||
float dX = g_dYaw * CURSOR_SPEED;
|
||||
float dY = g_dPitch * CURSOR_SPEED;
|
||||
|
||||
// Scale the shift down to fit the protocol.
|
||||
if (dX > 127) {
|
||||
dY *= 127.0 / dX;
|
||||
dX = 127;
|
||||
}
|
||||
if (dX < -127) {
|
||||
dY *= -127.0 / dX;
|
||||
dX = -127;
|
||||
}
|
||||
if (dY > 127) {
|
||||
dX *= 127.0 / dY;
|
||||
dY = 127;
|
||||
}
|
||||
if (dY < -127) {
|
||||
dX *= -127.0 / dY;
|
||||
dY = -127;
|
||||
}
|
||||
|
||||
const int8_t x = (int8_t)std::floor(dX + 0.5);
|
||||
const int8_t y = (int8_t)std::floor(dY + 0.5);
|
||||
|
||||
mouse_move(x, y);
|
||||
|
||||
// Only subtract the part of the error that was already sent.
|
||||
if (x != 0) {
|
||||
g_dYaw -= x / CURSOR_SPEED;
|
||||
}
|
||||
if (y != 0) {
|
||||
g_dPitch -= y / CURSOR_SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
void onOrientation(cardboard::Vector4& quaternion)
|
||||
{
|
||||
float q1 = quaternion[0]; // X * sin(T/2)
|
||||
float q2 = quaternion[1]; // Y * sin(T/2)
|
||||
float q3 = quaternion[2]; // Z * sin(T/2)
|
||||
float q0 = quaternion[3]; // cos(T/2)
|
||||
|
||||
float yaw = std::atan2(2 * (q0 * q3 - q1 * q2), (1 - 2 * (q1 * q1 + q3 * q3)));
|
||||
float pitch = std::asin(2 * (q0 * q1 + q2 * q3));
|
||||
// float roll = std::atan2(2 * (q0 * q2 - q1 * q3), (1 - 2 * (q1 * q1 + q2 * q2)));
|
||||
|
||||
if (yaw == NAN || pitch == NAN) {
|
||||
// NaN case, skip it
|
||||
return;
|
||||
}
|
||||
|
||||
if (firstRead) {
|
||||
g_yaw = yaw;
|
||||
g_pitch = pitch;
|
||||
firstRead = false;
|
||||
} else {
|
||||
const float newYaw = highpass(g_yaw, yaw);
|
||||
const float newPitch = highpass(g_pitch, pitch);
|
||||
|
||||
float dYaw = clamp(g_yaw - newYaw);
|
||||
float dPitch = g_pitch - newPitch;
|
||||
g_yaw = newYaw;
|
||||
g_pitch = newPitch;
|
||||
|
||||
// Accumulate the error locally.
|
||||
g_dYaw += dYaw;
|
||||
g_dPitch += dPitch;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
void calibration_begin() {
|
||||
calibration.reset();
|
||||
FURI_LOG_I(TAG, "Calibrating");
|
||||
}
|
||||
|
||||
bool calibration_step() {
|
||||
if (calibration.isComplete())
|
||||
return true;
|
||||
|
||||
double vec[6];
|
||||
if (imu_read(vec) & GYR_DATA_READY) {
|
||||
cardboard::Vector3 data(vec[3], vec[4], vec[5]);
|
||||
furi_delay_ms(9); // Artificially limit to ~100Hz
|
||||
return calibration.add(data);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void calibration_end() {
|
||||
CalibrationMedian store;
|
||||
cardboard::Vector3 median = calibration.getMedian();
|
||||
store.x = median[0];
|
||||
store.y = median[1];
|
||||
store.z = median[2];
|
||||
CALIBRATION_DATA_SAVE(&store);
|
||||
}
|
||||
|
||||
void tracking_begin() {
|
||||
CalibrationMedian store;
|
||||
cardboard::Vector3 median = calibration.getMedian();
|
||||
if (CALIBRATION_DATA_LOAD(&store)) {
|
||||
median[0] = store.x;
|
||||
median[1] = store.y;
|
||||
median[2] = store.z;
|
||||
}
|
||||
|
||||
ippms = furi_hal_cortex_instructions_per_microsecond();
|
||||
ippms2 = ippms / 2;
|
||||
tracker.SetCalibration(median);
|
||||
tracker.Resume();
|
||||
}
|
||||
|
||||
void tracking_step(MouseMoveCallback mouse_move) {
|
||||
double vec[6];
|
||||
int ret = imu_read(vec);
|
||||
if (ret != 0) {
|
||||
uint64_t t = (DWT->CYCCNT * 1000llu + ippms2) / ippms;
|
||||
if (ret & ACC_DATA_READY) {
|
||||
cardboard::AccelerometerData adata
|
||||
= { .system_timestamp = t, .sensor_timestamp_ns = t,
|
||||
.data = cardboard::Vector3(vec[0], vec[1], vec[2]) };
|
||||
tracker.OnAccelerometerData(adata);
|
||||
}
|
||||
if (ret & GYR_DATA_READY) {
|
||||
cardboard::GyroscopeData gdata
|
||||
= { .system_timestamp = t, .sensor_timestamp_ns = t,
|
||||
.data = cardboard::Vector3(vec[3], vec[4], vec[5]) };
|
||||
cardboard::Vector4 pose = tracker.OnGyroscopeData(gdata);
|
||||
onOrientation(pose);
|
||||
sendCurrentState(mouse_move);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tracking_end() {
|
||||
tracker.Pause();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef bool (*MouseMoveCallback)(int8_t x, int8_t y);
|
||||
|
||||
void calibration_begin();
|
||||
bool calibration_step();
|
||||
void calibration_end();
|
||||
|
||||
void tracking_begin();
|
||||
void tracking_step(MouseMoveCallback mouse_move);
|
||||
void tracking_end();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "orientation_tracker.h"
|
||||
|
||||
#include "sensors/pose_prediction.h"
|
||||
#include "util/logging.h"
|
||||
#include "util/vector.h"
|
||||
#include "util/vectorutils.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
OrientationTracker::OrientationTracker(const long sampling_period_ns)
|
||||
: sampling_period_ns_(sampling_period_ns)
|
||||
, calibration_(Vector3::Zero())
|
||||
, is_tracking_(false)
|
||||
, sensor_fusion_(new SensorFusionEkf())
|
||||
, latest_gyroscope_data_({ 0, 0, Vector3::Zero() })
|
||||
{
|
||||
sensor_fusion_->SetBiasEstimationEnabled(/*kGyroBiasEstimationEnabled*/ true);
|
||||
}
|
||||
|
||||
void OrientationTracker::SetCalibration(const Vector3& calibration) {
|
||||
calibration_ = calibration;
|
||||
}
|
||||
|
||||
void OrientationTracker::Pause()
|
||||
{
|
||||
if (!is_tracking_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a gyro event with zero velocity. This effectively stops the prediction.
|
||||
GyroscopeData event = latest_gyroscope_data_;
|
||||
event.data = Vector3::Zero();
|
||||
|
||||
OnGyroscopeData(event);
|
||||
|
||||
is_tracking_ = false;
|
||||
}
|
||||
|
||||
void OrientationTracker::Resume() { is_tracking_ = true; }
|
||||
|
||||
Vector4 OrientationTracker::GetPose(int64_t timestamp_ns) const
|
||||
{
|
||||
Rotation predicted_rotation;
|
||||
const PoseState pose_state = sensor_fusion_->GetLatestPoseState();
|
||||
if (sensor_fusion_->IsFullyInitialized()) {
|
||||
predicted_rotation = pose_state.sensor_from_start_rotation;
|
||||
} else {
|
||||
CARDBOARD_LOGI("Tracker not fully initialized yet. Using pose prediction only.");
|
||||
predicted_rotation = pose_prediction::PredictPose(timestamp_ns, pose_state);
|
||||
}
|
||||
|
||||
return (-predicted_rotation).GetQuaternion();
|
||||
}
|
||||
|
||||
void OrientationTracker::OnAccelerometerData(const AccelerometerData& event)
|
||||
{
|
||||
if (!is_tracking_) {
|
||||
return;
|
||||
}
|
||||
sensor_fusion_->ProcessAccelerometerSample(event);
|
||||
}
|
||||
|
||||
Vector4 OrientationTracker::OnGyroscopeData(const GyroscopeData& event)
|
||||
{
|
||||
if (!is_tracking_) {
|
||||
return Vector4();
|
||||
}
|
||||
|
||||
const GyroscopeData data = { .system_timestamp = event.system_timestamp,
|
||||
.sensor_timestamp_ns = event.sensor_timestamp_ns,
|
||||
.data = event.data - calibration_ };
|
||||
|
||||
latest_gyroscope_data_ = data;
|
||||
|
||||
sensor_fusion_->ProcessGyroscopeSample(data);
|
||||
|
||||
return OrientationTracker::GetPose(data.sensor_timestamp_ns + sampling_period_ns_);
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <mutex> // NOLINT
|
||||
|
||||
#include "sensors/accelerometer_data.h"
|
||||
#include "sensors/gyroscope_data.h"
|
||||
#include "sensors/sensor_fusion_ekf.h"
|
||||
#include "util/rotation.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// OrientationTracker encapsulates pose tracking by connecting sensors
|
||||
// to SensorFusion.
|
||||
// This pose tracker reports poses in display space.
|
||||
class OrientationTracker {
|
||||
public:
|
||||
OrientationTracker(const long sampling_period_ns);
|
||||
|
||||
void SetCalibration(const Vector3& calibration);
|
||||
|
||||
// Pauses tracking and sensors.
|
||||
void Pause();
|
||||
|
||||
// Resumes tracking ans sensors.
|
||||
void Resume();
|
||||
|
||||
// Gets the predicted pose for a given timestamp.
|
||||
Vector4 GetPose(int64_t timestamp_ns) const;
|
||||
|
||||
// Function called when receiving AccelerometerData.
|
||||
//
|
||||
// @param event sensor event.
|
||||
void OnAccelerometerData(const AccelerometerData& event);
|
||||
|
||||
// Function called when receiving GyroscopeData.
|
||||
//
|
||||
// @param event sensor event.
|
||||
Vector4 OnGyroscopeData(const GyroscopeData& event);
|
||||
|
||||
private:
|
||||
long sampling_period_ns_;
|
||||
Vector3 calibration_;
|
||||
|
||||
std::atomic<bool> is_tracking_;
|
||||
// Sensor Fusion object that stores the internal state of the filter.
|
||||
std::unique_ptr<SensorFusionEkf> sensor_fusion_;
|
||||
// Latest gyroscope data.
|
||||
GyroscopeData latest_gyroscope_data_;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_SENSORS_ACCELEROMETER_DATA_H_
|
||||
#define CARDBOARD_SDK_SENSORS_ACCELEROMETER_DATA_H_
|
||||
|
||||
#include "../util/vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
struct AccelerometerData {
|
||||
// System wall time.
|
||||
uint64_t system_timestamp;
|
||||
|
||||
// Sensor clock time in nanoseconds.
|
||||
uint64_t sensor_timestamp_ns;
|
||||
|
||||
// Acceleration force along the x,y,z axes in m/s^2. This follows android
|
||||
// specification
|
||||
// (https://developer.android.com/guide/topics/sensors/sensors_overview.html#sensors-coords).
|
||||
Vector3 data;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_SENSORS_ACCELEROMETER_DATA_H_
|
||||
@@ -1,313 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "gyroscope_bias_estimator.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono> // NOLINT
|
||||
|
||||
#include "../util/rotation.h"
|
||||
#include "../util/vector.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// Cutoff frequencies in Hertz applied to our various signals, and their
|
||||
// corresponding filters.
|
||||
const float kAccelerometerLowPassCutOffFrequencyHz = 1.0f;
|
||||
const float kRotationVelocityBasedAccelerometerLowPassCutOffFrequencyHz = 0.15f;
|
||||
const float kGyroscopeLowPassCutOffFrequencyHz = 1.0f;
|
||||
const float kGyroscopeBiasLowPassCutOffFrequencyHz = 0.15f;
|
||||
|
||||
// Note that MEMS IMU are not that precise.
|
||||
const double kEpsilon = 1.0e-8;
|
||||
|
||||
// Size of the filtering window for the mean and median filter. The larger the
|
||||
// windows the larger the filter delay.
|
||||
const int kFilterWindowSize = 5;
|
||||
|
||||
// Threshold used to compare rotation computed from the accelerometer and the
|
||||
// gyroscope bias.
|
||||
const double kRatioBetweenGyroBiasAndAccel = 1.5;
|
||||
|
||||
// The minimum sum of weights we need to acquire before returning a bias
|
||||
// estimation.
|
||||
const float kMinSumOfWeightsGyroBiasThreshold = 25.0f;
|
||||
|
||||
// Amount of change in m/s^3 we allow on the smoothed accelerometer values to
|
||||
// consider the phone static.
|
||||
const double kAccelerometerDeltaStaticThreshold = 0.5;
|
||||
|
||||
// Amount of change in radians/s^2 we allow on the smoothed gyroscope values to
|
||||
// consider the phone static.
|
||||
const double kGyroscopeDeltaStaticThreshold = 0.03;
|
||||
|
||||
// If the gyroscope value is above this threshold, don't update the gyroscope
|
||||
// bias estimation. This threshold is applied to the magnitude of gyroscope
|
||||
// vectors in radians/s.
|
||||
const float kGyroscopeForBiasThreshold = 0.30f;
|
||||
|
||||
// Used to monitor if accelerometer and gyroscope have been static for a few
|
||||
// frames.
|
||||
const int kStaticFrameDetectionThreshold = 50;
|
||||
|
||||
// Minimum time step between sensor updates.
|
||||
const double kMinTimestep = 1; // std::chrono::nanoseconds(1);
|
||||
} // namespace
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// A helper class to keep track of whether some signal can be considered static
|
||||
// over specified number of frames.
|
||||
class GyroscopeBiasEstimator::IsStaticCounter {
|
||||
public:
|
||||
// Initializes a counter with the number of consecutive frames we require
|
||||
// the signal to be static before IsRecentlyStatic returns true.
|
||||
//
|
||||
// @param min_static_frames_threshold number of consecutive frames we
|
||||
// require the signal to be static before IsRecentlyStatic returns true.
|
||||
explicit IsStaticCounter(int min_static_frames_threshold)
|
||||
: min_static_frames_threshold_(min_static_frames_threshold)
|
||||
, consecutive_static_frames_(0)
|
||||
{
|
||||
}
|
||||
|
||||
// Specifies whether the current frame is considered static.
|
||||
//
|
||||
// @param is_static static flag for current frame.
|
||||
void AppendFrame(bool is_static)
|
||||
{
|
||||
if (is_static) {
|
||||
++consecutive_static_frames_;
|
||||
} else {
|
||||
consecutive_static_frames_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns if static movement is assumed.
|
||||
bool IsRecentlyStatic() const
|
||||
{
|
||||
return consecutive_static_frames_ >= min_static_frames_threshold_;
|
||||
}
|
||||
// Resets counter.
|
||||
void Reset() { consecutive_static_frames_ = 0; }
|
||||
|
||||
private:
|
||||
const int min_static_frames_threshold_;
|
||||
int consecutive_static_frames_;
|
||||
};
|
||||
|
||||
GyroscopeBiasEstimator::GyroscopeBiasEstimator()
|
||||
: accelerometer_lowpass_filter_(kAccelerometerLowPassCutOffFrequencyHz)
|
||||
, simulated_gyroscope_from_accelerometer_lowpass_filter_(
|
||||
kRotationVelocityBasedAccelerometerLowPassCutOffFrequencyHz)
|
||||
, gyroscope_lowpass_filter_(kGyroscopeLowPassCutOffFrequencyHz)
|
||||
, gyroscope_bias_lowpass_filter_(kGyroscopeBiasLowPassCutOffFrequencyHz)
|
||||
, accelerometer_static_counter_(new IsStaticCounter(kStaticFrameDetectionThreshold))
|
||||
, gyroscope_static_counter_(new IsStaticCounter(kStaticFrameDetectionThreshold))
|
||||
, current_accumulated_weights_gyroscope_bias_(0.f)
|
||||
, mean_filter_(kFilterWindowSize)
|
||||
, median_filter_(kFilterWindowSize)
|
||||
, last_mean_filtered_accelerometer_value_({ 0, 0, 0 })
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
GyroscopeBiasEstimator::~GyroscopeBiasEstimator() { }
|
||||
|
||||
void GyroscopeBiasEstimator::Reset()
|
||||
{
|
||||
accelerometer_lowpass_filter_.Reset();
|
||||
gyroscope_lowpass_filter_.Reset();
|
||||
gyroscope_bias_lowpass_filter_.Reset();
|
||||
accelerometer_static_counter_->Reset();
|
||||
gyroscope_static_counter_->Reset();
|
||||
}
|
||||
|
||||
void GyroscopeBiasEstimator::ProcessGyroscope(
|
||||
const Vector3& gyroscope_sample, uint64_t timestamp_ns)
|
||||
{
|
||||
// Update gyroscope and gyroscope delta low-pass filters.
|
||||
gyroscope_lowpass_filter_.AddSample(gyroscope_sample, timestamp_ns);
|
||||
|
||||
const auto smoothed_gyroscope_delta
|
||||
= gyroscope_sample - gyroscope_lowpass_filter_.GetFilteredData();
|
||||
|
||||
gyroscope_static_counter_->AppendFrame(
|
||||
Length(smoothed_gyroscope_delta) < kGyroscopeDeltaStaticThreshold);
|
||||
|
||||
// Only update the bias if the gyroscope and accelerometer signals have been
|
||||
// relatively static recently.
|
||||
if (gyroscope_static_counter_->IsRecentlyStatic()
|
||||
&& accelerometer_static_counter_->IsRecentlyStatic()) {
|
||||
// Reset static counter when updating the bias fails.
|
||||
if (!UpdateGyroscopeBias(gyroscope_sample, timestamp_ns)) {
|
||||
// Bias update fails because of large motion, thus reset the static
|
||||
// counter.
|
||||
gyroscope_static_counter_->AppendFrame(false);
|
||||
}
|
||||
} else {
|
||||
// Reset weights, if not static.
|
||||
current_accumulated_weights_gyroscope_bias_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void GyroscopeBiasEstimator::ProcessAccelerometer(
|
||||
const Vector3& accelerometer_sample, uint64_t timestamp_ns)
|
||||
{
|
||||
// Get current state of the filter.
|
||||
const uint64_t previous_accel_timestamp_ns
|
||||
= accelerometer_lowpass_filter_.GetMostRecentTimestampNs();
|
||||
const bool is_low_pass_filter_init = accelerometer_lowpass_filter_.IsInitialized();
|
||||
|
||||
// Update accel and accel delta low-pass filters.
|
||||
accelerometer_lowpass_filter_.AddSample(accelerometer_sample, timestamp_ns);
|
||||
|
||||
const auto smoothed_accelerometer_delta
|
||||
= accelerometer_sample - accelerometer_lowpass_filter_.GetFilteredData();
|
||||
|
||||
accelerometer_static_counter_->AppendFrame(
|
||||
Length(smoothed_accelerometer_delta) < kAccelerometerDeltaStaticThreshold);
|
||||
|
||||
// Rotation from accel cannot be differentiated with only one sample.
|
||||
if (!is_low_pass_filter_init) {
|
||||
simulated_gyroscope_from_accelerometer_lowpass_filter_.AddSample({ 0, 0, 0 }, timestamp_ns);
|
||||
return;
|
||||
}
|
||||
|
||||
// No need to update the simulated gyroscope at this point because the motion
|
||||
// is too large.
|
||||
if (!accelerometer_static_counter_->IsRecentlyStatic()) {
|
||||
return;
|
||||
}
|
||||
|
||||
median_filter_.AddSample(accelerometer_lowpass_filter_.GetFilteredData());
|
||||
|
||||
// This processing can only be started if the buffer is fully initialized.
|
||||
if (!median_filter_.IsValid()) {
|
||||
mean_filter_.AddSample(accelerometer_lowpass_filter_.GetFilteredData());
|
||||
|
||||
// Update the last filtered accelerometer value.
|
||||
last_mean_filtered_accelerometer_value_ = accelerometer_lowpass_filter_.GetFilteredData();
|
||||
return;
|
||||
}
|
||||
|
||||
mean_filter_.AddSample(median_filter_.GetFilteredData());
|
||||
|
||||
// Compute a mock gyroscope value from accelerometer.
|
||||
const int64_t diff = timestamp_ns - previous_accel_timestamp_ns;
|
||||
const double timestep = static_cast<double>(diff);
|
||||
|
||||
simulated_gyroscope_from_accelerometer_lowpass_filter_.AddSample(
|
||||
ComputeAngularVelocityFromLatestAccelerometer(timestep), timestamp_ns);
|
||||
last_mean_filtered_accelerometer_value_ = mean_filter_.GetFilteredData();
|
||||
}
|
||||
|
||||
Vector3 GyroscopeBiasEstimator::ComputeAngularVelocityFromLatestAccelerometer(double timestep) const
|
||||
{
|
||||
if (timestep < kMinTimestep) {
|
||||
return { 0, 0, 0 };
|
||||
}
|
||||
|
||||
const auto mean_of_median = mean_filter_.GetFilteredData();
|
||||
|
||||
// Compute an incremental rotation between the last state and the current
|
||||
// state.
|
||||
//
|
||||
// Note that we switch to double precision here because of precision problem
|
||||
// with small rotation.
|
||||
const auto incremental_rotation = Rotation::RotateInto(
|
||||
Vector3(last_mean_filtered_accelerometer_value_[0],
|
||||
last_mean_filtered_accelerometer_value_[1], last_mean_filtered_accelerometer_value_[2]),
|
||||
Vector3(mean_of_median[0], mean_of_median[1], mean_of_median[2]));
|
||||
|
||||
// We use axis angle here because this is how gyroscope values are stored.
|
||||
Vector3 incremental_rotation_axis;
|
||||
double incremental_rotation_angle;
|
||||
incremental_rotation.GetAxisAndAngle(&incremental_rotation_axis, &incremental_rotation_angle);
|
||||
|
||||
incremental_rotation_axis *= incremental_rotation_angle / timestep;
|
||||
|
||||
return { static_cast<float>(incremental_rotation_axis[0]),
|
||||
static_cast<float>(incremental_rotation_axis[1]),
|
||||
static_cast<float>(incremental_rotation_axis[2]) };
|
||||
}
|
||||
|
||||
bool GyroscopeBiasEstimator::UpdateGyroscopeBias(
|
||||
const Vector3& gyroscope_sample, uint64_t timestamp_ns)
|
||||
{
|
||||
// Gyroscope values that are too big are potentially dangerous as they could
|
||||
// originate from slow and steady head rotations.
|
||||
//
|
||||
// Therefore we compute an update weight which:
|
||||
// * favors gyroscope values that are closer to 0
|
||||
// * is set to zero if gyroscope values are greater than a threshold.
|
||||
//
|
||||
// This way, the gyroscope bias estimation converges faster if the phone is
|
||||
// flat on a table, as opposed to held up somewhat stationary in the user's
|
||||
// hands.
|
||||
|
||||
// If magnitude is too big, don't update the filter at all so that we don't
|
||||
// artificially increase the number of samples accumulated by the filter.
|
||||
const float gyroscope_sample_norm2 = Length(gyroscope_sample);
|
||||
if (gyroscope_sample_norm2 >= kGyroscopeForBiasThreshold) {
|
||||
return false;
|
||||
}
|
||||
|
||||
float update_weight
|
||||
= std::max(0.0f, 1 - gyroscope_sample_norm2 / kGyroscopeForBiasThreshold);
|
||||
update_weight *= update_weight;
|
||||
gyroscope_bias_lowpass_filter_.AddWeightedSample(
|
||||
gyroscope_lowpass_filter_.GetFilteredData(), timestamp_ns, update_weight);
|
||||
|
||||
// This counter is only partially valid as the low pass filter drops large
|
||||
// samples.
|
||||
current_accumulated_weights_gyroscope_bias_ += update_weight;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Vector3 GyroscopeBiasEstimator::GetGyroscopeBias() const
|
||||
{
|
||||
return gyroscope_bias_lowpass_filter_.GetFilteredData();
|
||||
}
|
||||
|
||||
bool GyroscopeBiasEstimator::IsCurrentEstimateValid() const
|
||||
{
|
||||
// Remove any bias component along the gravity because they cannot be
|
||||
// evaluated from accelerometer.
|
||||
const auto current_gravity_dir = Normalized(last_mean_filtered_accelerometer_value_);
|
||||
const auto gyro_bias_lowpass = gyroscope_bias_lowpass_filter_.GetFilteredData();
|
||||
|
||||
const auto off_gravity_gyro_bias
|
||||
= gyro_bias_lowpass - current_gravity_dir * Dot(gyro_bias_lowpass, current_gravity_dir);
|
||||
|
||||
// Checks that the current bias estimate is not correlated with the
|
||||
// rotation computed from accelerometer.
|
||||
const auto gyro_from_accel
|
||||
= simulated_gyroscope_from_accelerometer_lowpass_filter_.GetFilteredData();
|
||||
const bool isGyroscopeBiasCorrelatedWithSimulatedGyro
|
||||
= (Length(gyro_from_accel) * kRatioBetweenGyroBiasAndAccel
|
||||
> (Length(off_gravity_gyro_bias) + kEpsilon));
|
||||
const bool hasEnoughSamples
|
||||
= current_accumulated_weights_gyroscope_bias_ > kMinSumOfWeightsGyroBiasThreshold;
|
||||
const bool areCountersStatic = gyroscope_static_counter_->IsRecentlyStatic()
|
||||
&& accelerometer_static_counter_->IsRecentlyStatic();
|
||||
|
||||
const bool isStatic
|
||||
= hasEnoughSamples && areCountersStatic && !isGyroscopeBiasCorrelatedWithSimulatedGyro;
|
||||
return isStatic;
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,134 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_SENSORS_GYROSCOPE_BIAS_ESTIMATOR_H_
|
||||
#define CARDBOARD_SDK_SENSORS_GYROSCOPE_BIAS_ESTIMATOR_H_
|
||||
|
||||
#include <chrono> // NOLINT
|
||||
#include <cstdint>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "lowpass_filter.h"
|
||||
#include "mean_filter.h"
|
||||
#include "median_filter.h"
|
||||
#include "../util/vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// Class that attempts to estimate the gyroscope's bias.
|
||||
// Its main idea is that it averages the gyroscope values when the phone is
|
||||
// considered stationary.
|
||||
// Usage: A client should call the ProcessGyroscope and ProcessAccelerometer
|
||||
// methods for every accelerometer and gyroscope sensor sample. This class
|
||||
// expects these calls to be frequent, i.e., at least at 10 Hz. The client can
|
||||
// then call GetGyroBias to retrieve the current estimate of the gyroscope bias.
|
||||
// For best results, the fastest available delay option should be used when
|
||||
// registering to sensors. Note that this class is not thread-safe.
|
||||
//
|
||||
// The filtering applied to the accelerometer to estimate a rotation
|
||||
// from it follows :
|
||||
// Baptiste Delporte, Laurent Perroton, Thierry Grandpierre, Jacques Trichet.
|
||||
// Accelerometer and Magnetometer Based Gyroscope Emulation on Smart Sensor
|
||||
// for a Virtual Reality Application. Sensor and Transducers Journal, 2012.
|
||||
//
|
||||
// which is a combination of a IIR filter, a median and a mean filter.
|
||||
class GyroscopeBiasEstimator {
|
||||
public:
|
||||
GyroscopeBiasEstimator();
|
||||
virtual ~GyroscopeBiasEstimator();
|
||||
|
||||
// Updates the estimator with a gyroscope event.
|
||||
//
|
||||
// @param gyroscope_sample the angular speed around the x, y, z axis in
|
||||
// radians/sec.
|
||||
// @param timestamp_ns the nanosecond at which the event occurred. Only
|
||||
// guaranteed to be comparable with timestamps from other PocessGyroscope
|
||||
// invocations.
|
||||
virtual void ProcessGyroscope(const Vector3& gyroscope_sample, uint64_t timestamp_ns);
|
||||
|
||||
// Processes accelerometer samples to estimate if device is
|
||||
// stable or not.
|
||||
//
|
||||
// First we filter the accelerometer. This is done with 3 filters.
|
||||
// - A IIR low-pass filter
|
||||
// - A median filter
|
||||
// - A mean filter.
|
||||
// Then a rotation is computed between consecutive filtered accelerometer
|
||||
// samples.
|
||||
// Finally this is converted to a velocity to emulate a gyroscope.
|
||||
//
|
||||
// @param accelerometer_sample the acceleration (including gravity) on the x,
|
||||
// y, z axis in meters/s^2.
|
||||
// @param timestamp_ns the nanosecond at which the event occurred. Only
|
||||
// guaranteed to be comparable with timestamps from other
|
||||
// ProcessAccelerometer invocations.
|
||||
virtual void ProcessAccelerometer(const Vector3& accelerometer_sample, uint64_t timestamp_ns);
|
||||
|
||||
// Returns the estimated gyroscope bias.
|
||||
//
|
||||
// @return Estimated gyroscope bias. A vector with zeros is returned if no
|
||||
// estimate has been computed.
|
||||
virtual Vector3 GetGyroscopeBias() const;
|
||||
|
||||
// Resets the estimator state.
|
||||
void Reset();
|
||||
|
||||
// Returns true if the current estimate returned by GetGyroscopeBias is
|
||||
// correct. The device (measured using the sensors) has to be static for this
|
||||
// function to return true.
|
||||
virtual bool IsCurrentEstimateValid() const;
|
||||
|
||||
private:
|
||||
// A helper class to keep track of whether some signal can be considered
|
||||
// static over specified number of frames.
|
||||
class IsStaticCounter;
|
||||
|
||||
// Updates gyroscope bias estimation.
|
||||
//
|
||||
// @return false if the current sample is too large.
|
||||
bool UpdateGyroscopeBias(const Vector3& gyroscope_sample, uint64_t timestamp_ns);
|
||||
|
||||
// Returns device angular velocity (rad/s) from the latest accelerometer data.
|
||||
//
|
||||
// @param timestep in seconds between the last two samples.
|
||||
// @return rotation velocity from latest accelerometer. This can be
|
||||
// interpreted as an gyroscope.
|
||||
Vector3 ComputeAngularVelocityFromLatestAccelerometer(double timestep) const;
|
||||
|
||||
LowpassFilter accelerometer_lowpass_filter_;
|
||||
LowpassFilter simulated_gyroscope_from_accelerometer_lowpass_filter_;
|
||||
LowpassFilter gyroscope_lowpass_filter_;
|
||||
LowpassFilter gyroscope_bias_lowpass_filter_;
|
||||
|
||||
std::unique_ptr<IsStaticCounter> accelerometer_static_counter_;
|
||||
std::unique_ptr<IsStaticCounter> gyroscope_static_counter_;
|
||||
|
||||
// Sum of the weight of sample used for gyroscope filtering.
|
||||
float current_accumulated_weights_gyroscope_bias_;
|
||||
|
||||
// Set of filters for accelerometer data to estimate a rotation
|
||||
// based only on accelerometer.
|
||||
MeanFilter mean_filter_;
|
||||
MedianFilter median_filter_;
|
||||
|
||||
// Last computed filter accelerometer value used for finite differences.
|
||||
Vector3 last_mean_filtered_accelerometer_value_;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_SENSORS_GYROSCOPE_BIAS_ESTIMATOR_H_
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_SENSORS_GYROSCOPE_DATA_H_
|
||||
#define CARDBOARD_SDK_SENSORS_GYROSCOPE_DATA_H_
|
||||
|
||||
#include "../util/vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
struct GyroscopeData {
|
||||
// System wall time.
|
||||
uint64_t system_timestamp;
|
||||
|
||||
// Sensor clock time in nanoseconds.
|
||||
uint64_t sensor_timestamp_ns;
|
||||
|
||||
// Rate of rotation around the x,y,z axes in rad/s. This follows android
|
||||
// specification
|
||||
// (https://developer.android.com/guide/topics/sensors/sensors_overview.html#sensors-coords).
|
||||
Vector3 data;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_SENSORS_GYROSCOPE_DATA_H_
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "lowpass_filter.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace {
|
||||
|
||||
const double kSecondsFromNanoseconds = 1.0e-9;
|
||||
|
||||
// Minimum time step between sensor updates. This corresponds to 1000 Hz.
|
||||
const double kMinTimestepS = 0.001f;
|
||||
|
||||
// Maximum time step between sensor updates. This corresponds to 1 Hz.
|
||||
const double kMaxTimestepS = 1.00f;
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
LowpassFilter::LowpassFilter(double cutoff_freq_hz)
|
||||
: cutoff_time_constant_(1 / (2 * (double)M_PI * cutoff_freq_hz))
|
||||
, initialized_(false)
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
void LowpassFilter::AddSample(const Vector3& sample, uint64_t timestamp_ns)
|
||||
{
|
||||
AddWeightedSample(sample, timestamp_ns, 1.0);
|
||||
}
|
||||
|
||||
void LowpassFilter::AddWeightedSample(const Vector3& sample, uint64_t timestamp_ns, double weight)
|
||||
{
|
||||
if (!initialized_) {
|
||||
// Initialize filter state
|
||||
filtered_data_ = { sample[0], sample[1], sample[2] };
|
||||
timestamp_most_recent_update_ns_ = timestamp_ns;
|
||||
initialized_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (timestamp_ns < timestamp_most_recent_update_ns_) {
|
||||
timestamp_most_recent_update_ns_ = timestamp_ns;
|
||||
return;
|
||||
}
|
||||
|
||||
const double delta_s = static_cast<double>(timestamp_ns - timestamp_most_recent_update_ns_)
|
||||
* kSecondsFromNanoseconds;
|
||||
if (delta_s <= kMinTimestepS || delta_s > kMaxTimestepS) {
|
||||
timestamp_most_recent_update_ns_ = timestamp_ns;
|
||||
return;
|
||||
}
|
||||
|
||||
const double weighted_delta_secs = weight * delta_s;
|
||||
|
||||
const double alpha = weighted_delta_secs / (cutoff_time_constant_ + weighted_delta_secs);
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
filtered_data_[i] = (1 - alpha) * filtered_data_[i] + alpha * sample[i];
|
||||
}
|
||||
timestamp_most_recent_update_ns_ = timestamp_ns;
|
||||
}
|
||||
|
||||
void LowpassFilter::Reset()
|
||||
{
|
||||
initialized_ = false;
|
||||
filtered_data_ = { 0, 0, 0 };
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_SENSORS_LOWPASS_FILTER_H_
|
||||
#define CARDBOARD_SDK_SENSORS_LOWPASS_FILTER_H_
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
#include "../util/vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// Implements an IIR, first order, low pass filter over vectors of the given
|
||||
// dimension = 3.
|
||||
// See http://en.wikipedia.org/wiki/Low-pass_filter
|
||||
class LowpassFilter {
|
||||
public:
|
||||
// Initializes a filter with the given cutoff frequency in Hz.
|
||||
explicit LowpassFilter(double cutoff_freq_hz);
|
||||
|
||||
// Updates the filter with the given sample. Note that samples with
|
||||
// non-monotonic timestamps and successive samples with a time steps below 1
|
||||
// ms or above 1 s are ignored.
|
||||
//
|
||||
// @param sample current sample data.
|
||||
// @param timestamp_ns timestamp associated to this sample in nanoseconds.
|
||||
void AddSample(const Vector3& sample, uint64_t timestamp_ns);
|
||||
|
||||
// Updates the filter with the given weighted sample.
|
||||
//
|
||||
// @param sample current sample data.
|
||||
// @param timestamp_ns timestamp associated to this sample in nanoseconds.
|
||||
// @param weight typically a [0, 1] weight factor used when applying a new
|
||||
// sample. A weight of 1 corresponds to calling AddSample. A weight of 0
|
||||
// makes the update no-op. The first initial sample is not affected by
|
||||
// this.
|
||||
void AddWeightedSample(const Vector3& sample, uint64_t timestamp_ns, double weight);
|
||||
|
||||
// Returns the filtered value. A vector with zeros is returned if no samples
|
||||
// have been added.
|
||||
Vector3 GetFilteredData() const {
|
||||
return filtered_data_;
|
||||
}
|
||||
|
||||
// Returns the most recent update timestamp in ns.
|
||||
uint64_t GetMostRecentTimestampNs() const {
|
||||
return timestamp_most_recent_update_ns_;
|
||||
}
|
||||
|
||||
// Returns true when the filter is initialized.
|
||||
bool IsInitialized() const {
|
||||
return initialized_;
|
||||
}
|
||||
|
||||
// Resets filter state.
|
||||
void Reset();
|
||||
|
||||
private:
|
||||
const double cutoff_time_constant_;
|
||||
uint64_t timestamp_most_recent_update_ns_;
|
||||
bool initialized_;
|
||||
|
||||
Vector3 filtered_data_;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_SENSORS_LOWPASS_FILTER_H_
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "mean_filter.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
MeanFilter::MeanFilter(size_t filter_size)
|
||||
: filter_size_(filter_size)
|
||||
{
|
||||
}
|
||||
|
||||
void MeanFilter::AddSample(const Vector3& sample)
|
||||
{
|
||||
buffer_.push_back(sample);
|
||||
if (buffer_.size() > filter_size_) {
|
||||
buffer_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
bool MeanFilter::IsValid() const { return buffer_.size() == filter_size_; }
|
||||
|
||||
Vector3 MeanFilter::GetFilteredData() const
|
||||
{
|
||||
// Compute mean of the samples stored in buffer_.
|
||||
Vector3 mean = Vector3::Zero();
|
||||
for (auto sample : buffer_) {
|
||||
mean += sample;
|
||||
}
|
||||
|
||||
return mean / static_cast<double>(filter_size_);
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_SENSORS_MEAN_FILTER_H_
|
||||
#define CARDBOARD_SDK_SENSORS_MEAN_FILTER_H_
|
||||
|
||||
#include <deque>
|
||||
|
||||
#include "../util/vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// Fixed window FIFO mean filter for vectors of the given dimension.
|
||||
class MeanFilter {
|
||||
public:
|
||||
// Create a mean filter of size filter_size.
|
||||
// @param filter_size size of the internal filter.
|
||||
explicit MeanFilter(size_t filter_size);
|
||||
|
||||
// Add sample to buffer_ if buffer_ is full it drop the oldest sample.
|
||||
void AddSample(const Vector3& sample);
|
||||
|
||||
// Returns true if buffer has filter_size_ sample, false otherwise.
|
||||
bool IsValid() const;
|
||||
|
||||
// Returns the mean of values stored in the internal buffer.
|
||||
Vector3 GetFilteredData() const;
|
||||
|
||||
private:
|
||||
const size_t filter_size_;
|
||||
std::deque<Vector3> buffer_;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_SENSORS_MEAN_FILTER_H_
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "median_filter.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include "../util/vector.h"
|
||||
#include "../util/vectorutils.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
MedianFilter::MedianFilter(size_t filter_size)
|
||||
: filter_size_(filter_size)
|
||||
{
|
||||
}
|
||||
|
||||
void MedianFilter::AddSample(const Vector3& sample)
|
||||
{
|
||||
buffer_.push_back(sample);
|
||||
norms_.push_back(Length(sample));
|
||||
if (buffer_.size() > filter_size_) {
|
||||
buffer_.pop_front();
|
||||
norms_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
bool MedianFilter::IsValid() const { return buffer_.size() == filter_size_; }
|
||||
|
||||
Vector3 MedianFilter::GetFilteredData() const
|
||||
{
|
||||
std::vector<float> norms(norms_.begin(), norms_.end());
|
||||
|
||||
// Get median of value of the norms.
|
||||
std::nth_element(norms.begin(), norms.begin() + filter_size_ / 2, norms.end());
|
||||
const float median_norm = norms[filter_size_ / 2];
|
||||
|
||||
// Get median value based on their norm.
|
||||
auto median_it = buffer_.begin();
|
||||
for (const auto norm : norms_) {
|
||||
if (norm == median_norm) {
|
||||
break;
|
||||
}
|
||||
++median_it;
|
||||
}
|
||||
|
||||
return *median_it;
|
||||
}
|
||||
|
||||
void MedianFilter::Reset()
|
||||
{
|
||||
buffer_.clear();
|
||||
norms_.clear();
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_SENSORS_MEDIAN_FILTER_H_
|
||||
#define CARDBOARD_SDK_SENSORS_MEDIAN_FILTER_H_
|
||||
|
||||
#include <deque>
|
||||
|
||||
#include "../util/vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// Fixed window FIFO median filter for vectors of the given dimension = 3.
|
||||
class MedianFilter {
|
||||
public:
|
||||
// Creates a median filter of size filter_size.
|
||||
// @param filter_size size of the internal filter.
|
||||
explicit MedianFilter(size_t filter_size);
|
||||
|
||||
// Adds sample to buffer_ if buffer_ is full it drops the oldest sample.
|
||||
void AddSample(const Vector3& sample);
|
||||
|
||||
// Returns true if buffer has filter_size_ sample, false otherwise.
|
||||
bool IsValid() const;
|
||||
|
||||
// Returns the median of values store in the internal buffer.
|
||||
Vector3 GetFilteredData() const;
|
||||
|
||||
// Resets the filter, removing all samples that have been added.
|
||||
void Reset();
|
||||
|
||||
private:
|
||||
const size_t filter_size_;
|
||||
std::deque<Vector3> buffer_;
|
||||
// Contains norms of the elements stored in buffer_.
|
||||
std::deque<float> norms_;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_SENSORS_MEDIAN_FILTER_H_
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "pose_prediction.h"
|
||||
|
||||
#include <chrono> // NOLINT
|
||||
|
||||
#include "../util/logging.h"
|
||||
#include "../util/vectorutils.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
namespace {
|
||||
const double kEpsilon = 1.0e-15;
|
||||
} // namespace
|
||||
|
||||
namespace pose_prediction {
|
||||
|
||||
Rotation GetRotationFromGyroscope(const Vector3& gyroscope_value, double timestep_s)
|
||||
{
|
||||
const double velocity = Length(gyroscope_value);
|
||||
|
||||
// When there is no rotation data return an identity rotation.
|
||||
if (velocity < kEpsilon) {
|
||||
CARDBOARD_LOGI("PosePrediction::GetRotationFromGyroscope: Velocity really small, "
|
||||
"returning identity rotation.");
|
||||
return Rotation::Identity();
|
||||
}
|
||||
// Since the gyroscope_value is a start from sensor transformation we need to
|
||||
// invert it to have a sensor from start transformation, hence the minus sign.
|
||||
// For more info:
|
||||
// http://developer.android.com/guide/topics/sensors/sensors_motion.html#sensors-motion-gyro
|
||||
return Rotation::FromAxisAndAngle(gyroscope_value / velocity, -timestep_s * velocity);
|
||||
}
|
||||
|
||||
Rotation PredictPose(int64_t requested_pose_timestamp, const PoseState& current_state)
|
||||
{
|
||||
// Subtracting unsigned numbers is bad when the result is negative.
|
||||
const int64_t diff = requested_pose_timestamp - current_state.timestamp;
|
||||
const double timestep_s = diff * 1.0e-9;
|
||||
|
||||
const Rotation update = GetRotationFromGyroscope(
|
||||
current_state.sensor_from_start_rotation_velocity, timestep_s);
|
||||
return update * current_state.sensor_from_start_rotation;
|
||||
}
|
||||
|
||||
Rotation PredictPoseInv(int64_t requested_pose_timestamp, const PoseState& current_state)
|
||||
{
|
||||
// Subtracting unsigned numbers is bad when the result is negative.
|
||||
const int64_t diff = requested_pose_timestamp - current_state.timestamp;
|
||||
const double timestep_s = diff * 1.0e-9;
|
||||
|
||||
const Rotation update = GetRotationFromGyroscope(
|
||||
current_state.sensor_from_start_rotation_velocity, timestep_s);
|
||||
return current_state.sensor_from_start_rotation * (-update);
|
||||
}
|
||||
|
||||
} // namespace pose_prediction
|
||||
} // namespace cardboard
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_SENSORS_POSE_PREDICTION_H_
|
||||
#define CARDBOARD_SDK_SENSORS_POSE_PREDICTION_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "pose_state.h"
|
||||
#include "../util/rotation.h"
|
||||
|
||||
namespace cardboard {
|
||||
namespace pose_prediction {
|
||||
|
||||
// Returns a rotation matrix based on the integration of the gyroscope_value
|
||||
// over the timestep_s in seconds.
|
||||
// TODO(pfg): Document the space better here.
|
||||
//
|
||||
// @param gyroscope_value gyroscope sensor values.
|
||||
// @param timestep_s integration period in seconds.
|
||||
// @return Integration of the gyroscope value the rotation is from Start to
|
||||
// Sensor Space.
|
||||
Rotation GetRotationFromGyroscope(const Vector3& gyroscope_value, double timestep_s);
|
||||
|
||||
// Gets a predicted pose for a given time in the future (e.g. rendering time)
|
||||
// based on a linear prediction model. This uses the system current state
|
||||
// (position, velocity, etc) from the past to extrapolate a position in the
|
||||
// future.
|
||||
//
|
||||
// @param requested_pose_timestamp time at which you want the pose.
|
||||
// @param current_state current state that stores the pose and linear model at a
|
||||
// given time prior to requested_pose_timestamp_ns.
|
||||
// @return pose from Start to Sensor Space.
|
||||
Rotation PredictPose(int64_t requested_pose_timestamp, const PoseState& current_state);
|
||||
|
||||
// Equivalent to PredictPose, but for use with poses relative to Start Space
|
||||
// rather than sensor space.
|
||||
Rotation PredictPoseInv(int64_t requested_pose_timestamp, const PoseState& current_state);
|
||||
|
||||
} // namespace pose_prediction
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_SENSORS_POSE_PREDICTION_H_
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_SENSORS_POSE_STATE_H_
|
||||
#define CARDBOARD_SDK_SENSORS_POSE_STATE_H_
|
||||
|
||||
#include "../util/rotation.h"
|
||||
#include "../util/vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
enum {
|
||||
kPoseStateFlagInvalid = 1U << 0,
|
||||
kPoseStateFlagInitializing = 1U << 1,
|
||||
kPoseStateFlagHas6DoF = 1U << 2,
|
||||
};
|
||||
|
||||
// Stores a head pose pose plus derivatives. This can be used for prediction.
|
||||
struct PoseState {
|
||||
// System wall time.
|
||||
int64_t timestamp;
|
||||
|
||||
// Rotation from Sensor Space to Start Space.
|
||||
Rotation sensor_from_start_rotation;
|
||||
|
||||
// First derivative of the rotation.
|
||||
Vector3 sensor_from_start_rotation_velocity;
|
||||
|
||||
// Current gyroscope bias in rad/s.
|
||||
Vector3 bias;
|
||||
|
||||
// The position of the headset.
|
||||
Vector3 position = Vector3(0, 0, 0);
|
||||
|
||||
// In the same coordinate frame as the position.
|
||||
Vector3 velocity = Vector3(0, 0, 0);
|
||||
|
||||
// Flags indicating the status of the pose.
|
||||
uint64_t flags = 0U;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_SENSORS_POSE_STATE_H_
|
||||
@@ -1,333 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "sensor_fusion_ekf.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "accelerometer_data.h"
|
||||
#include "gyroscope_data.h"
|
||||
#include "pose_prediction.h"
|
||||
#include "../util/matrixutils.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
namespace {
|
||||
|
||||
const double kFiniteDifferencingEpsilon = 1.0e-7;
|
||||
const double kEpsilon = 1.0e-15;
|
||||
// Default gyroscope frequency. This corresponds to 100 Hz.
|
||||
const double kDefaultGyroscopeTimestep_s = 0.01f;
|
||||
// Maximum time between gyroscope before we start limiting the integration.
|
||||
const double kMaximumGyroscopeSampleDelay_s = 0.04f;
|
||||
// Compute a first-order exponential moving average of changes in accel norm per
|
||||
// frame.
|
||||
const double kSmoothingFactor = 0.5;
|
||||
// Minimum and maximum values used for accelerometer noise covariance matrix.
|
||||
// The smaller the sigma value, the more weight is given to the accelerometer
|
||||
// signal.
|
||||
const double kMinAccelNoiseSigma = 0.75;
|
||||
const double kMaxAccelNoiseSigma = 7.0;
|
||||
// Initial value for the diagonal elements of the different covariance matrices.
|
||||
const double kInitialStateCovarianceValue = 25.0;
|
||||
const double kInitialProcessCovarianceValue = 1.0;
|
||||
// Maximum accelerometer norm change allowed before capping it covariance to a
|
||||
// large value.
|
||||
const double kMaxAccelNormChange = 0.15;
|
||||
// Timestep IIR filtering coefficient.
|
||||
const double kTimestepFilterCoeff = 0.95;
|
||||
// Minimum number of sample for timestep filtering.
|
||||
const int kTimestepFilterMinSamples = 10;
|
||||
|
||||
// Z direction in start space.
|
||||
const Vector3 kCanonicalZDirection(0.0, 0.0, 1.0);
|
||||
|
||||
// Computes an axis-angle rotation from the input vector.
|
||||
// angle = norm(a)
|
||||
// axis = a.normalized()
|
||||
// If norm(a) == 0, it returns an identity rotation.
|
||||
static inline Rotation RotationFromVector(const Vector3& a)
|
||||
{
|
||||
const double norm_a = Length(a);
|
||||
if (norm_a < kEpsilon) {
|
||||
return Rotation::Identity();
|
||||
}
|
||||
return Rotation::FromAxisAndAngle(a / norm_a, norm_a);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
SensorFusionEkf::SensorFusionEkf()
|
||||
: execute_reset_with_next_accelerometer_sample_(false)
|
||||
, bias_estimation_enabled_(true)
|
||||
, gyroscope_bias_estimate_({ 0, 0, 0 })
|
||||
{
|
||||
ResetState();
|
||||
}
|
||||
|
||||
void SensorFusionEkf::Reset() { execute_reset_with_next_accelerometer_sample_ = true; }
|
||||
|
||||
void SensorFusionEkf::ResetState()
|
||||
{
|
||||
current_state_.sensor_from_start_rotation = Rotation::Identity();
|
||||
current_state_.sensor_from_start_rotation_velocity = Vector3::Zero();
|
||||
|
||||
current_gyroscope_sensor_timestamp_ns_ = 0;
|
||||
current_accelerometer_sensor_timestamp_ns_ = 0;
|
||||
|
||||
state_covariance_ = Matrix3x3::Identity() * kInitialStateCovarianceValue;
|
||||
process_covariance_ = Matrix3x3::Identity() * kInitialProcessCovarianceValue;
|
||||
accelerometer_measurement_covariance_
|
||||
= Matrix3x3::Identity() * kMinAccelNoiseSigma * kMinAccelNoiseSigma;
|
||||
innovation_covariance_ = Matrix3x3::Identity();
|
||||
|
||||
accelerometer_measurement_jacobian_ = Matrix3x3::Zero();
|
||||
kalman_gain_ = Matrix3x3::Zero();
|
||||
innovation_ = Vector3::Zero();
|
||||
accelerometer_measurement_ = Vector3::Zero();
|
||||
prediction_ = Vector3::Zero();
|
||||
control_input_ = Vector3::Zero();
|
||||
state_update_ = Vector3::Zero();
|
||||
|
||||
moving_average_accelerometer_norm_change_ = 0.0;
|
||||
|
||||
is_timestep_filter_initialized_ = false;
|
||||
is_gyroscope_filter_valid_ = false;
|
||||
is_aligned_with_gravity_ = false;
|
||||
|
||||
// Reset biases.
|
||||
gyroscope_bias_estimator_.Reset();
|
||||
gyroscope_bias_estimate_ = { 0, 0, 0 };
|
||||
}
|
||||
|
||||
// Here I am doing something wrong relative to time stamps. The state timestamps
|
||||
// always correspond to the gyrostamps because it would require additional
|
||||
// extrapolation if I wanted to do otherwise.
|
||||
PoseState SensorFusionEkf::GetLatestPoseState() const { return current_state_; }
|
||||
|
||||
void SensorFusionEkf::ProcessGyroscopeSample(const GyroscopeData& sample)
|
||||
{
|
||||
// Don't accept gyroscope sample when waiting for a reset.
|
||||
if (execute_reset_with_next_accelerometer_sample_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Discard outdated samples.
|
||||
if (current_gyroscope_sensor_timestamp_ns_ >= sample.sensor_timestamp_ns) {
|
||||
current_gyroscope_sensor_timestamp_ns_ = sample.sensor_timestamp_ns;
|
||||
return;
|
||||
}
|
||||
|
||||
// Checks that we received at least one gyroscope sample in the past.
|
||||
if (current_gyroscope_sensor_timestamp_ns_ != 0) {
|
||||
double current_timestep_s = std::chrono::duration_cast<std::chrono::duration<double>>(
|
||||
std::chrono::nanoseconds(
|
||||
sample.sensor_timestamp_ns - current_gyroscope_sensor_timestamp_ns_))
|
||||
.count();
|
||||
if (current_timestep_s > kMaximumGyroscopeSampleDelay_s) {
|
||||
if (is_gyroscope_filter_valid_) {
|
||||
// Replaces the delta timestamp by the filtered estimates of the delta time.
|
||||
current_timestep_s = filtered_gyroscope_timestep_s_;
|
||||
} else {
|
||||
current_timestep_s = kDefaultGyroscopeTimestep_s;
|
||||
}
|
||||
} else {
|
||||
FilterGyroscopeTimestep(current_timestep_s);
|
||||
}
|
||||
|
||||
if (bias_estimation_enabled_) {
|
||||
gyroscope_bias_estimator_.ProcessGyroscope(sample.data, sample.sensor_timestamp_ns);
|
||||
|
||||
if (gyroscope_bias_estimator_.IsCurrentEstimateValid()) {
|
||||
// As soon as the device is considered to be static, the bias estimator
|
||||
// should have a precise estimate of the gyroscope bias.
|
||||
gyroscope_bias_estimate_ = gyroscope_bias_estimator_.GetGyroscopeBias();
|
||||
}
|
||||
}
|
||||
|
||||
// Only integrate after receiving an accelerometer sample.
|
||||
if (is_aligned_with_gravity_) {
|
||||
const Rotation rotation_from_gyroscope = pose_prediction::GetRotationFromGyroscope(
|
||||
{ sample.data[0] - gyroscope_bias_estimate_[0],
|
||||
sample.data[1] - gyroscope_bias_estimate_[1],
|
||||
sample.data[2] - gyroscope_bias_estimate_[2] },
|
||||
current_timestep_s);
|
||||
current_state_.sensor_from_start_rotation
|
||||
= rotation_from_gyroscope * current_state_.sensor_from_start_rotation;
|
||||
UpdateStateCovariance(RotationMatrixNH(rotation_from_gyroscope));
|
||||
state_covariance_ = state_covariance_
|
||||
+ ((current_timestep_s * current_timestep_s) * process_covariance_);
|
||||
}
|
||||
}
|
||||
|
||||
// Saves gyroscope event for future prediction.
|
||||
current_state_.timestamp = sample.system_timestamp;
|
||||
current_gyroscope_sensor_timestamp_ns_ = sample.sensor_timestamp_ns;
|
||||
current_state_.sensor_from_start_rotation_velocity.Set(
|
||||
sample.data[0] - gyroscope_bias_estimate_[0], sample.data[1] - gyroscope_bias_estimate_[1],
|
||||
sample.data[2] - gyroscope_bias_estimate_[2]);
|
||||
}
|
||||
|
||||
Vector3 SensorFusionEkf::ComputeInnovation(const Rotation& pose)
|
||||
{
|
||||
const Vector3 predicted_down_direction = pose * kCanonicalZDirection;
|
||||
|
||||
const Rotation rotation
|
||||
= Rotation::RotateInto(predicted_down_direction, accelerometer_measurement_);
|
||||
Vector3 axis;
|
||||
double angle;
|
||||
rotation.GetAxisAndAngle(&axis, &angle);
|
||||
return axis * angle;
|
||||
}
|
||||
|
||||
void SensorFusionEkf::ComputeMeasurementJacobian()
|
||||
{
|
||||
for (int dof = 0; dof < 3; dof++) {
|
||||
Vector3 delta = Vector3::Zero();
|
||||
delta[dof] = kFiniteDifferencingEpsilon;
|
||||
|
||||
const Rotation epsilon_rotation = RotationFromVector(delta);
|
||||
const Vector3 delta_rotation
|
||||
= ComputeInnovation(epsilon_rotation * current_state_.sensor_from_start_rotation);
|
||||
|
||||
const Vector3 col = (innovation_ - delta_rotation) / kFiniteDifferencingEpsilon;
|
||||
accelerometer_measurement_jacobian_(0, dof) = col[0];
|
||||
accelerometer_measurement_jacobian_(1, dof) = col[1];
|
||||
accelerometer_measurement_jacobian_(2, dof) = col[2];
|
||||
}
|
||||
}
|
||||
|
||||
void SensorFusionEkf::ProcessAccelerometerSample(const AccelerometerData& sample)
|
||||
{
|
||||
// Discard outdated samples.
|
||||
if (current_accelerometer_sensor_timestamp_ns_ >= sample.sensor_timestamp_ns) {
|
||||
current_accelerometer_sensor_timestamp_ns_ = sample.sensor_timestamp_ns;
|
||||
return;
|
||||
}
|
||||
|
||||
// Call reset state if required.
|
||||
if (execute_reset_with_next_accelerometer_sample_.exchange(false)) {
|
||||
ResetState();
|
||||
}
|
||||
|
||||
accelerometer_measurement_.Set(sample.data[0], sample.data[1], sample.data[2]);
|
||||
current_accelerometer_sensor_timestamp_ns_ = sample.sensor_timestamp_ns;
|
||||
|
||||
if (bias_estimation_enabled_) {
|
||||
gyroscope_bias_estimator_.ProcessAccelerometer(sample.data, sample.sensor_timestamp_ns);
|
||||
}
|
||||
|
||||
if (!is_aligned_with_gravity_) {
|
||||
// This is the first accelerometer measurement so it initializes the
|
||||
// orientation estimate.
|
||||
current_state_.sensor_from_start_rotation
|
||||
= Rotation::RotateInto(kCanonicalZDirection, accelerometer_measurement_);
|
||||
is_aligned_with_gravity_ = true;
|
||||
|
||||
previous_accelerometer_norm_ = Length(accelerometer_measurement_);
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateMeasurementCovariance();
|
||||
|
||||
innovation_ = ComputeInnovation(current_state_.sensor_from_start_rotation);
|
||||
ComputeMeasurementJacobian();
|
||||
|
||||
// S = H * P * H' + R
|
||||
innovation_covariance_ = accelerometer_measurement_jacobian_ * state_covariance_
|
||||
* Transpose(accelerometer_measurement_jacobian_)
|
||||
+ accelerometer_measurement_covariance_;
|
||||
|
||||
// K = P * H' * S^-1
|
||||
kalman_gain_ = state_covariance_ * Transpose(accelerometer_measurement_jacobian_)
|
||||
* Inverse(innovation_covariance_);
|
||||
|
||||
// x_update = K*nu
|
||||
state_update_ = kalman_gain_ * innovation_;
|
||||
|
||||
// P = (I - K * H) * P;
|
||||
state_covariance_ = (Matrix3x3::Identity() - kalman_gain_ * accelerometer_measurement_jacobian_)
|
||||
* state_covariance_;
|
||||
|
||||
// Updates pose and associate covariance matrix.
|
||||
const Rotation rotation_from_state_update = RotationFromVector(state_update_);
|
||||
|
||||
current_state_.sensor_from_start_rotation
|
||||
= rotation_from_state_update * current_state_.sensor_from_start_rotation;
|
||||
UpdateStateCovariance(RotationMatrixNH(rotation_from_state_update));
|
||||
}
|
||||
|
||||
void SensorFusionEkf::UpdateStateCovariance(const Matrix3x3& motion_update)
|
||||
{
|
||||
state_covariance_ = motion_update * state_covariance_ * Transpose(motion_update);
|
||||
}
|
||||
|
||||
void SensorFusionEkf::FilterGyroscopeTimestep(double gyroscope_timestep_s)
|
||||
{
|
||||
if (!is_timestep_filter_initialized_) {
|
||||
// Initializes the filter.
|
||||
filtered_gyroscope_timestep_s_ = gyroscope_timestep_s;
|
||||
num_gyroscope_timestep_samples_ = 1;
|
||||
is_timestep_filter_initialized_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Computes the IIR filter response.
|
||||
filtered_gyroscope_timestep_s_ = kTimestepFilterCoeff * filtered_gyroscope_timestep_s_
|
||||
+ (1 - kTimestepFilterCoeff) * gyroscope_timestep_s;
|
||||
++num_gyroscope_timestep_samples_;
|
||||
|
||||
if (num_gyroscope_timestep_samples_ > kTimestepFilterMinSamples) {
|
||||
is_gyroscope_filter_valid_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void SensorFusionEkf::UpdateMeasurementCovariance()
|
||||
{
|
||||
const double current_accelerometer_norm = Length(accelerometer_measurement_);
|
||||
// Norm change between current and previous accel readings.
|
||||
const double current_accelerometer_norm_change
|
||||
= std::abs(current_accelerometer_norm - previous_accelerometer_norm_);
|
||||
previous_accelerometer_norm_ = current_accelerometer_norm;
|
||||
|
||||
moving_average_accelerometer_norm_change_ = kSmoothingFactor * current_accelerometer_norm_change
|
||||
+ (1 - kSmoothingFactor) * moving_average_accelerometer_norm_change_;
|
||||
|
||||
// If we hit the accel norm change threshold, we use the maximum noise sigma
|
||||
// for the accel covariance. For anything below that, we use a linear
|
||||
// combination between min and max sigma values.
|
||||
const double norm_change_ratio
|
||||
= moving_average_accelerometer_norm_change_ / kMaxAccelNormChange;
|
||||
const double accelerometer_noise_sigma = std::min(kMaxAccelNoiseSigma,
|
||||
kMinAccelNoiseSigma + norm_change_ratio * (kMaxAccelNoiseSigma - kMinAccelNoiseSigma));
|
||||
|
||||
// Updates the accel covariance matrix with the new sigma value.
|
||||
accelerometer_measurement_covariance_
|
||||
= Matrix3x3::Identity() * accelerometer_noise_sigma * accelerometer_noise_sigma;
|
||||
}
|
||||
|
||||
bool SensorFusionEkf::IsBiasEstimationEnabled() const { return bias_estimation_enabled_; }
|
||||
|
||||
void SensorFusionEkf::SetBiasEstimationEnabled(bool enable)
|
||||
{
|
||||
if (bias_estimation_enabled_ != enable) {
|
||||
bias_estimation_enabled_ = enable;
|
||||
gyroscope_bias_estimate_ = { 0, 0, 0 };
|
||||
gyroscope_bias_estimator_.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,188 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_SENSORS_SENSOR_FUSION_EKF_H_
|
||||
#define CARDBOARD_SDK_SENSORS_SENSOR_FUSION_EKF_H_
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
|
||||
#include "accelerometer_data.h"
|
||||
#include "gyroscope_bias_estimator.h"
|
||||
#include "gyroscope_data.h"
|
||||
#include "pose_state.h"
|
||||
#include "../util/matrix_3x3.h"
|
||||
#include "../util/rotation.h"
|
||||
#include "../util/vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// Sensor fusion class that implements an Extended Kalman Filter (EKF) to
|
||||
// estimate a 3D rotation from a gyroscope and an accelerometer.
|
||||
// This system only has one state, the pose. It does not estimate any velocity
|
||||
// or acceleration.
|
||||
//
|
||||
// To learn more about Kalman filtering one can read this article which is a
|
||||
// good introduction: https://en.wikipedia.org/wiki/Kalman_filter
|
||||
class SensorFusionEkf {
|
||||
public:
|
||||
SensorFusionEkf();
|
||||
|
||||
// Resets the state of the sensor fusion. It sets the velocity for
|
||||
// prediction to zero. The reset will happen with the next
|
||||
// accelerometer sample. Gyroscope sample will be discarded until a new
|
||||
// accelerometer sample arrives.
|
||||
void Reset();
|
||||
|
||||
// Gets the PoseState representing the latest pose and derivatives at a
|
||||
// particular timestamp as estimated by SensorFusion.
|
||||
PoseState GetLatestPoseState() const;
|
||||
|
||||
// Processes one gyroscope sample event. This updates the pose of the system
|
||||
// and the prediction model. The gyroscope data is assumed to be in axis angle
|
||||
// form. Angle = ||v|| and Axis = v / ||v||, with v = [v_x, v_y, v_z]^T.
|
||||
//
|
||||
// @param sample gyroscope sample data.
|
||||
void ProcessGyroscopeSample(const GyroscopeData& sample);
|
||||
|
||||
// Processes one accelerometer sample event. This updates the pose of the
|
||||
// system. If the Accelerometer norm changes too much between sample it is not
|
||||
// trusted as much.
|
||||
//
|
||||
// @param sample accelerometer sample data.
|
||||
void ProcessAccelerometerSample(const AccelerometerData& sample);
|
||||
|
||||
// Enables or disables the drift correction by estimating the gyroscope bias.
|
||||
//
|
||||
// @param enable Enable drift correction.
|
||||
void SetBiasEstimationEnabled(bool enable);
|
||||
|
||||
// Returns a boolean that indicates if bias estimation is enabled or disabled.
|
||||
//
|
||||
// @return true if bias estimation is enabled, false otherwise.
|
||||
bool IsBiasEstimationEnabled() const;
|
||||
|
||||
// Returns the current gyroscope bias estimate from GyroscopeBiasEstimator.
|
||||
Vector3 GetGyroscopeBias() const {
|
||||
return {
|
||||
gyroscope_bias_estimate_[0], gyroscope_bias_estimate_[1], gyroscope_bias_estimate_[2]};
|
||||
}
|
||||
|
||||
// Returns true after receiving the first accelerometer measurement.
|
||||
bool IsFullyInitialized() const {
|
||||
return is_aligned_with_gravity_;
|
||||
}
|
||||
|
||||
private:
|
||||
// Estimates the average timestep between gyroscope event.
|
||||
void FilterGyroscopeTimestep(double gyroscope_timestep);
|
||||
|
||||
// Updates the state covariance with an incremental motion. It changes the
|
||||
// space of the quadric.
|
||||
void UpdateStateCovariance(const Matrix3x3& motion_update);
|
||||
|
||||
// Computes the innovation vector of the Kalman based on the input pose.
|
||||
// It uses the latest measurement vector (i.e. accelerometer data), which must
|
||||
// be set prior to calling this function.
|
||||
Vector3 ComputeInnovation(const Rotation& pose);
|
||||
|
||||
// This computes the measurement_jacobian_ via numerical differentiation based
|
||||
// on the current value of sensor_from_start_rotation_.
|
||||
void ComputeMeasurementJacobian();
|
||||
|
||||
// Updates the accelerometer covariance matrix.
|
||||
//
|
||||
// This looks at the norm of recent accelerometer readings. If it has changed
|
||||
// significantly, it means the phone receives additional acceleration than
|
||||
// just gravity, and so the down vector information gravity signal is noisier.
|
||||
void UpdateMeasurementCovariance();
|
||||
|
||||
// Reset all internal states. This is not thread safe. Lock should be acquired
|
||||
// outside of it. This function is called in ProcessAccelerometerSample.
|
||||
void ResetState();
|
||||
|
||||
// Current transformation from Sensor Space to Start Space.
|
||||
// x_sensor = sensor_from_start_rotation_ * x_start;
|
||||
PoseState current_state_;
|
||||
|
||||
// Filtering of the gyroscope timestep started?
|
||||
bool is_timestep_filter_initialized_;
|
||||
// Filtered gyroscope timestep valid?
|
||||
bool is_gyroscope_filter_valid_;
|
||||
// Sensor fusion currently aligned with gravity? After initialization
|
||||
// it will requires a couple of accelerometer data for the system to get
|
||||
// aligned.
|
||||
std::atomic<bool> is_aligned_with_gravity_;
|
||||
|
||||
// Covariance of Kalman filter state (P in common formulation).
|
||||
Matrix3x3 state_covariance_;
|
||||
// Covariance of the process noise (Q in common formulation).
|
||||
Matrix3x3 process_covariance_;
|
||||
// Covariance of the accelerometer measurement (R in common formulation).
|
||||
Matrix3x3 accelerometer_measurement_covariance_;
|
||||
// Covariance of innovation (S in common formulation).
|
||||
Matrix3x3 innovation_covariance_;
|
||||
// Jacobian of the measurements (H in common formulation).
|
||||
Matrix3x3 accelerometer_measurement_jacobian_;
|
||||
// Gain of the Kalman filter (K in common formulation).
|
||||
Matrix3x3 kalman_gain_;
|
||||
// Parameter update a.k.a. innovation vector. (\nu in common formulation).
|
||||
Vector3 innovation_;
|
||||
// Measurement vector (z in common formulation).
|
||||
Vector3 accelerometer_measurement_;
|
||||
// Current prediction vector (g in common formulation).
|
||||
Vector3 prediction_;
|
||||
// Control input, currently this is only the gyroscope data (\mu in common
|
||||
// formulation).
|
||||
Vector3 control_input_;
|
||||
// Update of the state vector. (x in common formulation).
|
||||
Vector3 state_update_;
|
||||
|
||||
// Sensor time of the last gyroscope processed event.
|
||||
uint64_t current_gyroscope_sensor_timestamp_ns_;
|
||||
// Sensor time of the last accelerometer processed event.
|
||||
uint64_t current_accelerometer_sensor_timestamp_ns_;
|
||||
|
||||
// Estimates of the timestep between gyroscope event in seconds.
|
||||
double filtered_gyroscope_timestep_s_;
|
||||
// Number of timestep samples processed so far by the filter.
|
||||
uint32_t num_gyroscope_timestep_samples_;
|
||||
// Norm of the accelerometer for the previous measurement.
|
||||
double previous_accelerometer_norm_;
|
||||
// Moving average of the accelerometer norm changes. It is computed for every
|
||||
// sensor datum.
|
||||
double moving_average_accelerometer_norm_change_;
|
||||
|
||||
// Flag indicating if a state reset should be executed with the next
|
||||
// accelerometer sample.
|
||||
std::atomic<bool> execute_reset_with_next_accelerometer_sample_;
|
||||
|
||||
// Flag indicating if bias estimation is enabled (enabled by default).
|
||||
std::atomic<bool> bias_estimation_enabled_;
|
||||
|
||||
// Bias estimator and static device detector.
|
||||
GyroscopeBiasEstimator gyroscope_bias_estimator_;
|
||||
|
||||
// Current bias estimate_;
|
||||
Vector3 gyroscope_bias_estimate_;
|
||||
|
||||
SensorFusionEkf(const SensorFusionEkf&) = delete;
|
||||
SensorFusionEkf& operator=(const SensorFusionEkf&) = delete;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_SENSORS_SENSOR_FUSION_EKF_H_
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_UTIL_LOGGING_H_
|
||||
#define CARDBOARD_SDK_UTIL_LOGGING_H_
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
|
||||
#include <android/log.h>
|
||||
|
||||
// Uncomment these to enable debug logging from native code
|
||||
|
||||
#define CARDBOARD_LOGI(...) // __android_log_print(ANDROID_LOG_INFO, "CardboardSDK", __VA_ARGS__)
|
||||
#define CARDBOARD_LOGE(...) // __android_log_print(ANDROID_LOG_ERROR, "CardboardSDK", __VA_ARGS__)
|
||||
|
||||
#else
|
||||
|
||||
#define CARDBOARD_LOGI(...) // FURI_LOG_I("CardboardSDK", __VA_ARGS__)
|
||||
#define CARDBOARD_LOGE(...) // FURI_LOG_E("CardboardSDK", __VA_ARGS__)
|
||||
|
||||
#endif
|
||||
|
||||
#endif // CARDBOARD_SDK_UTIL_LOGGING_H_
|
||||
@@ -1,121 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "matrix_3x3.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
Matrix3x3::Matrix3x3(double m00, double m01, double m02, double m10, double m11, double m12,
|
||||
double m20, double m21, double m22)
|
||||
: elem_ { { { m00, m01, m02 }, { m10, m11, m12 }, { m20, m21, m22 } } }
|
||||
{
|
||||
}
|
||||
|
||||
Matrix3x3::Matrix3x3()
|
||||
{
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col)
|
||||
elem_[row][col] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Matrix3x3 Matrix3x3::Zero()
|
||||
{
|
||||
Matrix3x3 result;
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix3x3 Matrix3x3::Identity()
|
||||
{
|
||||
Matrix3x3 result;
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
result.elem_[row][row] = 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void Matrix3x3::MultiplyScalar(double s)
|
||||
{
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col)
|
||||
elem_[row][col] *= s;
|
||||
}
|
||||
}
|
||||
|
||||
Matrix3x3 Matrix3x3::Negation() const
|
||||
{
|
||||
Matrix3x3 result;
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col)
|
||||
result.elem_[row][col] = -elem_[row][col];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix3x3 Matrix3x3::Scale(const Matrix3x3& m, double s)
|
||||
{
|
||||
Matrix3x3 result;
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col)
|
||||
result.elem_[row][col] = m.elem_[row][col] * s;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix3x3 Matrix3x3::Addition(const Matrix3x3& lhs, const Matrix3x3& rhs)
|
||||
{
|
||||
Matrix3x3 result;
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col)
|
||||
result.elem_[row][col] = lhs.elem_[row][col] + rhs.elem_[row][col];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix3x3 Matrix3x3::Subtraction(const Matrix3x3& lhs, const Matrix3x3& rhs)
|
||||
{
|
||||
Matrix3x3 result;
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col)
|
||||
result.elem_[row][col] = lhs.elem_[row][col] - rhs.elem_[row][col];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix3x3 Matrix3x3::Product(const Matrix3x3& m0, const Matrix3x3& m1)
|
||||
{
|
||||
Matrix3x3 result;
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col) {
|
||||
result.elem_[row][col] = 0;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
result.elem_[row][col] += m0.elem_[row][i] * m1.elem_[i][col];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Matrix3x3::AreEqual(const Matrix3x3& m0, const Matrix3x3& m1)
|
||||
{
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col) {
|
||||
if (m0.elem_[row][col] != m1.elem_[row][col])
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,138 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_UTIL_MATRIX_3X3_H_
|
||||
#define CARDBOARD_SDK_UTIL_MATRIX_3X3_H_
|
||||
|
||||
#include <array>
|
||||
#include <cstring> // For memcpy().
|
||||
#include <istream> // NOLINT
|
||||
#include <ostream> // NOLINT
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// The Matrix3x3 class defines a square 3-dimensional matrix. Elements are
|
||||
// stored in row-major order.
|
||||
// TODO(b/135461889): Make this class consistent with Matrix4x4.
|
||||
class Matrix3x3 {
|
||||
public:
|
||||
// The default constructor zero-initializes all elements.
|
||||
Matrix3x3();
|
||||
|
||||
// Dimension-specific constructors that are passed individual element values.
|
||||
Matrix3x3(
|
||||
double m00,
|
||||
double m01,
|
||||
double m02,
|
||||
double m10,
|
||||
double m11,
|
||||
double m12,
|
||||
double m20,
|
||||
double m21,
|
||||
double m22);
|
||||
|
||||
// Constructor that reads elements from a linear array of the correct size.
|
||||
explicit Matrix3x3(const double array[3 * 3]);
|
||||
|
||||
// Returns a Matrix3x3 containing all zeroes.
|
||||
static Matrix3x3 Zero();
|
||||
|
||||
// Returns an identity Matrix3x3.
|
||||
static Matrix3x3 Identity();
|
||||
|
||||
// Mutable element accessors.
|
||||
double& operator()(int row, int col) {
|
||||
return elem_[row][col];
|
||||
}
|
||||
std::array<double, 3>& operator[](int row) {
|
||||
return elem_[row];
|
||||
}
|
||||
|
||||
// Read-only element accessors.
|
||||
const double& operator()(int row, int col) const {
|
||||
return elem_[row][col];
|
||||
}
|
||||
const std::array<double, 3>& operator[](int row) const {
|
||||
return elem_[row];
|
||||
}
|
||||
|
||||
// Return a pointer to the data for interfacing with libraries.
|
||||
double* Data() {
|
||||
return &elem_[0][0];
|
||||
}
|
||||
const double* Data() const {
|
||||
return &elem_[0][0];
|
||||
}
|
||||
|
||||
// Self-modifying multiplication operators.
|
||||
void operator*=(double s) {
|
||||
MultiplyScalar(s);
|
||||
}
|
||||
void operator*=(const Matrix3x3& m) {
|
||||
*this = Product(*this, m);
|
||||
}
|
||||
|
||||
// Unary operators.
|
||||
Matrix3x3 operator-() const {
|
||||
return Negation();
|
||||
}
|
||||
|
||||
// Binary scale operators.
|
||||
friend Matrix3x3 operator*(const Matrix3x3& m, double s) {
|
||||
return Scale(m, s);
|
||||
}
|
||||
friend Matrix3x3 operator*(double s, const Matrix3x3& m) {
|
||||
return Scale(m, s);
|
||||
}
|
||||
|
||||
// Binary matrix addition.
|
||||
friend Matrix3x3 operator+(const Matrix3x3& lhs, const Matrix3x3& rhs) {
|
||||
return Addition(lhs, rhs);
|
||||
}
|
||||
|
||||
// Binary matrix subtraction.
|
||||
friend Matrix3x3 operator-(const Matrix3x3& lhs, const Matrix3x3& rhs) {
|
||||
return Subtraction(lhs, rhs);
|
||||
}
|
||||
|
||||
// Binary multiplication operator.
|
||||
friend Matrix3x3 operator*(const Matrix3x3& m0, const Matrix3x3& m1) {
|
||||
return Product(m0, m1);
|
||||
}
|
||||
|
||||
// Exact equality and inequality comparisons.
|
||||
friend bool operator==(const Matrix3x3& m0, const Matrix3x3& m1) {
|
||||
return AreEqual(m0, m1);
|
||||
}
|
||||
friend bool operator!=(const Matrix3x3& m0, const Matrix3x3& m1) {
|
||||
return !AreEqual(m0, m1);
|
||||
}
|
||||
|
||||
private:
|
||||
// These private functions implement most of the operators.
|
||||
void MultiplyScalar(double s);
|
||||
Matrix3x3 Negation() const;
|
||||
static Matrix3x3 Addition(const Matrix3x3& lhs, const Matrix3x3& rhs);
|
||||
static Matrix3x3 Subtraction(const Matrix3x3& lhs, const Matrix3x3& rhs);
|
||||
static Matrix3x3 Scale(const Matrix3x3& m, double s);
|
||||
static Matrix3x3 Product(const Matrix3x3& m0, const Matrix3x3& m1);
|
||||
static bool AreEqual(const Matrix3x3& m0, const Matrix3x3& m1);
|
||||
|
||||
std::array<std::array<double, 3>, 3> elem_;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_UTIL_MATRIX_3X3_H_
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "matrix_4x4.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
Matrix4x4 Matrix4x4::Identity()
|
||||
{
|
||||
Matrix4x4 ret;
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
ret.m[j][i] = (i == j) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Matrix4x4 Matrix4x4::Zeros()
|
||||
{
|
||||
Matrix4x4 ret;
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
ret.m[j][i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Matrix4x4 Matrix4x4::Translation(float x, float y, float z)
|
||||
{
|
||||
Matrix4x4 ret = Matrix4x4::Identity();
|
||||
ret.m[3][0] = x;
|
||||
ret.m[3][1] = y;
|
||||
ret.m[3][2] = z;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Matrix4x4 Matrix4x4::Perspective(const std::array<float, 4>& fov, float zNear, float zFar)
|
||||
{
|
||||
Matrix4x4 ret = Matrix4x4::Zeros();
|
||||
|
||||
const float xLeft = -std::tan(fov[0] * M_PI / 180.0f) * zNear;
|
||||
const float xRight = std::tan(fov[1] * M_PI / 180.0f) * zNear;
|
||||
const float yBottom = -std::tan(fov[2] * M_PI / 180.0f) * zNear;
|
||||
const float yTop = std::tan(fov[3] * M_PI / 180.0f) * zNear;
|
||||
|
||||
const float X = (2 * zNear) / (xRight - xLeft);
|
||||
const float Y = (2 * zNear) / (yTop - yBottom);
|
||||
const float A = (xRight + xLeft) / (xRight - xLeft);
|
||||
const float B = (yTop + yBottom) / (yTop - yBottom);
|
||||
const float C = (zNear + zFar) / (zNear - zFar);
|
||||
const float D = (2 * zNear * zFar) / (zNear - zFar);
|
||||
|
||||
ret.m[0][0] = X;
|
||||
ret.m[2][0] = A;
|
||||
ret.m[1][1] = Y;
|
||||
ret.m[2][1] = B;
|
||||
ret.m[2][2] = C;
|
||||
ret.m[3][2] = D;
|
||||
ret.m[2][3] = -1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Matrix4x4::ToArray(float* array) const { std::memcpy(array, &m[0][0], 16 * sizeof(float)); }
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_UTIL_MATRIX_4X4_H_
|
||||
#define CARDBOARD_SDK_UTIL_MATRIX_4X4_H_
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
class Matrix4x4 {
|
||||
public:
|
||||
static Matrix4x4 Identity();
|
||||
static Matrix4x4 Zeros();
|
||||
static Matrix4x4 Translation(float x, float y, float z);
|
||||
static Matrix4x4 Perspective(const std::array<float, 4>& fov, float zNear, float zFar);
|
||||
void ToArray(float* array) const;
|
||||
|
||||
private:
|
||||
std::array<std::array<float, 4>, 4> m;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_UTIL_MATRIX4X4_H_
|
||||
@@ -1,148 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "matrixutils.h"
|
||||
|
||||
#include "vectorutils.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
namespace {
|
||||
|
||||
// Returns true if the cofactor for a given row and column should be negated.
|
||||
static bool IsCofactorNegated(int row, int col)
|
||||
{
|
||||
// Negated iff (row + col) is odd.
|
||||
return ((row + col) & 1) != 0;
|
||||
}
|
||||
|
||||
static double CofactorElement3(const Matrix3x3& m, int row, int col)
|
||||
{
|
||||
static const int index[3][2] = { { 1, 2 }, { 0, 2 }, { 0, 1 } };
|
||||
const int i0 = index[row][0];
|
||||
const int i1 = index[row][1];
|
||||
const int j0 = index[col][0];
|
||||
const int j1 = index[col][1];
|
||||
const double cofactor = m(i0, j0) * m(i1, j1) - m(i0, j1) * m(i1, j0);
|
||||
return IsCofactorNegated(row, col) ? -cofactor : cofactor;
|
||||
}
|
||||
|
||||
// Multiplies a matrix and some type of column vector to
|
||||
// produce another column vector of the same type.
|
||||
Vector3 MultiplyMatrixAndVector(const Matrix3x3& m, const Vector3& v)
|
||||
{
|
||||
Vector3 result = Vector3::Zero();
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col)
|
||||
result[row] += m(row, col) * v[col];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Sets the upper 3x3 of a Matrix to represent a 3D rotation.
|
||||
void RotationMatrix3x3(const Rotation& r, Matrix3x3* matrix)
|
||||
{
|
||||
//
|
||||
// Given a quaternion (a,b,c,d) where d is the scalar part, the 3x3 rotation
|
||||
// matrix is:
|
||||
//
|
||||
// a^2 - b^2 - c^2 + d^2 2ab - 2cd 2ac + 2bd
|
||||
// 2ab + 2cd -a^2 + b^2 - c^2 + d^2 2bc - 2ad
|
||||
// 2ac - 2bd 2bc + 2ad -a^2 - b^2 + c^2 + d^2
|
||||
//
|
||||
const Vector<4>& quat = r.GetQuaternion();
|
||||
const double aa = quat[0] * quat[0];
|
||||
const double bb = quat[1] * quat[1];
|
||||
const double cc = quat[2] * quat[2];
|
||||
const double dd = quat[3] * quat[3];
|
||||
|
||||
const double ab = quat[0] * quat[1];
|
||||
const double ac = quat[0] * quat[2];
|
||||
const double bc = quat[1] * quat[2];
|
||||
|
||||
const double ad = quat[0] * quat[3];
|
||||
const double bd = quat[1] * quat[3];
|
||||
const double cd = quat[2] * quat[3];
|
||||
|
||||
Matrix3x3& m = *matrix;
|
||||
m[0][0] = aa - bb - cc + dd;
|
||||
m[0][1] = 2 * ab - 2 * cd;
|
||||
m[0][2] = 2 * ac + 2 * bd;
|
||||
m[1][0] = 2 * ab + 2 * cd;
|
||||
m[1][1] = -aa + bb - cc + dd;
|
||||
m[1][2] = 2 * bc - 2 * ad;
|
||||
m[2][0] = 2 * ac - 2 * bd;
|
||||
m[2][1] = 2 * bc + 2 * ad;
|
||||
m[2][2] = -aa - bb + cc + dd;
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
Vector3 operator*(const Matrix3x3& m, const Vector3& v) { return MultiplyMatrixAndVector(m, v); }
|
||||
|
||||
Matrix3x3 CofactorMatrix(const Matrix3x3& m)
|
||||
{
|
||||
Matrix3x3 result;
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col)
|
||||
result(row, col) = CofactorElement3(m, row, col);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix3x3 AdjugateWithDeterminant(const Matrix3x3& m, double* determinant)
|
||||
{
|
||||
const Matrix3x3 cofactor_matrix = CofactorMatrix(m);
|
||||
if (determinant) {
|
||||
*determinant = m(0, 0) * cofactor_matrix(0, 0) + m(0, 1) * cofactor_matrix(0, 1)
|
||||
+ m(0, 2) * cofactor_matrix(0, 2);
|
||||
}
|
||||
return Transpose(cofactor_matrix);
|
||||
}
|
||||
|
||||
// Returns the transpose of a matrix.
|
||||
Matrix3x3 Transpose(const Matrix3x3& m)
|
||||
{
|
||||
Matrix3x3 result;
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int col = 0; col < 3; ++col)
|
||||
result(row, col) = m(col, row);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix3x3 InverseWithDeterminant(const Matrix3x3& m, double* determinant)
|
||||
{
|
||||
// The inverse is the adjugate divided by the determinant.
|
||||
double det;
|
||||
Matrix3x3 adjugate = AdjugateWithDeterminant(m, &det);
|
||||
if (determinant)
|
||||
*determinant = det;
|
||||
if (det == 0)
|
||||
return Matrix3x3::Zero();
|
||||
else
|
||||
return adjugate * (1 / det);
|
||||
}
|
||||
|
||||
Matrix3x3 Inverse(const Matrix3x3& m) { return InverseWithDeterminant(m, nullptr); }
|
||||
|
||||
Matrix3x3 RotationMatrixNH(const Rotation& r)
|
||||
{
|
||||
Matrix3x3 m;
|
||||
RotationMatrix3x3(r, &m);
|
||||
return m;
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_UTIL_MATRIXUTILS_H_
|
||||
#define CARDBOARD_SDK_UTIL_MATRIXUTILS_H_
|
||||
|
||||
//
|
||||
// This file contains operators and free functions that define generic Matrix
|
||||
// operations.
|
||||
//
|
||||
|
||||
#include "matrix_3x3.h"
|
||||
#include "rotation.h"
|
||||
#include "vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// Returns the transpose of a matrix.
|
||||
Matrix3x3 Transpose(const Matrix3x3& m);
|
||||
|
||||
// Multiplies a Matrix and a column Vector of the same Dimension to produce
|
||||
// another column Vector.
|
||||
Vector3 operator*(const Matrix3x3& m, const Vector3& v);
|
||||
|
||||
// Returns the determinant of the matrix. This function is defined for all the
|
||||
// typedef'ed Matrix types.
|
||||
double Determinant(const Matrix3x3& m);
|
||||
|
||||
// Returns the adjugate of the matrix, which is defined as the transpose of the
|
||||
// cofactor matrix. This function is defined for all the typedef'ed Matrix
|
||||
// types. The determinant of the matrix is computed as a side effect, so it is
|
||||
// returned in the determinant parameter if it is not null.
|
||||
Matrix3x3 AdjugateWithDeterminant(const Matrix3x3& m, double* determinant);
|
||||
|
||||
// Returns the inverse of the matrix. This function is defined for all the
|
||||
// typedef'ed Matrix types. The determinant of the matrix is computed as a
|
||||
// side effect, so it is returned in the determinant parameter if it is not
|
||||
// null. If the determinant is 0, the returned matrix has all zeroes.
|
||||
Matrix3x3 InverseWithDeterminant(const Matrix3x3& m, double* determinant);
|
||||
|
||||
// Returns the inverse of the matrix. This function is defined for all the
|
||||
// typedef'ed Matrix types. If the determinant of the matrix is 0, the returned
|
||||
// matrix has all zeroes.
|
||||
Matrix3x3 Inverse(const Matrix3x3& m);
|
||||
|
||||
// Returns a 3x3 Matrix representing a 3D rotation. This creates a Matrix that
|
||||
// does not work with homogeneous coordinates, so the function name ends in
|
||||
// "NH".
|
||||
Matrix3x3 RotationMatrixNH(const Rotation& r);
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_UTIL_MATRIXUTILS_H_
|
||||
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "rotation.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
#include "vectorutils.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
void Rotation::SetAxisAndAngle(const VectorType& axis, double angle)
|
||||
{
|
||||
VectorType unit_axis = axis;
|
||||
if (!Normalize(&unit_axis)) {
|
||||
*this = Identity();
|
||||
} else {
|
||||
double a = angle / 2;
|
||||
const double s = sin(a);
|
||||
const VectorType v(unit_axis * s);
|
||||
SetQuaternion(QuaternionType(v[0], v[1], v[2], cos(a)));
|
||||
}
|
||||
}
|
||||
|
||||
Rotation Rotation::FromRotationMatrix(const Matrix3x3& mat)
|
||||
{
|
||||
static const double kOne = 1.0;
|
||||
static const double kFour = 4.0;
|
||||
|
||||
const double d0 = mat(0, 0), d1 = mat(1, 1), d2 = mat(2, 2);
|
||||
const double ww = kOne + d0 + d1 + d2;
|
||||
const double xx = kOne + d0 - d1 - d2;
|
||||
const double yy = kOne - d0 + d1 - d2;
|
||||
const double zz = kOne - d0 - d1 + d2;
|
||||
|
||||
const double max = std::max(ww, std::max(xx, std::max(yy, zz)));
|
||||
if (ww == max) {
|
||||
const double w4 = sqrt(ww * kFour);
|
||||
return Rotation::FromQuaternion(QuaternionType((mat(2, 1) - mat(1, 2)) / w4,
|
||||
(mat(0, 2) - mat(2, 0)) / w4, (mat(1, 0) - mat(0, 1)) / w4, w4 / kFour));
|
||||
}
|
||||
|
||||
if (xx == max) {
|
||||
const double x4 = sqrt(xx * kFour);
|
||||
return Rotation::FromQuaternion(QuaternionType(x4 / kFour, (mat(0, 1) + mat(1, 0)) / x4,
|
||||
(mat(0, 2) + mat(2, 0)) / x4, (mat(2, 1) - mat(1, 2)) / x4));
|
||||
}
|
||||
|
||||
if (yy == max) {
|
||||
const double y4 = sqrt(yy * kFour);
|
||||
return Rotation::FromQuaternion(QuaternionType((mat(0, 1) + mat(1, 0)) / y4, y4 / kFour,
|
||||
(mat(1, 2) + mat(2, 1)) / y4, (mat(0, 2) - mat(2, 0)) / y4));
|
||||
}
|
||||
|
||||
// zz is the largest component.
|
||||
const double z4 = sqrt(zz * kFour);
|
||||
return Rotation::FromQuaternion(QuaternionType((mat(0, 2) + mat(2, 0)) / z4,
|
||||
(mat(1, 2) + mat(2, 1)) / z4, z4 / kFour, (mat(1, 0) - mat(0, 1)) / z4));
|
||||
}
|
||||
|
||||
void Rotation::GetAxisAndAngle(VectorType* axis, double* angle) const
|
||||
{
|
||||
VectorType vec(quat_[0], quat_[1], quat_[2]);
|
||||
if (Normalize(&vec)) {
|
||||
*angle = 2 * acos(quat_[3]);
|
||||
*axis = vec;
|
||||
} else {
|
||||
*axis = VectorType(1, 0, 0);
|
||||
*angle = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
Rotation Rotation::RotateInto(const VectorType& from, const VectorType& to)
|
||||
{
|
||||
static const double kTolerance = std::numeric_limits<double>::epsilon() * 100;
|
||||
|
||||
// Directly build the quaternion using the following technique:
|
||||
// http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
|
||||
const double norm_u_norm_v = sqrt(LengthSquared(from) * LengthSquared(to));
|
||||
double real_part = norm_u_norm_v + Dot(from, to);
|
||||
VectorType w;
|
||||
if (real_part < kTolerance * norm_u_norm_v) {
|
||||
// If |from| and |to| are exactly opposite, rotate 180 degrees around an
|
||||
// arbitrary orthogonal axis. Axis normalization can happen later, when we
|
||||
// normalize the quaternion.
|
||||
real_part = 0.0;
|
||||
w = (abs(from[0]) > abs(from[2])) ? VectorType(-from[1], from[0], 0)
|
||||
: VectorType(0, -from[2], from[1]);
|
||||
} else {
|
||||
// Otherwise, build the quaternion the standard way.
|
||||
w = Cross(from, to);
|
||||
}
|
||||
|
||||
// Build and return a normalized quaternion.
|
||||
// Note that Rotation::FromQuaternion automatically performs normalization.
|
||||
return Rotation::FromQuaternion(QuaternionType(w[0], w[1], w[2], real_part));
|
||||
}
|
||||
|
||||
Rotation::VectorType Rotation::operator*(const Rotation::VectorType& v) const
|
||||
{
|
||||
return ApplyToVector(v);
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,156 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_UTIL_ROTATION_H_
|
||||
#define CARDBOARD_SDK_UTIL_ROTATION_H_
|
||||
|
||||
#include "matrix_3x3.h"
|
||||
#include "vector.h"
|
||||
#include "vectorutils.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// The Rotation class represents a rotation around a 3-dimensional axis. It
|
||||
// uses normalized quaternions internally to make the math robust.
|
||||
class Rotation {
|
||||
public:
|
||||
// Convenience typedefs for vector of the correct type.
|
||||
typedef Vector<3> VectorType;
|
||||
typedef Vector<4> QuaternionType;
|
||||
|
||||
// The default constructor creates an identity Rotation, which has no effect.
|
||||
Rotation() {
|
||||
quat_.Set(0, 0, 0, 1);
|
||||
}
|
||||
|
||||
// Returns an identity Rotation, which has no effect.
|
||||
static Rotation Identity() {
|
||||
return Rotation();
|
||||
}
|
||||
|
||||
// Sets the Rotation from a quaternion (4D vector), which is first normalized.
|
||||
void SetQuaternion(const QuaternionType& quaternion) {
|
||||
quat_ = Normalized(quaternion);
|
||||
}
|
||||
|
||||
// Returns the Rotation as a normalized quaternion (4D vector).
|
||||
const QuaternionType& GetQuaternion() const {
|
||||
return quat_;
|
||||
}
|
||||
|
||||
// Sets the Rotation to rotate by the given angle around the given axis,
|
||||
// following the right-hand rule. The axis does not need to be unit
|
||||
// length. If it is zero length, this results in an identity Rotation.
|
||||
void SetAxisAndAngle(const VectorType& axis, double angle);
|
||||
|
||||
// Returns the right-hand rule axis and angle corresponding to the
|
||||
// Rotation. If the Rotation is the identity rotation, this returns the +X
|
||||
// axis and an angle of 0.
|
||||
void GetAxisAndAngle(VectorType* axis, double* angle) const;
|
||||
|
||||
// Convenience function that constructs and returns a Rotation given an axis
|
||||
// and angle.
|
||||
static Rotation FromAxisAndAngle(const VectorType& axis, double angle) {
|
||||
Rotation r;
|
||||
r.SetAxisAndAngle(axis, angle);
|
||||
return r;
|
||||
}
|
||||
|
||||
// Convenience function that constructs and returns a Rotation given a
|
||||
// quaternion.
|
||||
static Rotation FromQuaternion(const QuaternionType& quat) {
|
||||
Rotation r;
|
||||
r.SetQuaternion(quat);
|
||||
return r;
|
||||
}
|
||||
|
||||
// Convenience function that constructs and returns a Rotation given a
|
||||
// rotation matrix R with $R^\top R = I && det(R) = 1$.
|
||||
static Rotation FromRotationMatrix(const Matrix3x3& mat);
|
||||
|
||||
// Convenience function that constructs and returns a Rotation given Euler
|
||||
// angles that are applied in the order of rotate-Z by roll, rotate-X by
|
||||
// pitch, rotate-Y by yaw (same as GetRollPitchYaw).
|
||||
static Rotation FromRollPitchYaw(double roll, double pitch, double yaw) {
|
||||
VectorType x(1, 0, 0), y(0, 1, 0), z(0, 0, 1);
|
||||
return FromAxisAndAngle(z, roll) * (FromAxisAndAngle(x, pitch) * FromAxisAndAngle(y, yaw));
|
||||
}
|
||||
|
||||
// Convenience function that constructs and returns a Rotation given Euler
|
||||
// angles that are applied in the order of rotate-Y by yaw, rotate-X by
|
||||
// pitch, rotate-Z by roll (same as GetYawPitchRoll).
|
||||
static Rotation FromYawPitchRoll(double yaw, double pitch, double roll) {
|
||||
VectorType x(1, 0, 0), y(0, 1, 0), z(0, 0, 1);
|
||||
return FromAxisAndAngle(y, yaw) * (FromAxisAndAngle(x, pitch) * FromAxisAndAngle(z, roll));
|
||||
}
|
||||
|
||||
// Constructs and returns a Rotation that rotates one vector to another along
|
||||
// the shortest arc. This returns an identity rotation if either vector has
|
||||
// zero length.
|
||||
static Rotation RotateInto(const VectorType& from, const VectorType& to);
|
||||
|
||||
// The negation operator returns the inverse rotation.
|
||||
friend Rotation operator-(const Rotation& r) {
|
||||
// Because we store normalized quaternions, the inverse is found by
|
||||
// negating the vector part.
|
||||
return Rotation(-r.quat_[0], -r.quat_[1], -r.quat_[2], r.quat_[3]);
|
||||
}
|
||||
|
||||
// Appends a rotation to this one.
|
||||
Rotation& operator*=(const Rotation& r) {
|
||||
const QuaternionType& qr = r.quat_;
|
||||
QuaternionType& qt = quat_;
|
||||
SetQuaternion(QuaternionType(
|
||||
qr[3] * qt[0] + qr[0] * qt[3] + qr[2] * qt[1] - qr[1] * qt[2],
|
||||
qr[3] * qt[1] + qr[1] * qt[3] + qr[0] * qt[2] - qr[2] * qt[0],
|
||||
qr[3] * qt[2] + qr[2] * qt[3] + qr[1] * qt[0] - qr[0] * qt[1],
|
||||
qr[3] * qt[3] - qr[0] * qt[0] - qr[1] * qt[1] - qr[2] * qt[2]));
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Binary multiplication operator - returns a composite Rotation.
|
||||
friend const Rotation operator*(const Rotation& r0, const Rotation& r1) {
|
||||
Rotation r = r0;
|
||||
r *= r1;
|
||||
return r;
|
||||
}
|
||||
|
||||
// Multiply a Rotation and a Vector to get a Vector.
|
||||
VectorType operator*(const VectorType& v) const;
|
||||
|
||||
private:
|
||||
// Private constructor that builds a Rotation from quaternion components.
|
||||
Rotation(double q0, double q1, double q2, double q3)
|
||||
: quat_(q0, q1, q2, q3) {
|
||||
}
|
||||
|
||||
// Applies a Rotation to a Vector to rotate the Vector. Method borrowed from:
|
||||
// http://blog.molecular-matters.com/2013/05/24/a-faster-quaternion-vector-multiplication/
|
||||
VectorType ApplyToVector(const VectorType& v) const {
|
||||
VectorType im(quat_[0], quat_[1], quat_[2]);
|
||||
VectorType temp = 2.0 * Cross(im, v);
|
||||
return v + quat_[3] * temp + Cross(im, temp);
|
||||
}
|
||||
|
||||
// The rotation represented as a normalized quaternion. (Unit quaternions are
|
||||
// required for constructing rotation matrices, so it makes sense to always
|
||||
// store them that way.) The vector part is in the first 3 elements, and the
|
||||
// scalar part is in the last element.
|
||||
QuaternionType quat_;
|
||||
};
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_UTIL_ROTATION_H_
|
||||
@@ -1,251 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_UTIL_VECTOR_H_
|
||||
#define CARDBOARD_SDK_UTIL_VECTOR_H_
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// Geometric N-dimensional Vector class.
|
||||
template <int Dimension>
|
||||
class Vector {
|
||||
public:
|
||||
// The default constructor zero-initializes all elements.
|
||||
Vector();
|
||||
|
||||
// Dimension-specific constructors that are passed individual element values.
|
||||
constexpr Vector(double e0, double e1, double e2);
|
||||
constexpr Vector(double e0, double e1, double e2, double e3);
|
||||
|
||||
// Constructor for a Vector of dimension N from a Vector of dimension N-1 and
|
||||
// a scalar of the correct type, assuming N is at least 2.
|
||||
// constexpr Vector(const Vector<Dimension - 1>& v, double s);
|
||||
|
||||
void Set(double e0, double e1, double e2); // Only when Dimension == 3.
|
||||
void Set(double e0, double e1, double e2,
|
||||
double e3); // Only when Dimension == 4.
|
||||
|
||||
// Mutable element accessor.
|
||||
double& operator[](int index) {
|
||||
return elem_[index];
|
||||
}
|
||||
|
||||
// Element accessor.
|
||||
double operator[](int index) const {
|
||||
return elem_[index];
|
||||
}
|
||||
|
||||
// Returns a Vector containing all zeroes.
|
||||
static Vector Zero();
|
||||
|
||||
// Self-modifying operators.
|
||||
void operator+=(const Vector& v) {
|
||||
Add(v);
|
||||
}
|
||||
void operator-=(const Vector& v) {
|
||||
Subtract(v);
|
||||
}
|
||||
void operator*=(double s) {
|
||||
Multiply(s);
|
||||
}
|
||||
void operator/=(double s) {
|
||||
Divide(s);
|
||||
}
|
||||
|
||||
// Unary negation operator.
|
||||
Vector operator-() const {
|
||||
return Negation();
|
||||
}
|
||||
|
||||
// Binary operators.
|
||||
friend Vector operator+(const Vector& v0, const Vector& v1) {
|
||||
return Sum(v0, v1);
|
||||
}
|
||||
friend Vector operator-(const Vector& v0, const Vector& v1) {
|
||||
return Difference(v0, v1);
|
||||
}
|
||||
friend Vector operator*(const Vector& v, double s) {
|
||||
return Scale(v, s);
|
||||
}
|
||||
friend Vector operator*(double s, const Vector& v) {
|
||||
return Scale(v, s);
|
||||
}
|
||||
friend Vector operator*(const Vector& v, const Vector& s) {
|
||||
return Product(v, s);
|
||||
}
|
||||
friend Vector operator/(const Vector& v, double s) {
|
||||
return Divide(v, s);
|
||||
}
|
||||
|
||||
// Self-modifying addition.
|
||||
void Add(const Vector& v);
|
||||
// Self-modifying subtraction.
|
||||
void Subtract(const Vector& v);
|
||||
// Self-modifying multiplication by a scalar.
|
||||
void Multiply(double s);
|
||||
// Self-modifying division by a scalar.
|
||||
void Divide(double s);
|
||||
|
||||
// Unary negation.
|
||||
Vector Negation() const;
|
||||
|
||||
// Binary component-wise multiplication.
|
||||
static Vector Product(const Vector& v0, const Vector& v1);
|
||||
// Binary component-wise addition.
|
||||
static Vector Sum(const Vector& v0, const Vector& v1);
|
||||
// Binary component-wise subtraction.
|
||||
static Vector Difference(const Vector& v0, const Vector& v1);
|
||||
// Binary multiplication by a scalar.
|
||||
static Vector Scale(const Vector& v, double s);
|
||||
// Binary division by a scalar.
|
||||
static Vector Divide(const Vector& v, double s);
|
||||
|
||||
private:
|
||||
std::array<double, Dimension> elem_;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template <int Dimension>
|
||||
Vector<Dimension>::Vector() {
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
elem_[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
constexpr Vector<Dimension>::Vector(double e0, double e1, double e2)
|
||||
: elem_{e0, e1, e2} {
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
constexpr Vector<Dimension>::Vector(double e0, double e1, double e2, double e3)
|
||||
: elem_{e0, e1, e2, e3} {
|
||||
}
|
||||
/*
|
||||
template <>
|
||||
constexpr Vector<4>::Vector(const Vector<3>& v, double s)
|
||||
: elem_{v[0], v[1], v[2], s} {}
|
||||
*/
|
||||
template <int Dimension>
|
||||
void Vector<Dimension>::Set(double e0, double e1, double e2) {
|
||||
elem_[0] = e0;
|
||||
elem_[1] = e1;
|
||||
elem_[2] = e2;
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
void Vector<Dimension>::Set(double e0, double e1, double e2, double e3) {
|
||||
elem_[0] = e0;
|
||||
elem_[1] = e1;
|
||||
elem_[2] = e2;
|
||||
elem_[3] = e3;
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
Vector<Dimension> Vector<Dimension>::Zero() {
|
||||
Vector<Dimension> v;
|
||||
return v;
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
void Vector<Dimension>::Add(const Vector& v) {
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
elem_[i] += v[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
void Vector<Dimension>::Subtract(const Vector& v) {
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
elem_[i] -= v[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
void Vector<Dimension>::Multiply(double s) {
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
elem_[i] *= s;
|
||||
}
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
void Vector<Dimension>::Divide(double s) {
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
elem_[i] /= s;
|
||||
}
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
Vector<Dimension> Vector<Dimension>::Negation() const {
|
||||
Vector<Dimension> ret;
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
ret.elem_[i] = -elem_[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
Vector<Dimension> Vector<Dimension>::Product(const Vector& v0, const Vector& v1) {
|
||||
Vector<Dimension> ret;
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
ret.elem_[i] = v0[i] * v1[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
Vector<Dimension> Vector<Dimension>::Sum(const Vector& v0, const Vector& v1) {
|
||||
Vector<Dimension> ret;
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
ret.elem_[i] = v0[i] + v1[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
Vector<Dimension> Vector<Dimension>::Difference(const Vector& v0, const Vector& v1) {
|
||||
Vector<Dimension> ret;
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
ret.elem_[i] = v0[i] - v1[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
Vector<Dimension> Vector<Dimension>::Scale(const Vector& v, double s) {
|
||||
Vector<Dimension> ret;
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
ret.elem_[i] = v[i] * s;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <int Dimension>
|
||||
Vector<Dimension> Vector<Dimension>::Divide(const Vector& v, double s) {
|
||||
Vector<Dimension> ret;
|
||||
for(int i = 0; i < Dimension; i++) {
|
||||
ret.elem_[i] = v[i] / s;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
typedef Vector<3> Vector3;
|
||||
typedef Vector<4> Vector4;
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_UTIL_VECTOR_H_
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "vectorutils.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// Returns the dot (inner) product of two Vectors.
|
||||
double Dot(const Vector<3>& v0, const Vector<3>& v1)
|
||||
{
|
||||
return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2];
|
||||
}
|
||||
|
||||
// Returns the dot (inner) product of two Vectors.
|
||||
double Dot(const Vector<4>& v0, const Vector<4>& v1)
|
||||
{
|
||||
return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2] + v0[3] * v1[3];
|
||||
}
|
||||
|
||||
// Returns the 3-dimensional cross product of 2 Vectors. Note that this is
|
||||
// defined only for 3-dimensional Vectors.
|
||||
Vector<3> Cross(const Vector<3>& v0, const Vector<3>& v1)
|
||||
{
|
||||
return Vector<3>(v0[1] * v1[2] - v0[2] * v1[1], v0[2] * v1[0] - v0[0] * v1[2],
|
||||
v0[0] * v1[1] - v0[1] * v1[0]);
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef CARDBOARD_SDK_UTIL_VECTORUTILS_H_
|
||||
#define CARDBOARD_SDK_UTIL_VECTORUTILS_H_
|
||||
|
||||
//
|
||||
// This file contains free functions that operate on Vector instances.
|
||||
//
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
namespace cardboard {
|
||||
|
||||
// Returns the dot (inner) product of two Vectors.
|
||||
double Dot(const Vector<3>& v0, const Vector<3>& v1);
|
||||
|
||||
// Returns the dot (inner) product of two Vectors.
|
||||
double Dot(const Vector<4>& v0, const Vector<4>& v1);
|
||||
|
||||
// Returns the 3-dimensional cross product of 2 Vectors. Note that this is
|
||||
// defined only for 3-dimensional Vectors.
|
||||
Vector<3> Cross(const Vector<3>& v0, const Vector<3>& v1);
|
||||
|
||||
// Returns the square of the length of a Vector.
|
||||
template <int Dimension>
|
||||
double LengthSquared(const Vector<Dimension>& v) {
|
||||
return Dot(v, v);
|
||||
}
|
||||
|
||||
// Returns the geometric length of a Vector.
|
||||
template <int Dimension>
|
||||
double Length(const Vector<Dimension>& v) {
|
||||
return sqrt(LengthSquared(v));
|
||||
}
|
||||
|
||||
// the Vector untouched and returns false.
|
||||
template <int Dimension>
|
||||
bool Normalize(Vector<Dimension>* v) {
|
||||
const double len = Length(*v);
|
||||
if(len == 0) {
|
||||
return false;
|
||||
} else {
|
||||
(*v) /= len;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a unit-length version of a Vector. If the given Vector has no
|
||||
// length, this returns a Zero() Vector.
|
||||
template <int Dimension>
|
||||
Vector<Dimension> Normalized(const Vector<Dimension>& v) {
|
||||
Vector<Dimension> result = v;
|
||||
if(Normalize(&result))
|
||||
return result;
|
||||
else
|
||||
return Vector<Dimension>::Zero();
|
||||
}
|
||||
|
||||
} // namespace cardboard
|
||||
|
||||
#endif // CARDBOARD_SDK_UTIL_VECTORUTILS_H_
|
||||
@@ -1,157 +0,0 @@
|
||||
#include "bt_mouse.h"
|
||||
#include "../tracking/main_loop.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal_bt.h>
|
||||
#include <furi_hal_bt_hid.h>
|
||||
#include <furi_hal_usb_hid.h>
|
||||
#include <bt/bt_service/bt.h>
|
||||
#include <gui/elements.h>
|
||||
#include <notification/notification.h>
|
||||
#include <notification/notification_messages.h>
|
||||
|
||||
struct BtMouse {
|
||||
View* view;
|
||||
ViewDispatcher* view_dispatcher;
|
||||
Bt* bt;
|
||||
NotificationApp* notifications;
|
||||
};
|
||||
|
||||
#define MOUSE_MOVE_SHORT 5
|
||||
#define MOUSE_MOVE_LONG 20
|
||||
|
||||
static void bt_mouse_draw_callback(Canvas* canvas, void* context) {
|
||||
UNUSED(context);
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 0, 10, "Bluetooth Mouse mode");
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 0, 63, "Hold [back] to exit");
|
||||
}
|
||||
|
||||
static void bt_mouse_process(BtMouse* bt_mouse, InputEvent* event) {
|
||||
with_view_model(
|
||||
bt_mouse->view,
|
||||
void* model,
|
||||
{
|
||||
UNUSED(model);
|
||||
if(event->key == InputKeyUp) {
|
||||
if(event->type == InputTypePress) {
|
||||
furi_hal_bt_hid_mouse_press(HID_MOUSE_BTN_LEFT);
|
||||
} else if(event->type == InputTypeRelease) {
|
||||
furi_hal_bt_hid_mouse_release(HID_MOUSE_BTN_LEFT);
|
||||
}
|
||||
} else if(event->key == InputKeyDown) {
|
||||
if(event->type == InputTypePress) {
|
||||
furi_hal_bt_hid_mouse_press(HID_MOUSE_BTN_RIGHT);
|
||||
} else if(event->type == InputTypeRelease) {
|
||||
furi_hal_bt_hid_mouse_release(HID_MOUSE_BTN_RIGHT);
|
||||
}
|
||||
} else if(event->key == InputKeyOk) {
|
||||
if(event->type == InputTypePress) {
|
||||
furi_hal_bt_hid_mouse_press(HID_MOUSE_BTN_WHEEL);
|
||||
} else if(event->type == InputTypeRelease) {
|
||||
furi_hal_bt_hid_mouse_release(HID_MOUSE_BTN_WHEEL);
|
||||
}
|
||||
}
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
static bool bt_mouse_input_callback(InputEvent* event, void* context) {
|
||||
furi_assert(context);
|
||||
BtMouse* bt_mouse = context;
|
||||
bool consumed = false;
|
||||
|
||||
if(event->type == InputTypeLong && event->key == InputKeyBack) {
|
||||
furi_hal_bt_hid_mouse_release_all();
|
||||
} else {
|
||||
bt_mouse_process(bt_mouse, event);
|
||||
consumed = true;
|
||||
}
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void bt_mouse_connection_status_changed_callback(BtStatus status, void* context) {
|
||||
furi_assert(context);
|
||||
BtMouse* bt_mouse = context;
|
||||
|
||||
bool connected = (status == BtStatusConnected);
|
||||
if(connected) {
|
||||
notification_internal_message(bt_mouse->notifications, &sequence_set_blue_255);
|
||||
} else {
|
||||
notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
|
||||
}
|
||||
|
||||
//with_view_model(
|
||||
// bt_mouse->view, void * model, { model->connected = connected; }, true);
|
||||
}
|
||||
|
||||
void bt_mouse_enter_callback(void* context) {
|
||||
furi_assert(context);
|
||||
BtMouse* bt_mouse = context;
|
||||
|
||||
bt_mouse->bt = furi_record_open(RECORD_BT);
|
||||
bt_mouse->notifications = furi_record_open(RECORD_NOTIFICATION);
|
||||
bt_set_status_changed_callback(
|
||||
bt_mouse->bt, bt_mouse_connection_status_changed_callback, bt_mouse);
|
||||
furi_assert(bt_set_profile(bt_mouse->bt, BtProfileHidKeyboard));
|
||||
furi_hal_bt_start_advertising();
|
||||
|
||||
tracking_begin();
|
||||
|
||||
view_dispatcher_send_custom_event(bt_mouse->view_dispatcher, 0);
|
||||
}
|
||||
|
||||
bool bt_mouse_custom_callback(uint32_t event, void* context) {
|
||||
UNUSED(event);
|
||||
furi_assert(context);
|
||||
BtMouse* bt_mouse = context;
|
||||
|
||||
tracking_step(furi_hal_bt_hid_mouse_move);
|
||||
|
||||
view_dispatcher_send_custom_event(bt_mouse->view_dispatcher, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
void bt_mouse_exit_callback(void* context) {
|
||||
furi_assert(context);
|
||||
BtMouse* bt_mouse = context;
|
||||
|
||||
tracking_end();
|
||||
|
||||
notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
|
||||
|
||||
furi_hal_bt_stop_advertising();
|
||||
bt_set_profile(bt_mouse->bt, BtProfileSerial);
|
||||
|
||||
furi_record_close(RECORD_NOTIFICATION);
|
||||
bt_mouse->notifications = NULL;
|
||||
furi_record_close(RECORD_BT);
|
||||
bt_mouse->bt = NULL;
|
||||
}
|
||||
|
||||
BtMouse* bt_mouse_alloc(ViewDispatcher* view_dispatcher) {
|
||||
BtMouse* bt_mouse = malloc(sizeof(BtMouse));
|
||||
bt_mouse->view = view_alloc();
|
||||
bt_mouse->view_dispatcher = view_dispatcher;
|
||||
view_set_context(bt_mouse->view, bt_mouse);
|
||||
view_set_draw_callback(bt_mouse->view, bt_mouse_draw_callback);
|
||||
view_set_input_callback(bt_mouse->view, bt_mouse_input_callback);
|
||||
view_set_enter_callback(bt_mouse->view, bt_mouse_enter_callback);
|
||||
view_set_custom_callback(bt_mouse->view, bt_mouse_custom_callback);
|
||||
view_set_exit_callback(bt_mouse->view, bt_mouse_exit_callback);
|
||||
return bt_mouse;
|
||||
}
|
||||
|
||||
void bt_mouse_free(BtMouse* bt_mouse) {
|
||||
furi_assert(bt_mouse);
|
||||
view_free(bt_mouse->view);
|
||||
free(bt_mouse);
|
||||
}
|
||||
|
||||
View* bt_mouse_get_view(BtMouse* bt_mouse) {
|
||||
furi_assert(bt_mouse);
|
||||
return bt_mouse->view;
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/view.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
|
||||
typedef struct BtMouse BtMouse;
|
||||
|
||||
BtMouse* bt_mouse_alloc(ViewDispatcher* view_dispatcher);
|
||||
|
||||
void bt_mouse_free(BtMouse* bt_mouse);
|
||||
|
||||
View* bt_mouse_get_view(BtMouse* bt_mouse);
|
||||
|
||||
void bt_mouse_set_connected_status(BtMouse* bt_mouse, bool connected);
|
||||
@@ -1,69 +0,0 @@
|
||||
#include "calibration.h"
|
||||
#include "../tracking/main_loop.h"
|
||||
#include "../air_mouse.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <gui/elements.h>
|
||||
|
||||
struct Calibration {
|
||||
View* view;
|
||||
ViewDispatcher* view_dispatcher;
|
||||
};
|
||||
|
||||
static void calibration_draw_callback(Canvas* canvas, void* context) {
|
||||
UNUSED(context);
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 0, 10, "Calibrating...");
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 0, 63, "Please wait");
|
||||
}
|
||||
|
||||
void calibration_enter_callback(void* context) {
|
||||
furi_assert(context);
|
||||
Calibration* calibration = context;
|
||||
calibration_begin();
|
||||
view_dispatcher_send_custom_event(calibration->view_dispatcher, 0);
|
||||
}
|
||||
|
||||
bool calibration_custom_callback(uint32_t event, void* context) {
|
||||
UNUSED(event);
|
||||
furi_assert(context);
|
||||
Calibration* calibration = context;
|
||||
|
||||
if(calibration_step()) {
|
||||
view_dispatcher_switch_to_view(calibration->view_dispatcher, AirMouseViewSubmenu);
|
||||
} else {
|
||||
view_dispatcher_send_custom_event(calibration->view_dispatcher, 0);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void calibration_exit_callback(void* context) {
|
||||
furi_assert(context);
|
||||
calibration_end();
|
||||
}
|
||||
|
||||
Calibration* calibration_alloc(ViewDispatcher* view_dispatcher) {
|
||||
Calibration* calibration = malloc(sizeof(Calibration));
|
||||
calibration->view = view_alloc();
|
||||
calibration->view_dispatcher = view_dispatcher;
|
||||
view_set_context(calibration->view, calibration);
|
||||
view_set_draw_callback(calibration->view, calibration_draw_callback);
|
||||
view_set_enter_callback(calibration->view, calibration_enter_callback);
|
||||
view_set_custom_callback(calibration->view, calibration_custom_callback);
|
||||
view_set_exit_callback(calibration->view, calibration_exit_callback);
|
||||
return calibration;
|
||||
}
|
||||
|
||||
void calibration_free(Calibration* calibration) {
|
||||
furi_assert(calibration);
|
||||
view_free(calibration->view);
|
||||
free(calibration);
|
||||
}
|
||||
|
||||
View* calibration_get_view(Calibration* calibration) {
|
||||
furi_assert(calibration);
|
||||
return calibration->view;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/view.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
|
||||
typedef struct Calibration Calibration;
|
||||
|
||||
Calibration* calibration_alloc(ViewDispatcher* view_dispatcher);
|
||||
|
||||
void calibration_free(Calibration* calibration);
|
||||
|
||||
View* calibration_get_view(Calibration* calibration);
|
||||
@@ -1,124 +0,0 @@
|
||||
#include "usb_mouse.h"
|
||||
#include "../tracking/main_loop.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal_usb.h>
|
||||
#include <furi_hal_usb_hid.h>
|
||||
#include <gui/elements.h>
|
||||
|
||||
struct UsbMouse {
|
||||
View* view;
|
||||
ViewDispatcher* view_dispatcher;
|
||||
FuriHalUsbInterface* usb_mode_prev;
|
||||
};
|
||||
|
||||
static void usb_mouse_draw_callback(Canvas* canvas, void* context) {
|
||||
UNUSED(context);
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 0, 10, "USB Mouse mode");
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 0, 63, "Hold [back] to exit");
|
||||
}
|
||||
|
||||
static void usb_mouse_process(UsbMouse* usb_mouse, InputEvent* event) {
|
||||
with_view_model(
|
||||
usb_mouse->view,
|
||||
void* model,
|
||||
{
|
||||
UNUSED(model);
|
||||
if(event->key == InputKeyUp) {
|
||||
if(event->type == InputTypePress) {
|
||||
furi_hal_hid_mouse_press(HID_MOUSE_BTN_LEFT);
|
||||
} else if(event->type == InputTypeRelease) {
|
||||
furi_hal_hid_mouse_release(HID_MOUSE_BTN_LEFT);
|
||||
}
|
||||
} else if(event->key == InputKeyDown) {
|
||||
if(event->type == InputTypePress) {
|
||||
furi_hal_hid_mouse_press(HID_MOUSE_BTN_RIGHT);
|
||||
} else if(event->type == InputTypeRelease) {
|
||||
furi_hal_hid_mouse_release(HID_MOUSE_BTN_RIGHT);
|
||||
}
|
||||
} else if(event->key == InputKeyOk) {
|
||||
if(event->type == InputTypePress) {
|
||||
furi_hal_hid_mouse_press(HID_MOUSE_BTN_WHEEL);
|
||||
} else if(event->type == InputTypeRelease) {
|
||||
furi_hal_hid_mouse_release(HID_MOUSE_BTN_WHEEL);
|
||||
}
|
||||
}
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
static bool usb_mouse_input_callback(InputEvent* event, void* context) {
|
||||
furi_assert(context);
|
||||
UsbMouse* usb_mouse = context;
|
||||
bool consumed = false;
|
||||
|
||||
if(event->type == InputTypeLong && event->key == InputKeyBack) {
|
||||
// furi_hal_hid_mouse_release_all();
|
||||
} else {
|
||||
usb_mouse_process(usb_mouse, event);
|
||||
consumed = true;
|
||||
}
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void usb_mouse_enter_callback(void* context) {
|
||||
furi_assert(context);
|
||||
UsbMouse* usb_mouse = context;
|
||||
|
||||
usb_mouse->usb_mode_prev = furi_hal_usb_get_config();
|
||||
furi_hal_usb_unlock();
|
||||
furi_check(furi_hal_usb_set_config(&usb_hid, NULL) == true);
|
||||
|
||||
tracking_begin();
|
||||
|
||||
view_dispatcher_send_custom_event(usb_mouse->view_dispatcher, 0);
|
||||
}
|
||||
|
||||
bool usb_mouse_custom_callback(uint32_t event, void* context) {
|
||||
UNUSED(event);
|
||||
furi_assert(context);
|
||||
UsbMouse* usb_mouse = context;
|
||||
|
||||
tracking_step(furi_hal_hid_mouse_move);
|
||||
furi_delay_ms(1); // Magic! Removing this will break the buttons
|
||||
|
||||
view_dispatcher_send_custom_event(usb_mouse->view_dispatcher, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
void usb_mouse_exit_callback(void* context) {
|
||||
furi_assert(context);
|
||||
UsbMouse* usb_mouse = context;
|
||||
|
||||
tracking_end();
|
||||
|
||||
furi_hal_usb_set_config(usb_mouse->usb_mode_prev, NULL);
|
||||
}
|
||||
|
||||
UsbMouse* usb_mouse_alloc(ViewDispatcher* view_dispatcher) {
|
||||
UsbMouse* usb_mouse = malloc(sizeof(UsbMouse));
|
||||
usb_mouse->view = view_alloc();
|
||||
usb_mouse->view_dispatcher = view_dispatcher;
|
||||
view_set_context(usb_mouse->view, usb_mouse);
|
||||
view_set_draw_callback(usb_mouse->view, usb_mouse_draw_callback);
|
||||
view_set_input_callback(usb_mouse->view, usb_mouse_input_callback);
|
||||
view_set_enter_callback(usb_mouse->view, usb_mouse_enter_callback);
|
||||
view_set_custom_callback(usb_mouse->view, usb_mouse_custom_callback);
|
||||
view_set_exit_callback(usb_mouse->view, usb_mouse_exit_callback);
|
||||
return usb_mouse;
|
||||
}
|
||||
|
||||
void usb_mouse_free(UsbMouse* usb_mouse) {
|
||||
furi_assert(usb_mouse);
|
||||
view_free(usb_mouse->view);
|
||||
free(usb_mouse);
|
||||
}
|
||||
|
||||
View* usb_mouse_get_view(UsbMouse* usb_mouse) {
|
||||
furi_assert(usb_mouse);
|
||||
return usb_mouse->view;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/view.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
|
||||
typedef struct UsbMouse UsbMouse;
|
||||
|
||||
UsbMouse* usb_mouse_alloc(ViewDispatcher* view_dispatcher);
|
||||
|
||||
void usb_mouse_free(UsbMouse* usb_mouse);
|
||||
|
||||
View* usb_mouse_get_view(UsbMouse* usb_mouse);
|
||||
@@ -1,14 +0,0 @@
|
||||
App(
|
||||
appid="AM2320_temp_sensor",
|
||||
name="[AM2320] Temp. Sensor",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="am_temperature_sensor_app",
|
||||
cdefines=["APP_AM_TEMPERATURE_SENSOR"],
|
||||
requires=[
|
||||
"gui",
|
||||
],
|
||||
stack_size=2 * 1024,
|
||||
order=90,
|
||||
fap_icon="temperature_sensor.png",
|
||||
fap_category="GPIO",
|
||||
)
|
||||
@@ -1,336 +0,0 @@
|
||||
/* Flipper Plugin to read the values from a AM2320/AM2321 Sensor */
|
||||
/* Created by @xMasterX, original app (was used as template) by Mywk - https://github.com/Mywk */
|
||||
/* Lib used as reference: https://github.com/Gozem/am2320/blob/master/am2321.c*/
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <furi_hal_i2c.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <gui/gui.h>
|
||||
#include <input/input.h>
|
||||
|
||||
#include <notification/notification_messages.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define TS_DEFAULT_VALUE 0xFFFF
|
||||
|
||||
#define AM2320_ADDRESS (0x5C << 1)
|
||||
|
||||
#define DATA_BUFFER_SIZE 8
|
||||
|
||||
// External I2C BUS
|
||||
#define I2C_BUS &furi_hal_i2c_handle_external
|
||||
|
||||
typedef enum {
|
||||
TSSInitializing,
|
||||
TSSNoSensor,
|
||||
TSSPendingUpdate,
|
||||
} TSStatus;
|
||||
|
||||
typedef enum {
|
||||
TSEventTypeTick,
|
||||
TSEventTypeInput,
|
||||
} TSEventType;
|
||||
|
||||
typedef struct {
|
||||
TSEventType type;
|
||||
InputEvent input;
|
||||
} TSEvent;
|
||||
|
||||
extern const NotificationSequence sequence_blink_red_100;
|
||||
extern const NotificationSequence sequence_blink_blue_100;
|
||||
|
||||
static TSStatus temperature_sensor_current_status = TSSInitializing;
|
||||
|
||||
// Temperature and Humidity data buffers, ready to print
|
||||
char ts_data_buffer_temperature_c[DATA_BUFFER_SIZE];
|
||||
char ts_data_buffer_temperature_f[DATA_BUFFER_SIZE];
|
||||
char ts_data_buffer_relative_humidity[DATA_BUFFER_SIZE];
|
||||
char ts_data_buffer_absolute_humidity[DATA_BUFFER_SIZE];
|
||||
|
||||
// CRC16 calculation
|
||||
static uint16_t get_crc16(const uint8_t* buf, size_t len) {
|
||||
uint16_t crc = 0xFFFF;
|
||||
|
||||
while(len--) {
|
||||
crc ^= (uint16_t)*buf++;
|
||||
for(unsigned i = 0; i < 8; i++) {
|
||||
if(crc & 0x0001) {
|
||||
crc >>= 1;
|
||||
crc ^= 0xA001;
|
||||
} else {
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
// Combine bytes
|
||||
static uint16_t combine_bytes(uint8_t msb, uint8_t lsb) {
|
||||
return ((uint16_t)msb << 8) | (uint16_t)lsb;
|
||||
}
|
||||
|
||||
// Executes an I2C wake up, sends command and reads result
|
||||
// true if fetch was successful, false otherwise
|
||||
static bool temperature_sensor_get_data(uint8_t* buffer, uint8_t size) {
|
||||
uint32_t timeout = furi_ms_to_ticks(100);
|
||||
uint8_t cmdbuffer[3] = {0, 0, 0};
|
||||
bool ret = false;
|
||||
|
||||
// Aquire I2C bus
|
||||
furi_hal_i2c_acquire(I2C_BUS);
|
||||
|
||||
// Wake UP AM2320 (sensor goes to sleep to not warm up and affect the humidity sensor)
|
||||
furi_hal_i2c_is_device_ready(I2C_BUS, (uint8_t)AM2320_ADDRESS, timeout);
|
||||
// Check if device woken up then we do next stuff
|
||||
if(furi_hal_i2c_is_device_ready(I2C_BUS, (uint8_t)AM2320_ADDRESS, timeout)) {
|
||||
// Wait a bit
|
||||
furi_delay_us(1000);
|
||||
|
||||
// Prepare command: Addr 0x03, start register = 0x00, number of registers to read = 0x04
|
||||
cmdbuffer[0] = 0x03;
|
||||
cmdbuffer[1] = 0x00;
|
||||
cmdbuffer[2] = 0x04;
|
||||
|
||||
// Transmit command to read registers
|
||||
ret = furi_hal_i2c_tx(I2C_BUS, (uint8_t)AM2320_ADDRESS, cmdbuffer, 3, timeout);
|
||||
|
||||
// Wait a bit
|
||||
furi_delay_us(1600);
|
||||
if(ret) {
|
||||
/*
|
||||
* Read out 8 bytes of data
|
||||
* Byte 0: Should be Modbus function code 0x03
|
||||
* Byte 1: Should be number of registers to read (0x04)
|
||||
* Byte 2: Humidity msb
|
||||
* Byte 3: Humidity lsb
|
||||
* Byte 4: Temperature msb
|
||||
* Byte 5: Temperature lsb
|
||||
* Byte 6: CRC lsb byte
|
||||
* Byte 7: CRC msb byte
|
||||
*/
|
||||
ret = furi_hal_i2c_rx(I2C_BUS, (uint8_t)AM2320_ADDRESS, buffer, size, timeout);
|
||||
}
|
||||
}
|
||||
// Release i2c bus
|
||||
furi_hal_i2c_release(I2C_BUS);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Fetches temperature and humidity from sensor
|
||||
// Temperature and humidity must be preallocated
|
||||
// true if fetch was successful, false otherwise
|
||||
static bool temperature_sensor_fetch_info(double* temperature, double* humidity) {
|
||||
*humidity = (float)0;
|
||||
bool ret = false;
|
||||
|
||||
uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
// Fetch data from sensor
|
||||
ret = temperature_sensor_get_data(buffer, 8);
|
||||
|
||||
// If we got no result
|
||||
if(!ret) return false;
|
||||
|
||||
if(buffer[0] != 0x03) return false; // must be 0x03 modbus reply
|
||||
if(buffer[1] != 0x04) return false; // must be 0x04 number of registers reply
|
||||
|
||||
// Check CRC16 sum, if not correct - return false
|
||||
uint16_t crcdata = get_crc16(buffer, 6);
|
||||
uint16_t crcread = combine_bytes(buffer[7], buffer[6]);
|
||||
if(crcdata != crcread) return false;
|
||||
|
||||
// Combine bytes for temp and humidity
|
||||
uint16_t temp16 = combine_bytes(buffer[4], buffer[5]);
|
||||
uint16_t humi16 = combine_bytes(buffer[2], buffer[3]);
|
||||
|
||||
/* Temperature resolution is 16Bit,
|
||||
* temperature highest bit (Bit15) is equal to 1 indicates a
|
||||
* negative temperature, the temperature highest bit (Bit15)
|
||||
* is equal to 0 indicates a positive temperature;
|
||||
* temperature in addition to the most significant bit (Bit14 ~ Bit0)
|
||||
* indicates the temperature sensor string value.
|
||||
* Temperature sensor value is a string of 10 times the
|
||||
* actual temperature value.
|
||||
*/
|
||||
if(temp16 & 0x8000) {
|
||||
temp16 = -(temp16 & 0x7FFF);
|
||||
}
|
||||
|
||||
// Prepare output data
|
||||
*temperature = (float)temp16 / 10.0;
|
||||
*humidity = (float)humi16 / 10.0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Draw callback
|
||||
|
||||
static void temperature_sensor_draw_callback(Canvas* canvas, void* ctx) {
|
||||
UNUSED(ctx);
|
||||
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 2, 10, "AM2320/AM2321 Sensor");
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 2, 62, "Press back to exit.");
|
||||
|
||||
switch(temperature_sensor_current_status) {
|
||||
case TSSInitializing:
|
||||
canvas_draw_str(canvas, 2, 30, "Initializing..");
|
||||
break;
|
||||
case TSSNoSensor:
|
||||
canvas_draw_str(canvas, 2, 30, "No sensor found!");
|
||||
break;
|
||||
case TSSPendingUpdate: {
|
||||
canvas_draw_str(canvas, 3, 24, "Temperature");
|
||||
canvas_draw_str(canvas, 68, 24, "Humidity");
|
||||
|
||||
// Draw vertical lines
|
||||
canvas_draw_line(canvas, 61, 16, 61, 50);
|
||||
canvas_draw_line(canvas, 62, 16, 62, 50);
|
||||
|
||||
// Draw horizontal line
|
||||
canvas_draw_line(canvas, 2, 27, 122, 27);
|
||||
|
||||
// Draw temperature and humidity values
|
||||
canvas_draw_str(canvas, 8, 38, ts_data_buffer_temperature_c);
|
||||
canvas_draw_str(canvas, 42, 38, "C");
|
||||
canvas_draw_str(canvas, 8, 48, ts_data_buffer_temperature_f);
|
||||
canvas_draw_str(canvas, 42, 48, "F");
|
||||
canvas_draw_str(canvas, 68, 38, ts_data_buffer_relative_humidity);
|
||||
canvas_draw_str(canvas, 100, 38, "%");
|
||||
canvas_draw_str(canvas, 68, 48, ts_data_buffer_absolute_humidity);
|
||||
canvas_draw_str(canvas, 100, 48, "g/m3");
|
||||
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Input callback
|
||||
|
||||
static void temperature_sensor_input_callback(InputEvent* input_event, void* ctx) {
|
||||
furi_assert(ctx);
|
||||
FuriMessageQueue* event_queue = ctx;
|
||||
|
||||
TSEvent event = {.type = TSEventTypeInput, .input = *input_event};
|
||||
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
||||
}
|
||||
|
||||
// Timer callback
|
||||
|
||||
static void temperature_sensor_timer_callback(FuriMessageQueue* event_queue) {
|
||||
furi_assert(event_queue);
|
||||
|
||||
TSEvent event = {.type = TSEventTypeTick};
|
||||
furi_message_queue_put(event_queue, &event, 0);
|
||||
}
|
||||
|
||||
// App entry point
|
||||
|
||||
int32_t am_temperature_sensor_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
furi_hal_power_suppress_charge_enter();
|
||||
// Declare our variables and assign variables a default value
|
||||
TSEvent tsEvent;
|
||||
bool sensorFound = false;
|
||||
double celsius, fahrenheit, rel_humidity, abs_humidity = TS_DEFAULT_VALUE;
|
||||
|
||||
// Used for absolute humidity calculation
|
||||
double vapour_pressure = 0;
|
||||
|
||||
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TSEvent));
|
||||
|
||||
// Register callbacks
|
||||
ViewPort* view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(view_port, temperature_sensor_draw_callback, NULL);
|
||||
view_port_input_callback_set(view_port, temperature_sensor_input_callback, event_queue);
|
||||
|
||||
// Create timer and register its callback
|
||||
FuriTimer* timer =
|
||||
furi_timer_alloc(temperature_sensor_timer_callback, FuriTimerTypePeriodic, event_queue);
|
||||
furi_timer_start(timer, furi_kernel_get_tick_frequency());
|
||||
|
||||
// Register viewport
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||
|
||||
// Used to notify the user by blinking red (error) or blue (fetch successful)
|
||||
NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
|
||||
|
||||
while(1) {
|
||||
furi_check(furi_message_queue_get(event_queue, &tsEvent, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
// Handle events
|
||||
if(tsEvent.type == TSEventTypeInput) {
|
||||
// Exit on back key
|
||||
if(tsEvent.input.key ==
|
||||
InputKeyBack) // We dont check for type here, we can check the type of keypress like: (event.input.type == InputTypeShort)
|
||||
break;
|
||||
|
||||
} else if(tsEvent.type == TSEventTypeTick) {
|
||||
// Update sensor data
|
||||
// Fetch data and set the sensor current status accordingly
|
||||
sensorFound = temperature_sensor_fetch_info(&celsius, &rel_humidity);
|
||||
temperature_sensor_current_status = (sensorFound ? TSSPendingUpdate : TSSNoSensor);
|
||||
|
||||
if(sensorFound) {
|
||||
// Blink blue
|
||||
notification_message(notifications, &sequence_blink_blue_100);
|
||||
|
||||
if(celsius != TS_DEFAULT_VALUE && rel_humidity != TS_DEFAULT_VALUE) {
|
||||
// Convert celsius to fahrenheit
|
||||
fahrenheit = (celsius * 9 / 5) + 32;
|
||||
|
||||
// Calculate absolute humidity - For more info refer to https://github.com/Mywk/FlipperTemperatureSensor/issues/1
|
||||
// Calculate saturation vapour pressure first
|
||||
vapour_pressure =
|
||||
(double)6.11 *
|
||||
pow(10, (double)(((double)7.5 * celsius) / ((double)237.3 + celsius)));
|
||||
// Then the vapour pressure in Pa
|
||||
vapour_pressure = vapour_pressure * rel_humidity;
|
||||
// Calculate absolute humidity
|
||||
abs_humidity =
|
||||
(double)2.16679 * (double)(vapour_pressure / ((double)273.15 + celsius));
|
||||
|
||||
// Fill our buffers here, not on the canvas draw callback
|
||||
snprintf(ts_data_buffer_temperature_c, DATA_BUFFER_SIZE, "%.2f", celsius);
|
||||
snprintf(ts_data_buffer_temperature_f, DATA_BUFFER_SIZE, "%.2f", fahrenheit);
|
||||
snprintf(
|
||||
ts_data_buffer_relative_humidity, DATA_BUFFER_SIZE, "%.2f", rel_humidity);
|
||||
snprintf(
|
||||
ts_data_buffer_absolute_humidity, DATA_BUFFER_SIZE, "%.2f", abs_humidity);
|
||||
}
|
||||
|
||||
} else {
|
||||
// Reset our variables to their default values
|
||||
celsius = fahrenheit = rel_humidity = abs_humidity = TS_DEFAULT_VALUE;
|
||||
|
||||
// Blink red
|
||||
notification_message(notifications, &sequence_blink_red_100);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t wait_ticks = furi_ms_to_ticks(!sensorFound ? 100 : 500);
|
||||
furi_delay_tick(wait_ticks);
|
||||
}
|
||||
|
||||
furi_hal_power_suppress_charge_exit();
|
||||
// Dobby is freee (free our variables, Flipper will crash if we don't do this!)
|
||||
furi_timer_free(timer);
|
||||
gui_remove_view_port(gui, view_port);
|
||||
view_port_free(view_port);
|
||||
furi_message_queue_free(event_queue);
|
||||
|
||||
furi_record_close(RECORD_NOTIFICATION);
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Before Width: | Height: | Size: 181 B |
@@ -1,9 +0,0 @@
|
||||
App(
|
||||
appid="BadApple",
|
||||
name="Bad Apple",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="bad_apple_main",
|
||||
requires=["gui"],
|
||||
stack_size=1 * 1024,
|
||||
fap_category="Misc"
|
||||
)
|
||||
@@ -1,183 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <stm32wbxx_ll_tim.h>
|
||||
|
||||
#include <gui/gui.h>
|
||||
#include <input/input.h>
|
||||
#include <storage/storage.h>
|
||||
#include <notification/notification.h>
|
||||
#include <notification/notification_messages.h>
|
||||
|
||||
#include "bad_apple.h"
|
||||
#include "video_player.h"
|
||||
|
||||
#define TAG "badapple"
|
||||
|
||||
typedef enum {
|
||||
BadAppleEventTypeInput,
|
||||
BadAppleEventTypeTick,
|
||||
} BadAppleEventType;
|
||||
|
||||
typedef struct {
|
||||
BadAppleEventType type;
|
||||
InputEvent* input;
|
||||
} BadAppleEvent;
|
||||
|
||||
// Screen is 128x64 px
|
||||
static void app_draw_callback(Canvas* canvas, void* ctx) {
|
||||
BadAppleCtx* inst = ctx;
|
||||
|
||||
canvas_clear(canvas);
|
||||
canvas_draw_xbm(canvas, VIDEO_X, VIDEO_Y, VIDEO_WIDTH, SCREEN_HEIGHT, inst->framebuffer);
|
||||
canvas_draw_box(canvas, 0, 0, VIDEO_X, SCREEN_HEIGHT);
|
||||
canvas_draw_box(
|
||||
canvas, VIDEO_X + VIDEO_WIDTH, 0, SCREEN_WIDTH - VIDEO_WIDTH - VIDEO_X, SCREEN_HEIGHT);
|
||||
}
|
||||
|
||||
static void app_input_callback(InputEvent* input_event, void* ctx) {
|
||||
furi_assert(ctx);
|
||||
|
||||
FuriMessageQueue* event_queue = ctx;
|
||||
BadAppleEvent event = {.type = BadAppleEventTypeInput, .input = input_event};
|
||||
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
||||
}
|
||||
|
||||
BadAppleCtx* bad_apple_ctx_alloc(void) {
|
||||
BadAppleCtx* inst = malloc(sizeof(BadAppleCtx));
|
||||
memset(inst, 0, sizeof(BadAppleCtx));
|
||||
|
||||
if(inst) {
|
||||
inst->storage = furi_record_open(RECORD_STORAGE);
|
||||
inst->video_file = storage_file_alloc(inst->storage);
|
||||
inst->file_buffer_offset = sizeof(inst->file_buffer);
|
||||
}
|
||||
|
||||
return inst;
|
||||
}
|
||||
|
||||
void bad_apple_ctx_free(BadAppleCtx* inst) {
|
||||
if(inst) {
|
||||
storage_file_free(inst->video_file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
free(inst);
|
||||
}
|
||||
}
|
||||
|
||||
void bad_apple_load_next_video_chunk(BadAppleCtx* inst) {
|
||||
size_t bytes_to_read = sizeof(inst->file_buffer);
|
||||
uint8_t* buf_ptr = inst->file_buffer;
|
||||
while(bytes_to_read > 0) {
|
||||
uint16_t curr_bytes_to_read = bytes_to_read > (UINT16_MAX / 2 + 1) ? (UINT16_MAX / 2 + 1) :
|
||||
bytes_to_read;
|
||||
uint16_t read = storage_file_read(inst->video_file, buf_ptr, curr_bytes_to_read);
|
||||
bytes_to_read -= read;
|
||||
buf_ptr += read;
|
||||
if(read == 0) break;
|
||||
}
|
||||
inst->file_buffer_offset = 0;
|
||||
}
|
||||
|
||||
uint8_t bad_apple_read_byte(BadAppleCtx* inst) {
|
||||
if(inst->file_buffer_offset >= sizeof(inst->file_buffer)) {
|
||||
bad_apple_load_next_video_chunk(inst);
|
||||
}
|
||||
return inst->file_buffer[inst->file_buffer_offset++];
|
||||
}
|
||||
|
||||
void bad_apple_timer_isr(void* ctx) {
|
||||
FuriMessageQueue* event_queue = ctx;
|
||||
BadAppleEvent event = {.type = BadAppleEventTypeTick};
|
||||
furi_message_queue_put(event_queue, &event, 0);
|
||||
LL_TIM_ClearFlag_UPDATE(TIM2);
|
||||
}
|
||||
|
||||
void bad_apple_timer_setup(BadAppleCtx* inst, void* ctx) {
|
||||
UNUSED(inst);
|
||||
|
||||
LL_TIM_InitTypeDef tim_init = {
|
||||
.Prescaler = 63999,
|
||||
.CounterMode = LL_TIM_COUNTERMODE_UP,
|
||||
.Autoreload = 30,
|
||||
};
|
||||
|
||||
LL_TIM_Init(TIM2, &tim_init);
|
||||
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
|
||||
LL_TIM_DisableCounter(TIM2);
|
||||
LL_TIM_SetCounter(TIM2, 0);
|
||||
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, bad_apple_timer_isr, ctx);
|
||||
LL_TIM_EnableIT_UPDATE(TIM2);
|
||||
}
|
||||
|
||||
void bad_apple_timer_deinit(void) {
|
||||
LL_TIM_DisableCounter(TIM2);
|
||||
LL_TIM_DisableIT_UPDATE(TIM2);
|
||||
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
|
||||
LL_TIM_DeInit(TIM2);
|
||||
}
|
||||
|
||||
int32_t bad_apple_main(void* p) {
|
||||
UNUSED(p);
|
||||
BadAppleCtx* ctx = bad_apple_ctx_alloc();
|
||||
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(BadAppleEvent));
|
||||
|
||||
// Configure view port
|
||||
ViewPort* view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(view_port, app_draw_callback, ctx);
|
||||
view_port_input_callback_set(view_port, app_input_callback, event_queue);
|
||||
|
||||
// Register view port in GUI
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||
|
||||
NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
|
||||
|
||||
// Frame rate: 32 FPS
|
||||
bad_apple_timer_setup(ctx, event_queue);
|
||||
|
||||
bool is_opened = storage_file_open(ctx->video_file, VIDEO_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
||||
if(is_opened) {
|
||||
BadAppleEvent event;
|
||||
notification_message(notification, &sequence_display_backlight_enforce_on);
|
||||
|
||||
bool running = true;
|
||||
LL_TIM_EnableCounter(TIM2);
|
||||
while(running) {
|
||||
if(furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
|
||||
if(event.type == BadAppleEventTypeInput) {
|
||||
InputEvent* input_event = event.input;
|
||||
if(input_event->type == InputTypeLong) {
|
||||
if(input_event->key == InputKeyBack) {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
} else if(event.type == BadAppleEventTypeTick) {
|
||||
// FURI_LOG_D(TAG, "Update frame");
|
||||
if(!vp_play_frame(ctx)) {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
view_port_update(view_port);
|
||||
}
|
||||
LL_TIM_DisableCounter(TIM2);
|
||||
notification_message(notification, &sequence_display_backlight_enforce_auto);
|
||||
storage_file_close(ctx->video_file);
|
||||
}
|
||||
|
||||
furi_record_close(RECORD_NOTIFICATION);
|
||||
|
||||
view_port_enabled_set(view_port, false);
|
||||
gui_remove_view_port(gui, view_port);
|
||||
view_port_free(view_port);
|
||||
furi_message_queue_free(event_queue);
|
||||
|
||||
furi_record_close(RECORD_GUI);
|
||||
bad_apple_timer_deinit();
|
||||
bad_apple_ctx_free(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <storage/storage.h>
|
||||
|
||||
#define SCREEN_WIDTH 128
|
||||
#define SCREEN_HEIGHT 64
|
||||
#define VIDEO_WIDTH 104
|
||||
#define VIDEO_HEIGHT 80
|
||||
#define FILE_BUFFER_SIZE (1024 * 64)
|
||||
#define VIDEO_PATH EXT_PATH("apps_data/bad_apple/video.bin")
|
||||
#define VIDEO_X ((SCREEN_WIDTH - VIDEO_WIDTH) / 2)
|
||||
#define VIDEO_Y 0
|
||||
|
||||
typedef struct {
|
||||
uint8_t framebuffer[VIDEO_HEIGHT * VIDEO_WIDTH / 8];
|
||||
uint8_t file_buffer[FILE_BUFFER_SIZE];
|
||||
uint32_t file_buffer_offset;
|
||||
uint32_t frame_write_offset; // bit index
|
||||
Storage* storage;
|
||||
File* video_file;
|
||||
} BadAppleCtx;
|
||||
|
||||
uint8_t bad_apple_read_byte(BadAppleCtx* inst);
|
||||
@@ -1,137 +0,0 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#include "bad_apple.h"
|
||||
#include "video_player.h"
|
||||
|
||||
#define TAG "video_player"
|
||||
|
||||
static void vp_decode_rle(BadAppleCtx* ctx);
|
||||
static void vp_decode_delta(BadAppleCtx* ctx);
|
||||
|
||||
int vp_play_frame(BadAppleCtx* ctx) {
|
||||
// Check frame type
|
||||
// FURI_LOG_D(TAG, "Buffer offset: %04lx", ctx->file_buffer_offset);
|
||||
uint8_t b = bad_apple_read_byte(ctx);
|
||||
// FURI_LOG_D(TAG, "Frame byte: %02x", b);
|
||||
switch(b) {
|
||||
case 1: // PFrame (delta)
|
||||
vp_decode_delta(ctx);
|
||||
break;
|
||||
case 2: // IFrame (rle)
|
||||
vp_decode_rle(ctx);
|
||||
break;
|
||||
case 3: // DFrame (duplicate)
|
||||
break;
|
||||
case 4: // next page (not supported)
|
||||
case 5: // end
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline void vp_write_pixel(BadAppleCtx* ctx, bool color) {
|
||||
if(color) {
|
||||
// White
|
||||
ctx->framebuffer[ctx->frame_write_offset / 8] &= ~(1 << (ctx->frame_write_offset % 8));
|
||||
} else {
|
||||
// Black
|
||||
ctx->framebuffer[ctx->frame_write_offset / 8] |= (1 << (ctx->frame_write_offset % 8));
|
||||
}
|
||||
++ctx->frame_write_offset;
|
||||
}
|
||||
|
||||
static inline void vp_set_rect_fast(BadAppleCtx* ctx, int x, int y) {
|
||||
ctx->frame_write_offset = y * VIDEO_WIDTH + x;
|
||||
}
|
||||
|
||||
static void vp_decode_rle(BadAppleCtx* ctx) {
|
||||
int i = 0;
|
||||
int repeat_byte = bad_apple_read_byte(ctx);
|
||||
|
||||
// Set rect to video area
|
||||
vp_set_rect_fast(ctx, 0, 0);
|
||||
|
||||
while(i < VIDEO_WIDTH * VIDEO_HEIGHT) {
|
||||
int count;
|
||||
unsigned pixels;
|
||||
int j;
|
||||
int b = bad_apple_read_byte(ctx);
|
||||
|
||||
if(b == repeat_byte) {
|
||||
count = bad_apple_read_byte(ctx);
|
||||
if(count == 0) count = 256;
|
||||
pixels = bad_apple_read_byte(ctx);
|
||||
} else {
|
||||
count = 1;
|
||||
pixels = (unsigned)b;
|
||||
}
|
||||
|
||||
for(j = 0; j < count; ++j) {
|
||||
bool out_color;
|
||||
int k;
|
||||
unsigned loop_pixels = pixels;
|
||||
|
||||
for(k = 0; k < 8; ++k) {
|
||||
if(loop_pixels & 0x80)
|
||||
out_color = COLOR_WHITE;
|
||||
else
|
||||
out_color = COLOR_BLACK;
|
||||
|
||||
vp_write_pixel(ctx, out_color);
|
||||
loop_pixels <<= 1;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void vp_decode_delta(BadAppleCtx* ctx) {
|
||||
int frame_header[(VIDEO_HEIGHT + 7) / 8];
|
||||
uint i;
|
||||
int fh_byte = 0;
|
||||
int fh_index = 0;
|
||||
|
||||
for(i = 0; i < sizeof(frame_header) / sizeof(frame_header[0]); ++i) {
|
||||
frame_header[i] = bad_apple_read_byte(ctx);
|
||||
}
|
||||
|
||||
for(i = 0; i < VIDEO_HEIGHT; ++i) {
|
||||
if(i % 8 == 0) fh_byte = frame_header[fh_index++];
|
||||
|
||||
if(fh_byte & 0x80) {
|
||||
int j;
|
||||
int sl_byte = 0;
|
||||
|
||||
for(j = 0; j < VIDEO_WIDTH;) {
|
||||
if(j % (8 * 8) == 0) sl_byte = bad_apple_read_byte(ctx);
|
||||
|
||||
if(sl_byte & 0x80) {
|
||||
unsigned out_color;
|
||||
int k;
|
||||
unsigned pixel_group = bad_apple_read_byte(ctx);
|
||||
|
||||
// Note: this needs to be revised for screen width not multiple of 8
|
||||
vp_set_rect_fast(ctx, j, i);
|
||||
|
||||
for(k = 0; k < 8 && j < VIDEO_WIDTH; ++k, ++j) {
|
||||
if(pixel_group & 0x80)
|
||||
out_color = COLOR_WHITE;
|
||||
else
|
||||
out_color = COLOR_BLACK;
|
||||
|
||||
vp_write_pixel(ctx, out_color);
|
||||
pixel_group <<= 1;
|
||||
}
|
||||
} else {
|
||||
j += 8;
|
||||
}
|
||||
sl_byte <<= 1;
|
||||
}
|
||||
}
|
||||
fh_byte <<= 1;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#define COLOR_WHITE true
|
||||
#define COLOR_BLACK false
|
||||
|
||||
int vp_play_frame(BadAppleCtx* ctx);
|
||||
@@ -1,674 +0,0 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
@@ -1,14 +0,0 @@
|
||||
# BPM Tapper
|
||||
|
||||
A BPM Tapper for the Flipper Zero.
|
||||
|
||||

|
||||
|
||||
Hit any button other than back repeatedly. Calculates based on the average of the last 8 inputs.
|
||||
|
||||
## Compiling
|
||||
|
||||
```
|
||||
./fbt firmware_bpm_tapper
|
||||
```
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
App(
|
||||
appid="BPM_Tapper",
|
||||
name="BPM Tapper",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="bpm_tapper_app",
|
||||
cdefines=["APP_BPM_TAPPER"],
|
||||
requires=["gui"],
|
||||
stack_size=2 * 1024,
|
||||
fap_icon="bpm_10px.png",
|
||||
fap_category="Music",
|
||||
fap_icon_assets="icons",
|
||||
order=15,
|
||||
)
|
||||
@@ -1,262 +0,0 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <dialogs/dialogs.h>
|
||||
#include <gui/gui.h>
|
||||
#include <input/input.h>
|
||||
#include <stdlib.h>
|
||||
#include "BPM_Tapper_icons.h"
|
||||
|
||||
typedef enum {
|
||||
EventTypeTick,
|
||||
EventTypeKey,
|
||||
} EventType;
|
||||
|
||||
typedef struct {
|
||||
EventType type;
|
||||
InputEvent input;
|
||||
} PluginEvent;
|
||||
|
||||
//QUEUE
|
||||
|
||||
struct node {
|
||||
int interval;
|
||||
struct node* next;
|
||||
};
|
||||
typedef struct node node;
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
int max_size;
|
||||
node* front;
|
||||
node* rear;
|
||||
} queue;
|
||||
|
||||
static void init_queue(queue* q) {
|
||||
q->size = 0;
|
||||
q->max_size = 8;
|
||||
q->front = NULL;
|
||||
q->rear = NULL;
|
||||
}
|
||||
|
||||
static void queue_remove(queue* q) {
|
||||
node* tmp;
|
||||
tmp = q->front;
|
||||
q->front = q->front->next;
|
||||
q->size--;
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
static void queue_add(queue* q, int value) {
|
||||
node* tmp = malloc(sizeof(node));
|
||||
tmp->interval = value;
|
||||
tmp->next = NULL;
|
||||
if(q->size == q->max_size) {
|
||||
queue_remove(q);
|
||||
}
|
||||
// check if empty
|
||||
if(q->rear == NULL) {
|
||||
q->front = tmp;
|
||||
q->rear = tmp;
|
||||
} else {
|
||||
q->rear->next = tmp;
|
||||
q->rear = tmp;
|
||||
}
|
||||
q->size++;
|
||||
}
|
||||
|
||||
static float queue_avg(queue* q) {
|
||||
float avg = 0.0;
|
||||
if(q->size == 0) {
|
||||
return avg;
|
||||
} else {
|
||||
node* tmp;
|
||||
float sum = 0.0;
|
||||
tmp = q->front;
|
||||
while(tmp != NULL) {
|
||||
sum = sum + tmp->interval;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
avg = sum / q->size;
|
||||
FURI_LOG_D("BPM-Tapper", "Sum: %.2f Avg: %.2f", (double)sum, (double)avg);
|
||||
return avg;
|
||||
}
|
||||
}
|
||||
|
||||
// TOO SLOW!
|
||||
//uint64_t dolphin_state_timestamp() {
|
||||
// FuriHalRtcDateTime datetime;
|
||||
// furi_hal_rtc_get_datetime(&datetime);
|
||||
// return furi_hal_rtc_datetime_to_timestamp(&datetime);
|
||||
//}
|
||||
//
|
||||
typedef struct {
|
||||
int taps;
|
||||
double bpm;
|
||||
uint32_t last_stamp;
|
||||
uint32_t interval;
|
||||
queue* tap_queue;
|
||||
} BPMTapper;
|
||||
|
||||
static void show_hello() {
|
||||
// BEGIN HELLO DIALOG
|
||||
DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
|
||||
DialogMessage* message = dialog_message_alloc();
|
||||
|
||||
const char* header_text = "BPM Tapper";
|
||||
const char* message_text = "Tap center to start";
|
||||
|
||||
dialog_message_set_header(message, header_text, 63, 3, AlignCenter, AlignTop);
|
||||
dialog_message_set_text(message, message_text, 0, 17, AlignLeft, AlignTop);
|
||||
dialog_message_set_buttons(message, NULL, "Tap", NULL);
|
||||
|
||||
dialog_message_set_icon(message, &I_DolphinCommon_56x48, 72, 17);
|
||||
|
||||
dialog_message_show(dialogs, message);
|
||||
|
||||
dialog_message_free(message);
|
||||
furi_record_close(RECORD_DIALOGS);
|
||||
// END HELLO DIALOG
|
||||
}
|
||||
|
||||
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
|
||||
furi_assert(event_queue);
|
||||
|
||||
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
|
||||
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
||||
}
|
||||
|
||||
static void render_callback(Canvas* const canvas, void* ctx) {
|
||||
FuriString* tempStr;
|
||||
|
||||
const BPMTapper* bpm_state = acquire_mutex((ValueMutex*)ctx, 25);
|
||||
if(bpm_state == NULL) {
|
||||
return;
|
||||
}
|
||||
// border
|
||||
//canvas_draw_frame(canvas, 0, 0, 128, 64);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
|
||||
tempStr = furi_string_alloc();
|
||||
|
||||
furi_string_printf(tempStr, "Taps: %d", bpm_state->taps);
|
||||
canvas_draw_str_aligned(canvas, 5, 10, AlignLeft, AlignBottom, furi_string_get_cstr(tempStr));
|
||||
furi_string_reset(tempStr);
|
||||
|
||||
furi_string_printf(tempStr, "Queue: %d", bpm_state->tap_queue->size);
|
||||
canvas_draw_str_aligned(canvas, 70, 10, AlignLeft, AlignBottom, furi_string_get_cstr(tempStr));
|
||||
furi_string_reset(tempStr);
|
||||
|
||||
furi_string_printf(tempStr, "Interval: %ldms", bpm_state->interval);
|
||||
canvas_draw_str_aligned(canvas, 5, 20, AlignLeft, AlignBottom, furi_string_get_cstr(tempStr));
|
||||
furi_string_reset(tempStr);
|
||||
|
||||
furi_string_printf(tempStr, "x2 %.2f /2 %.2f", bpm_state->bpm * 2, bpm_state->bpm / 2);
|
||||
canvas_draw_str_aligned(
|
||||
canvas, 64, 60, AlignCenter, AlignCenter, furi_string_get_cstr(tempStr));
|
||||
furi_string_reset(tempStr);
|
||||
|
||||
furi_string_printf(tempStr, "%.2f", bpm_state->bpm);
|
||||
canvas_set_font(canvas, FontBigNumbers);
|
||||
canvas_draw_str_aligned(
|
||||
canvas, 64, 40, AlignCenter, AlignCenter, furi_string_get_cstr(tempStr));
|
||||
furi_string_reset(tempStr);
|
||||
|
||||
furi_string_free(tempStr);
|
||||
|
||||
release_mutex((ValueMutex*)ctx, bpm_state);
|
||||
}
|
||||
|
||||
static void bpm_state_init(BPMTapper* const plugin_state) {
|
||||
plugin_state->taps = 0;
|
||||
plugin_state->bpm = 120.0;
|
||||
plugin_state->last_stamp = 0; // furi_get_tick();
|
||||
plugin_state->interval = 0;
|
||||
queue* q;
|
||||
q = malloc(sizeof(queue));
|
||||
init_queue(q);
|
||||
plugin_state->tap_queue = q;
|
||||
}
|
||||
|
||||
int32_t bpm_tapper_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
|
||||
|
||||
BPMTapper* bpm_state = malloc(sizeof(BPMTapper));
|
||||
// setup
|
||||
bpm_state_init(bpm_state);
|
||||
|
||||
ValueMutex state_mutex;
|
||||
if(!init_mutex(&state_mutex, bpm_state, sizeof(bpm_state))) {
|
||||
FURI_LOG_E("BPM-Tapper", "cannot create mutex\r\n");
|
||||
free(bpm_state);
|
||||
return 255;
|
||||
}
|
||||
show_hello();
|
||||
|
||||
// BEGIN IMPLEMENTATION
|
||||
|
||||
// Set system callbacks
|
||||
ViewPort* view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(view_port, render_callback, &state_mutex);
|
||||
view_port_input_callback_set(view_port, input_callback, event_queue);
|
||||
|
||||
// Open GUI and register view_port
|
||||
Gui* gui = furi_record_open("gui");
|
||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||
|
||||
PluginEvent event;
|
||||
for(bool processing = true; processing;) {
|
||||
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
|
||||
BPMTapper* bpm_state = (BPMTapper*)acquire_mutex_block(&state_mutex);
|
||||
if(event_status == FuriStatusOk) {
|
||||
// press events
|
||||
if(event.type == EventTypeKey) {
|
||||
if(event.input.type == InputTypePress) {
|
||||
switch(event.input.key) {
|
||||
case InputKeyUp:
|
||||
case InputKeyDown:
|
||||
case InputKeyRight:
|
||||
case InputKeyLeft:
|
||||
case InputKeyOk:
|
||||
bpm_state->taps++;
|
||||
uint32_t new_stamp = furi_get_tick();
|
||||
if(bpm_state->last_stamp == 0) {
|
||||
bpm_state->last_stamp = new_stamp;
|
||||
break;
|
||||
}
|
||||
bpm_state->interval = new_stamp - bpm_state->last_stamp;
|
||||
bpm_state->last_stamp = new_stamp;
|
||||
queue_add(bpm_state->tap_queue, bpm_state->interval);
|
||||
float avg = queue_avg(bpm_state->tap_queue);
|
||||
float bps = 1.0 / (avg / 1000.0);
|
||||
bpm_state->bpm = bps * 60.0;
|
||||
break;
|
||||
case InputKeyBack:
|
||||
// Exit the plugin
|
||||
processing = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_D("BPM-Tapper", "FuriMessageQueue: event timeout");
|
||||
// event timeout
|
||||
}
|
||||
view_port_update(view_port);
|
||||
release_mutex(&state_mutex, bpm_state);
|
||||
}
|
||||
view_port_enabled_set(view_port, false);
|
||||
gui_remove_view_port(gui, view_port);
|
||||
furi_record_close("gui");
|
||||
view_port_free(view_port);
|
||||
furi_message_queue_free(event_queue);
|
||||
delete_mutex(&state_mutex);
|
||||
queue* q = bpm_state->tap_queue;
|
||||
free(q);
|
||||
free(bpm_state);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Before Width: | Height: | Size: 181 B |
|
Before Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 1.9 KiB |
@@ -1,12 +0,0 @@
|
||||
App(
|
||||
appid="zBroken_Chess",
|
||||
name="Chess",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="chess_app",
|
||||
cdefines=["APP_CHESS"],
|
||||
requires=["storage","gui"],
|
||||
stack_size= 4 * 1024,
|
||||
order=500,
|
||||
fap_icon="chessIcon.png",
|
||||
fap_category="Games",
|
||||
)
|
||||
|
Before Width: | Height: | Size: 1.9 KiB |
@@ -1,686 +0,0 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <gui/gui.h>
|
||||
#include <input/input.h>
|
||||
#include <notification/notification_messages.h>
|
||||
|
||||
#include <gui/icon_i.h>
|
||||
#include "fast_chess.h"
|
||||
|
||||
static bool flag = true;
|
||||
static bool should_exit = false;
|
||||
// static bool ai_should_make_move = false;
|
||||
static bool thinking = false;
|
||||
static bool should_update_screen = true;
|
||||
static uint32_t anim = 0;
|
||||
static char white_move_str[8] = "", black_move_str[8] = ""; // last moves
|
||||
|
||||
static NotificationApp* notification;
|
||||
|
||||
const uint8_t _I_Chess_0[] = {
|
||||
0x01, 0x00, 0x2a, 0x01, 0x80, 0x7f, 0xc0, 0x2c, 0x0f, 0xf0, 0x7f, 0x83, 0xfc, 0x1f, 0xe0, 0xff,
|
||||
0x07, 0xf8, 0x3f, 0xc1, 0xde, 0x0f, 0xf0, 0x7f, 0x83, 0xfc, 0x1f, 0xe0, 0xff, 0x07, 0xf8, 0x3f,
|
||||
0xc2, 0x1e, 0x0f, 0xf0, 0x7f, 0x80, 0x07, 0x00, 0x0f, 0xf1, 0x7f, 0xc0, 0x41, 0xf8, 0x1e, 0x18,
|
||||
0x0f, 0xf2, 0xfe, 0x1f, 0xb8, 0x0e, 0x0a, 0x07, 0x80, 0x81, 0x83, 0xea, 0x05, 0x62, 0x01, 0x8c,
|
||||
0x30, 0x7d, 0x50, 0x48, 0x80, 0x0c, 0x66, 0x00, 0xfa, 0x84, 0x42, 0x00, 0x63, 0x40, 0x07, 0xd4,
|
||||
0x42, 0x08, 0x54, 0x24, 0xf5, 0xc0, 0x8f, 0xd9, 0x70, 0x3f, 0xa2, 0x7a, 0xb0, 0x69, 0xee, 0x57,
|
||||
0xa0, 0x3e, 0xa8, 0x0b, 0xfc, 0xc0, 0x4f, 0xc1, 0xe5, 0x3c, 0x07, 0xcd, 0x03, 0x81, 0x41, 0x06,
|
||||
0x0f, 0x0c, 0x1f, 0x32, 0x08, 0x04, 0x98, 0x46, 0x31, 0xf3, 0x10, 0x83, 0xdd, 0x58, 0x33, 0x88,
|
||||
0x07, 0x02, 0xfe, 0x82, 0x10, 0x7c, 0x88, 0x47, 0x81, 0x73, 0x07, 0xcf, 0x42, 0x07, 0xc0, 0x80,
|
||||
0x78, 0x3e, 0x2b, 0x21, 0x07, 0xbf, 0xc2, 0x1f, 0x00, 0x81, 0x83, 0xef, 0xc1, 0x1f, 0x7e, 0x0f,
|
||||
0x83, 0xff, 0x04, 0x47, 0xc7, 0x82, 0x7e, 0x42, 0x10, 0x7d, 0x96, 0xc4, 0x06, 0x71, 0x60, 0x7c,
|
||||
0x3e, 0x48, 0x1e, 0x52, 0xa5, 0xfc, 0xff, 0xd1, 0x62, 0x8f, 0x1a, 0xa8, 0x3e, 0x7f, 0xd0, 0x31,
|
||||
0x19, 0x07, 0xeb, 0xf9, 0x07, 0x80, 0x58, 0x2c, 0x01, 0xfa, 0xfc, 0x20, 0x06, 0x21, 0x80, 0x0f,
|
||||
0xd7, 0xc1, 0x00, 0x20, 0x01, 0xaa, 0xa3, 0xe1, 0x00, 0x60, 0x01, 0x95, 0x03, 0xe7, 0x80, 0x24,
|
||||
0x38, 0xa0, 0x3e, 0x7f, 0x1c, 0x73, 0x00, 0x9d, 0x8c, 0x02, 0xdc, 0x0d, 0x7c, 0x04, 0xa0, 0x2e,
|
||||
0xe1, 0x07, 0xc5, 0xc2, 0xeb, 0x00, 0x9f, 0x40, 0x12, 0x42, 0x0f, 0x8d, 0xc4, 0x69, 0x22, 0x3c,
|
||||
0x11, 0x7d, 0x5e, 0x20, 0xa0, 0x41, 0x30, 0x98, 0x3d, 0xf1, 0x1f, 0xff, 0xfa, 0x00, 0xcb, 0xd1,
|
||||
0x10, 0x2e, 0x18, 0x3e, 0xa4, 0x00, 0xfe, 0xa0, 0x03, 0xfb, 0x00, 0x6b, 0x20, 0x7d, 0xc0, 0x21,
|
||||
0xc0, 0xfe, 0xf8, 0x3f, 0xc4, 0x1f, 0x90, 0x0f, 0xe0, 0x40, 0xc1, 0xf6, 0x05, 0x30,
|
||||
};
|
||||
const uint8_t* const _I_Chess[] = {_I_Chess_0};
|
||||
|
||||
const uint8_t _I_Chess_Selection1_0[] = {
|
||||
0x00,
|
||||
0x55,
|
||||
0x80,
|
||||
0x01,
|
||||
0x80,
|
||||
0x01,
|
||||
0x80,
|
||||
0x01,
|
||||
0xAA,
|
||||
};
|
||||
const uint8_t* const _I_Chess_Selection1[] = {_I_Chess_Selection1_0};
|
||||
|
||||
const uint8_t _I_Chess_Selection2_0[] = {
|
||||
0x00,
|
||||
0xAA,
|
||||
0x01,
|
||||
0x80,
|
||||
0x01,
|
||||
0x80,
|
||||
0x01,
|
||||
0x80,
|
||||
0x55,
|
||||
};
|
||||
const uint8_t* const _I_Chess_Selection2[] = {_I_Chess_Selection2_0};
|
||||
|
||||
const uint8_t _I_Chess_bb_0[] = {
|
||||
0x00,
|
||||
0x0C,
|
||||
0x1A,
|
||||
0x3D,
|
||||
0x1E,
|
||||
0x0C,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_bb[] = {_I_Chess_bb_0};
|
||||
|
||||
const uint8_t _I_Chess_bw_0[] = {
|
||||
0x00,
|
||||
0x0C,
|
||||
0x16,
|
||||
0x23,
|
||||
0x16,
|
||||
0x0C,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_bw[] = {_I_Chess_bw_0};
|
||||
|
||||
const uint8_t _I_Chess_kb_0[] = {
|
||||
0x00,
|
||||
0x0C,
|
||||
0x2D,
|
||||
0x21,
|
||||
0x12,
|
||||
0x0C,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_kb[] = {_I_Chess_kb_0};
|
||||
|
||||
const uint8_t _I_Chess_kw_0[] = {
|
||||
0x00,
|
||||
0x0C,
|
||||
0x21,
|
||||
0x21,
|
||||
0x12,
|
||||
0x0C,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_kw[] = {_I_Chess_kw_0};
|
||||
|
||||
const uint8_t _I_Chess_nb_0[] = {
|
||||
0x00,
|
||||
0x06,
|
||||
0x0F,
|
||||
0x1F,
|
||||
0x2E,
|
||||
0x0E,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_nb[] = {_I_Chess_nb_0};
|
||||
|
||||
const uint8_t _I_Chess_nw_0[] = {
|
||||
0x00,
|
||||
0x06,
|
||||
0x09,
|
||||
0x11,
|
||||
0x2A,
|
||||
0x0A,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_nw[] = {_I_Chess_nw_0};
|
||||
|
||||
const uint8_t _I_Chess_old_0[] = {
|
||||
0x01, 0x00, 0x35, 0x01, 0x80, 0x7f, 0xc0, 0x2c, 0x0f, 0xf0, 0x7f, 0x83, 0xfc, 0x1f, 0xe0, 0xff,
|
||||
0x07, 0xf8, 0x3f, 0xc0, 0x03, 0x80, 0x0f, 0x70, 0x3f, 0xe0, 0x10, 0x11, 0x77, 0x40, 0x7f, 0x97,
|
||||
0xf0, 0xfd, 0xc0, 0x70, 0x50, 0x3c, 0x04, 0x0c, 0x1f, 0x50, 0x2b, 0x10, 0x0c, 0x61, 0x80, 0xfa,
|
||||
0x82, 0x44, 0x00, 0x63, 0x30, 0x07, 0xd4, 0x22, 0x10, 0x03, 0x1a, 0x00, 0x3e, 0xa2, 0x10, 0x42,
|
||||
0xa1, 0x90, 0x2d, 0x01, 0x97, 0x03, 0xfa, 0x03, 0xe7, 0x01, 0x83, 0x5f, 0xf8, 0x3f, 0xa8, 0x00,
|
||||
0xfc, 0xc0, 0x4f, 0xc1, 0xe5, 0x3c, 0x07, 0xcd, 0x03, 0x81, 0x41, 0x06, 0x0f, 0x0c, 0x1f, 0x32,
|
||||
0x08, 0x04, 0x98, 0x46, 0x31, 0xf8, 0x08, 0xfa, 0x15, 0x83, 0x38, 0x80, 0x70, 0x2f, 0xf0, 0x20,
|
||||
0x7d, 0x08, 0x47, 0x81, 0x73, 0x07, 0xcf, 0x42, 0x07, 0xc0, 0x80, 0x78, 0x3e, 0x30, 0x40, 0x7c,
|
||||
0x7c, 0x21, 0xf0, 0x08, 0x18, 0x3e, 0xfc, 0x11, 0xf7, 0xe0, 0xf8, 0x3f, 0xe0, 0xfa, 0x9f, 0x90,
|
||||
0x84, 0x1f, 0x65, 0xb1, 0x01, 0x9c, 0x59, 0x9d, 0x21, 0x34, 0x95, 0x2f, 0xe7, 0xfe, 0xee, 0x14,
|
||||
0x78, 0xd5, 0x41, 0xf3, 0xfe, 0x81, 0x88, 0xc8, 0x3f, 0x5f, 0xc8, 0x3c, 0x02, 0xc1, 0x60, 0x0f,
|
||||
0xd7, 0xe1, 0x00, 0x31, 0x0c, 0x00, 0x7e, 0xbe, 0x08, 0x01, 0x00, 0x08, 0x7e, 0x90, 0x04, 0x00,
|
||||
0x10, 0xfd, 0x70, 0x00, 0x65, 0x00, 0x8a, 0x0f, 0xeb, 0x8e, 0x60, 0x13, 0xa9, 0x07, 0xa3, 0x5f,
|
||||
0x01, 0x28, 0x0c, 0x08, 0x1f, 0x37, 0x0b, 0xac, 0x02, 0x7d, 0x00, 0x49, 0x08, 0x3e, 0x37, 0x11,
|
||||
0xa4, 0x88, 0xf0, 0x45, 0xf5, 0x78, 0x82, 0x81, 0x44, 0xc2, 0x40, 0xf8, 0xc4, 0x7f, 0xff, 0xe8,
|
||||
0x03, 0x07, 0xc4, 0x40, 0x18, 0x40, 0xfb, 0x90, 0x03, 0xfa, 0x80, 0x0f, 0x50, 0x84, 0x60, 0x0d,
|
||||
0x62, 0x20, 0xd8, 0x70, 0x3f, 0xbe, 0x0f, 0xf1, 0x07, 0xe4, 0x03, 0xf8, 0x10, 0x40, 0x7d, 0x01,
|
||||
0x0c, 0x1f, 0x77, 0xf2, 0x91, 0x83, 0xeb, 0x7e, 0x1f, 0xdf, 0xa5, 0x7c, 0x1d, 0x90, 0x0d, 0x54,
|
||||
0xa8, 0x1f, 0xb5, 0x68, 0xa8, 0x7f, 0xa1, 0x40, 0xfd, 0xaa, 0xc1, 0x41, 0xfb, 0xa1, 0x81, 0x03,
|
||||
0xf5, 0xa0, 0x80, 0xff, 0x07, 0xce, 0x01, 0x9c, 0x80,
|
||||
};
|
||||
const uint8_t* const _I_Chess_old[] = {_I_Chess_old_0};
|
||||
|
||||
const uint8_t _I_Chess_pb_0[] = {
|
||||
0x00,
|
||||
0x00,
|
||||
0x0C,
|
||||
0x1E,
|
||||
0x1E,
|
||||
0x0C,
|
||||
0x1E,
|
||||
};
|
||||
const uint8_t* const _I_Chess_pb[] = {_I_Chess_pb_0};
|
||||
|
||||
const uint8_t _I_Chess_pw_0[] = {
|
||||
0x00,
|
||||
0x00,
|
||||
0x0C,
|
||||
0x12,
|
||||
0x12,
|
||||
0x0C,
|
||||
0x1E,
|
||||
};
|
||||
const uint8_t* const _I_Chess_pw[] = {_I_Chess_pw_0};
|
||||
|
||||
const uint8_t _I_Chess_qb_0[] = {
|
||||
0x00,
|
||||
0x2D,
|
||||
0x2D,
|
||||
0x2D,
|
||||
0x1E,
|
||||
0x1E,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_qb[] = {_I_Chess_qb_0};
|
||||
|
||||
const uint8_t _I_Chess_qw_0[] = {
|
||||
0x00,
|
||||
0x2D,
|
||||
0x2D,
|
||||
0x2D,
|
||||
0x1E,
|
||||
0x1E,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_qw[] = {_I_Chess_qw_0};
|
||||
|
||||
const uint8_t _I_Chess_rb_0[] = {
|
||||
0x00,
|
||||
0x2D,
|
||||
0x2D,
|
||||
0x1E,
|
||||
0x1E,
|
||||
0x1E,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_rb[] = {_I_Chess_rb_0};
|
||||
|
||||
const uint8_t _I_Chess_rw_0[] = {
|
||||
0x00,
|
||||
0x2D,
|
||||
0x2D,
|
||||
0x12,
|
||||
0x12,
|
||||
0x12,
|
||||
0x3F,
|
||||
};
|
||||
const uint8_t* const _I_Chess_rw[] = {_I_Chess_rw_0};
|
||||
|
||||
const Icon I_Chess_Selection2 =
|
||||
{.width = 8, .height = 8, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_Selection2};
|
||||
const Icon I_Chess_old =
|
||||
{.width = 128, .height = 64, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_old};
|
||||
const Icon I_Chess_Selection1 =
|
||||
{.width = 8, .height = 8, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_Selection1};
|
||||
const Icon I_Chess =
|
||||
{.width = 128, .height = 64, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess};
|
||||
const Icon I_Chess_kb =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_kb};
|
||||
const Icon I_Chess_rw =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_rw};
|
||||
const Icon I_Chess_rb =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_rb};
|
||||
const Icon I_Chess_kw =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_kw};
|
||||
const Icon I_Chess_qb =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_qb};
|
||||
const Icon I_Chess_qw =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_qw};
|
||||
const Icon I_Chess_pw =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_pw};
|
||||
const Icon I_Chess_pb =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_pb};
|
||||
const Icon I_Chess_nb =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_nb};
|
||||
const Icon I_Chess_bw =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_bw};
|
||||
const Icon I_Chess_bb =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_bb};
|
||||
const Icon I_Chess_nw =
|
||||
{.width = 6, .height = 6, .frame_count = 1, .frame_rate = 0, .frames = _I_Chess_nw};
|
||||
|
||||
typedef struct {
|
||||
uint8_t col, row;
|
||||
} _Position;
|
||||
|
||||
typedef struct {
|
||||
enum {
|
||||
None = 0,
|
||||
Pawn,
|
||||
King,
|
||||
Queen,
|
||||
Bishop,
|
||||
Knight,
|
||||
Rook,
|
||||
} type;
|
||||
enum { White, Black } side;
|
||||
} Piece;
|
||||
|
||||
static const _Position PosNone = {.col = 255, .row = 255};
|
||||
// static Piece board[8][8]; // col, row
|
||||
static _Position sel, move_from = PosNone, move_to = PosNone;
|
||||
|
||||
Game* game;
|
||||
|
||||
// uint8_t sel_col = 0, sel_row = 0;
|
||||
|
||||
// static enum {
|
||||
// SelectingFrom,
|
||||
// SelectingTo
|
||||
// } state = SelectingFrom;
|
||||
|
||||
// static void reset_board() {
|
||||
// memset(board, 0, sizeof(board));
|
||||
|
||||
// board[0][0].type = Rook;
|
||||
// board[1][0].type = Knight;
|
||||
// board[2][0].type = Bishop;
|
||||
// board[3][0].type = Queen;
|
||||
// board[4][0].type = King;
|
||||
// board[5][0].type = Bishop;
|
||||
// board[6][0].type = Knight;
|
||||
// board[7][0].type = Rook;
|
||||
|
||||
// board[0][1].type = Pawn;
|
||||
// board[1][1].type = Pawn;
|
||||
// board[2][1].type = Pawn;
|
||||
// board[3][1].type = Pawn;
|
||||
// board[4][1].type = Pawn;
|
||||
// board[5][1].type = Pawn;
|
||||
// board[6][1].type = Pawn;
|
||||
// board[7][1].type = Pawn;
|
||||
|
||||
// board[0][7].type = Rook; board[0][7].side = Black;
|
||||
// board[1][7].type = Knight; board[1][7].side = Black;
|
||||
// board[2][7].type = Bishop; board[2][7].side = Black;
|
||||
// board[3][7].type = Queen; board[3][7].side = Black;
|
||||
// board[4][7].type = King; board[4][7].side = Black;
|
||||
// board[5][7].type = Bishop; board[5][7].side = Black;
|
||||
// board[6][7].type = Knight; board[6][7].side = Black;
|
||||
// board[7][7].type = Rook; board[7][7].side = Black;
|
||||
|
||||
// board[0][6].type = Pawn; board[0][6].side = Black;
|
||||
// board[1][6].type = Pawn; board[1][6].side = Black;
|
||||
// board[2][6].type = Pawn; board[2][6].side = Black;
|
||||
// board[3][6].type = Pawn; board[3][6].side = Black;
|
||||
// board[4][6].type = Pawn; board[4][6].side = Black;
|
||||
// board[5][6].type = Pawn; board[5][6].side = Black;
|
||||
// board[6][6].type = Pawn; board[6][6].side = Black;
|
||||
// board[7][6].type = Pawn; board[7][6].side = Black;
|
||||
// }
|
||||
|
||||
// static const Icon* get_icon(const Piece* piece) {
|
||||
// if (piece->side == White) {
|
||||
// switch (piece->type) {
|
||||
// case Pawn: return &I_Chess_pw;
|
||||
// case King: return &I_Chess_kw;
|
||||
// case Queen: return &I_Chess_qw;
|
||||
// case Bishop: return &I_Chess_bw;
|
||||
// case Knight: return &I_Chess_nw;
|
||||
// case Rook: return &I_Chess_rw;
|
||||
// default: return NULL;
|
||||
// }
|
||||
// } else {
|
||||
// switch (piece->type) {
|
||||
// case Pawn: return &I_Chess_pb;
|
||||
// case King: return &I_Chess_kb;
|
||||
// case Queen: return &I_Chess_qb;
|
||||
// case Bishop: return &I_Chess_bb;
|
||||
// case Knight: return &I_Chess_nb;
|
||||
// case Rook: return &I_Chess_rb;
|
||||
// default: return NULL;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
static void notify_click() {
|
||||
// static const NotificationSequence sequence = {
|
||||
// &message_click,
|
||||
// &message_delay_1,
|
||||
// &message_sound_off,
|
||||
// NULL,
|
||||
// };
|
||||
|
||||
// notification_message_block(notification, &sequence);
|
||||
notification_message(notification, &sequence_single_vibro);
|
||||
}
|
||||
static const Icon* _get_icon(uint8_t file, uint8_t rank) {
|
||||
char piece = getPieceChar((FILES_BB[file] & RANKS_BB[7 - rank]), &(game->position.board));
|
||||
switch(piece) {
|
||||
case 'P':
|
||||
return &I_Chess_pw;
|
||||
case 'K':
|
||||
return &I_Chess_kw;
|
||||
case 'Q':
|
||||
return &I_Chess_qw;
|
||||
case 'B':
|
||||
return &I_Chess_bw;
|
||||
case 'N':
|
||||
return &I_Chess_nw;
|
||||
case 'R':
|
||||
return &I_Chess_rw;
|
||||
case 'p':
|
||||
return &I_Chess_pb;
|
||||
case 'k':
|
||||
return &I_Chess_kb;
|
||||
case 'q':
|
||||
return &I_Chess_qb;
|
||||
case 'b':
|
||||
return &I_Chess_bb;
|
||||
case 'n':
|
||||
return &I_Chess_nb;
|
||||
case 'r':
|
||||
return &I_Chess_rb;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int get_position(uint8_t file, uint8_t rank) {
|
||||
return 8 * rank + file;
|
||||
}
|
||||
|
||||
static int get_rank(int position) {
|
||||
return (int)(position / 8);
|
||||
}
|
||||
|
||||
static int get_file(int position) {
|
||||
return position % 8;
|
||||
}
|
||||
|
||||
static void make_move(uint8_t file1, uint8_t rank1, uint8_t file2, uint8_t rank2) {
|
||||
int from = get_position(file1, rank1);
|
||||
int to = get_position(file2, rank2);
|
||||
Move move = generateMove(from, to);
|
||||
if(!isLegalMove(&game->position, move)) {
|
||||
return;
|
||||
}
|
||||
makeMove(game, move);
|
||||
move2str(white_move_str, game, game->moveListLen - 1);
|
||||
notify_click();
|
||||
black_move_str[0] = 0;
|
||||
anim = furi_get_tick();
|
||||
thinking = true;
|
||||
}
|
||||
|
||||
static int32_t make_ai_move(void* context) {
|
||||
UNUSED(context);
|
||||
// thinking = true;
|
||||
int depth = 1;
|
||||
Move move;
|
||||
Node node =
|
||||
iterativeDeepeningAlphaBeta(&(game->position), (char)depth, INT32_MIN, INT32_MAX, FALSE);
|
||||
move = node.move;
|
||||
makeMove(game, move);
|
||||
move2str(black_move_str, game, game->moveListLen - 1);
|
||||
notify_click();
|
||||
thinking = false;
|
||||
anim = furi_get_tick();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static FuriThread* worker_thread = NULL;
|
||||
|
||||
static int32_t ai_thread(void* context) {
|
||||
while(true) {
|
||||
if(should_exit) break;
|
||||
if(thinking) make_ai_move(context);
|
||||
furi_delay_ms(100);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void run_ai_thread() {
|
||||
if(worker_thread == NULL) {
|
||||
worker_thread = furi_thread_alloc();
|
||||
}
|
||||
|
||||
furi_thread_set_name(worker_thread, "ChessEngine");
|
||||
furi_thread_set_stack_size(worker_thread, 7000);
|
||||
// furi_thread_set_context(thread, bad_usb);
|
||||
furi_thread_set_callback(worker_thread, ai_thread);
|
||||
furi_thread_start(worker_thread);
|
||||
|
||||
// furi_thread_join(worker_thread);
|
||||
// furi_thread_free(worker_thread);
|
||||
}
|
||||
|
||||
static void chess_draw_callback(Canvas* canvas, void* ctx) {
|
||||
UNUSED(ctx);
|
||||
should_update_screen = false;
|
||||
canvas_clear(canvas);
|
||||
|
||||
// canvas_set_color(canvas, flag ? ColorBlack : ColorWhite);
|
||||
|
||||
canvas_draw_icon(canvas, 0, 0, &I_Chess);
|
||||
|
||||
if(!thinking) {
|
||||
canvas_set_color(canvas, (sel.col + sel.row) % 2 != 0 ? ColorBlack : ColorWhite);
|
||||
canvas_draw_icon(
|
||||
canvas,
|
||||
sel.col * 8,
|
||||
(7 - sel.row) * 8,
|
||||
flag ? &I_Chess_Selection1 : &I_Chess_Selection2);
|
||||
|
||||
if(move_from.col != 255) {
|
||||
canvas_set_color(
|
||||
canvas, (move_from.col + move_from.row) % 2 != 0 ? ColorBlack : ColorWhite);
|
||||
canvas_draw_icon(
|
||||
canvas,
|
||||
move_from.col * 8,
|
||||
(7 - move_from.row) * 8,
|
||||
flag ? &I_Chess_Selection1 : &I_Chess_Selection2);
|
||||
}
|
||||
}
|
||||
|
||||
// print moves
|
||||
if(game->moveListLen > 0) {
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
|
||||
// int num = game->moveListLen;
|
||||
|
||||
// char white_str[8], black_str[8] = "...";
|
||||
|
||||
// if (num == 0) {
|
||||
// } else if (num % 2 == 0) {
|
||||
// // white move
|
||||
// move2str(white_str, game, game->moveListLen - 2);
|
||||
// move2str(black_str, game, game->moveListLen - 1);
|
||||
// } else {
|
||||
// move2str(white_str, game, game->moveListLen - 1);
|
||||
// }
|
||||
|
||||
char str[28];
|
||||
snprintf(
|
||||
str, 28, "%d. %s %s", (game->moveListLen + 1) / 2, white_move_str, black_move_str);
|
||||
canvas_draw_str(canvas, 75, 12, str);
|
||||
}
|
||||
|
||||
Move last_move = getLastMove(game);
|
||||
|
||||
for(uint8_t row = 0; row < 8; row++) {
|
||||
for(uint8_t col = 0; col < 8; col++) {
|
||||
bool white_field = (row + col) % 2 != 0;
|
||||
|
||||
// if (!white_field) {
|
||||
// canvas_draw_box(canvas, col * 8, row * 8, 8, 8);
|
||||
// }
|
||||
const Icon* icon = _get_icon(col, row);
|
||||
if(icon != NULL) {
|
||||
int x = col * 8;
|
||||
int y = row * 8;
|
||||
|
||||
int dt = furi_get_tick() - anim;
|
||||
if(anim && dt >= 300) {
|
||||
anim = 0;
|
||||
}
|
||||
|
||||
if(anim && last_move && get_file(getTo(last_move)) == col &&
|
||||
get_rank(getTo(last_move)) == (7 - row)) {
|
||||
// moving piece
|
||||
uint8_t from_x = get_file(getFrom(last_move)) * 8;
|
||||
uint8_t from_y = (7 - get_rank(getFrom(last_move))) * 8;
|
||||
x = from_x + (x - from_x) * dt / 300;
|
||||
y = from_y + (y - from_y) * dt / 300;
|
||||
}
|
||||
|
||||
canvas_set_color(canvas, white_field ? ColorWhite : ColorBlack);
|
||||
canvas_draw_icon(canvas, x + 1, y + 1, icon);
|
||||
}
|
||||
|
||||
// if (board[col][7 - row].type != None) {
|
||||
// canvas_set_color(canvas, white_field ? ColorWhite : ColorBlack);
|
||||
// canvas_draw_icon(canvas, col * 8 + 1, row * 8 + 1, get_icon(&board[col][7 - row]));
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
// for (uint8_t i = 0; i < 4; i++) {
|
||||
// canvas_draw_dot(canvas, sel_col * 8, sel_row * 8);
|
||||
// canvas_draw_dot(canvas, sel_col * 8 + 2, sel_row * 8);
|
||||
// canvas_draw_dot(canvas, sel_col * 8, sel_row * 8);
|
||||
// canvas_draw_dot(canvas, sel_col * 8, sel_row * 8);
|
||||
// }
|
||||
|
||||
// canvas_draw_disc(canvas, GUI_DISPLAY_WIDTH / 2 - 40, GUI_DISPLAY_HEIGHT / 2, 15);
|
||||
// canvas_set_color(canvas, flag ? ColorBlack : ColorWhite);
|
||||
// canvas_draw_disc(canvas, GUI_DISPLAY_WIDTH / 2, GUI_DISPLAY_HEIGHT / 2, 15);
|
||||
}
|
||||
|
||||
static void chess_input_callback(InputEvent* event, void* ctx) {
|
||||
UNUSED(ctx);
|
||||
if(event->type == InputTypeShort) {
|
||||
if(event->key == InputKeyLeft) {
|
||||
sel.col = (sel.col == 0) ? 0 : sel.col - 1;
|
||||
} else if(event->key == InputKeyRight) {
|
||||
sel.col++;
|
||||
} else if(event->key == InputKeyDown) {
|
||||
sel.row = (sel.row == 0) ? 0 : sel.row - 1;
|
||||
} else if(event->key == InputKeyUp) {
|
||||
sel.row++;
|
||||
} else if(event->key == InputKeyOk) {
|
||||
if(move_from.col == 255) {
|
||||
move_from = sel;
|
||||
} else if(move_to.col == 255) {
|
||||
move_to = sel;
|
||||
make_move(move_from.col, move_from.row, move_to.col, move_to.row);
|
||||
// thinking = true;
|
||||
// ai_should_make_move = true;
|
||||
// make_ai_move_threaded();
|
||||
// Piece piece = board[move_from.col][move_from.row];
|
||||
// board[move_from.col][move_from.row].type = None;
|
||||
// board[move_to.col][move_to.row] = piece;
|
||||
move_from = PosNone;
|
||||
move_to = PosNone;
|
||||
}
|
||||
} else if(event->key == InputKeyBack) {
|
||||
should_exit = true;
|
||||
}
|
||||
sel.col = CLAMP(sel.col, 7, 0);
|
||||
sel.row = CLAMP(sel.row, 7, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void setup_engine() {
|
||||
// int depth = 1; // DEFAULT_AI_DEPTH;
|
||||
|
||||
getInitialGame(game);
|
||||
|
||||
// Move move;
|
||||
// Node node = iterativeDeepeningAlphaBeta(&(game.position), (char) depth, INT32_MIN, INT32_MAX, FALSE);
|
||||
// move = node.move;
|
||||
|
||||
// node = iterativeDeepeningAlphaBeta(&(game.position), (char) 2, INT32_MIN, INT32_MAX, FALSE);
|
||||
|
||||
// node = iterativeDeepeningAlphaBeta(&(game.position), (char) 3, INT32_MIN, INT32_MAX, FALSE);
|
||||
|
||||
// printf("%d\n", move);
|
||||
}
|
||||
|
||||
// void test_engine() {
|
||||
// FuriThread* thread; //
|
||||
|
||||
// thread = furi_thread_alloc();
|
||||
// furi_thread_set_name(thread, "ChessEngine");
|
||||
// furi_thread_set_stack_size(thread, 20000);
|
||||
// // furi_thread_set_context(thread, bad_usb);
|
||||
// furi_thread_set_callback(thread, setup_engine);
|
||||
|
||||
// furi_thread_start(thread);
|
||||
|
||||
// furi_thread_join(thread);
|
||||
|
||||
// furi_thread_free(thread);
|
||||
// }
|
||||
|
||||
int32_t chess_app(void* p) {
|
||||
UNUSED(p);
|
||||
// Configure view port
|
||||
ViewPort* view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(view_port, chess_draw_callback, NULL);
|
||||
view_port_input_callback_set(view_port, chess_input_callback, NULL);
|
||||
|
||||
// Register view port in GUI
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||
|
||||
notification = furi_record_open(RECORD_NOTIFICATION);
|
||||
|
||||
should_exit = false;
|
||||
|
||||
game = malloc(sizeof(Game));
|
||||
|
||||
setup_engine();
|
||||
run_ai_thread();
|
||||
|
||||
// test_engine();
|
||||
|
||||
while(!should_exit) {
|
||||
furi_delay_ms(100);
|
||||
if(!thinking) {
|
||||
flag = !flag;
|
||||
should_update_screen = true;
|
||||
}
|
||||
if(anim) {
|
||||
should_update_screen = true;
|
||||
}
|
||||
if(should_update_screen) {
|
||||
view_port_update(view_port);
|
||||
}
|
||||
// flag = true;
|
||||
// delay(40);
|
||||
// flag = false;
|
||||
// view_port_update(view_port);
|
||||
// delay(80);
|
||||
}
|
||||
|
||||
furi_thread_join(worker_thread);
|
||||
furi_thread_free(worker_thread);
|
||||
worker_thread = NULL;
|
||||
|
||||
gui_remove_view_port(gui, view_port);
|
||||
view_port_free(view_port);
|
||||
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
free(game);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,388 +0,0 @@
|
||||
/*
|
||||
* fast-chess.h
|
||||
*
|
||||
* Created on: 20 de set de 2016
|
||||
* Author: fvj
|
||||
*/
|
||||
|
||||
#ifndef FAST_CHESS_H_
|
||||
#define FAST_CHESS_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define ENGINE_VERSION "v1.8.1"
|
||||
|
||||
#define ENGINE_NAME "github.com/fredericojordan/fast-chess " ENGINE_VERSION
|
||||
#define HUMAN_NAME "Unknown Human Player"
|
||||
|
||||
#define NUM_SQUARES (64)
|
||||
#define ENDGAME_PIECE_COUNT (7)
|
||||
|
||||
#define COLOR_MASK (1 << 3)
|
||||
#define WHITE (0)
|
||||
#define BLACK (1 << 3)
|
||||
|
||||
#define PIECE_MASK (0x7)
|
||||
#define EMPTY (0)
|
||||
#define PAWN (1)
|
||||
#define KNIGHT (2)
|
||||
#define BISHOP (3)
|
||||
#define ROOK (4)
|
||||
#define QUEEN (5)
|
||||
#define KING (6)
|
||||
|
||||
#define ALL_SQUARES (0xFFFFFFFFFFFFFFFF)
|
||||
#define FILE_A (0x0101010101010101)
|
||||
#define FILE_B (0x0202020202020202)
|
||||
#define FILE_C (0x0404040404040404)
|
||||
#define FILE_D (0x0808080808080808)
|
||||
#define FILE_E (0x1010101010101010)
|
||||
#define FILE_F (0x2020202020202020)
|
||||
#define FILE_G (0x4040404040404040)
|
||||
#define FILE_H (0x8080808080808080)
|
||||
#define RANK_1 (0x00000000000000FF)
|
||||
#define RANK_2 (0x000000000000FF00)
|
||||
#define RANK_3 (0x0000000000FF0000)
|
||||
#define RANK_4 (0x00000000FF000000)
|
||||
#define RANK_5 (0x000000FF00000000)
|
||||
#define RANK_6 (0x0000FF0000000000)
|
||||
#define RANK_7 (0x00FF000000000000)
|
||||
#define RANK_8 (0xFF00000000000000)
|
||||
#define DIAG_A1H8 (0x8040201008040201)
|
||||
#define ANTI_DIAG_H1A8 (0x0102040810204080)
|
||||
#define LIGHT_SQUARES (0x55AA55AA55AA55AA)
|
||||
#define DARK_SQUARES (0xAA55AA55AA55AA55)
|
||||
|
||||
#define CASTLE_KINGSIDE_WHITE (1 << 0)
|
||||
#define CASTLE_QUEENSIDE_WHITE (1 << 1)
|
||||
#define CASTLE_KINGSIDE_BLACK (1 << 2)
|
||||
#define CASTLE_QUEENSIDE_BLACK (1 << 3)
|
||||
|
||||
#define BOOL char
|
||||
|
||||
#ifndef FALSE
|
||||
#define TRUE (1)
|
||||
#define FALSE (0)
|
||||
#endif
|
||||
|
||||
typedef uint_fast64_t Bitboard;
|
||||
typedef int Move;
|
||||
|
||||
#define MAX_BOOK_ENTRY_LEN (300)
|
||||
#define MAX_PLYS_PER_GAME (1024)
|
||||
#define MAX_FEN_LEN (100)
|
||||
// #define MAX_BRANCHING_FACTOR (218) /* R6R/3Q4/1Q4Q1/4Q3/2Q4Q/Q4Q2/pp1Q4/kBNN1KB1 w - - 0 1 3Q4/1Q4Q1/4Q3/2Q4R/Q4Q2/3Q4/1Q4Rp/1K1BBNNk w - - 0 1 */
|
||||
#define MAX_BRANCHING_FACTOR (100) // okalachev
|
||||
#define MAX_ATTACKING_PIECES (12)
|
||||
|
||||
#define DEFAULT_AI_DEPTH (3)
|
||||
|
||||
typedef struct {
|
||||
Bitboard whiteKing;
|
||||
Bitboard whiteQueens;
|
||||
Bitboard whiteRooks;
|
||||
Bitboard whiteKnights;
|
||||
Bitboard whiteBishops;
|
||||
Bitboard whitePawns;
|
||||
|
||||
Bitboard blackKing;
|
||||
Bitboard blackQueens;
|
||||
Bitboard blackRooks;
|
||||
Bitboard blackKnights;
|
||||
Bitboard blackBishops;
|
||||
Bitboard blackPawns;
|
||||
} Board;
|
||||
|
||||
typedef struct {
|
||||
Board board;
|
||||
char toMove;
|
||||
char epSquare;
|
||||
char castlingRights;
|
||||
unsigned int halfmoveClock;
|
||||
unsigned int fullmoveNumber;
|
||||
} Position;
|
||||
|
||||
typedef struct {
|
||||
Position position;
|
||||
|
||||
unsigned int moveListLen;
|
||||
Move moveList[MAX_PLYS_PER_GAME];
|
||||
char positionHistory[MAX_PLYS_PER_GAME][MAX_FEN_LEN];
|
||||
} Game;
|
||||
|
||||
typedef struct {
|
||||
Move move;
|
||||
int score;
|
||||
} Node;
|
||||
|
||||
typedef struct {
|
||||
int depth;
|
||||
Position pos;
|
||||
int* alpha;
|
||||
int* beta;
|
||||
BOOL verbose;
|
||||
} ThreadInfo;
|
||||
|
||||
extern char FILES[8];
|
||||
extern char RANKS[8];
|
||||
|
||||
extern Bitboard FILES_BB[8];
|
||||
extern Bitboard RANKS_BB[8];
|
||||
|
||||
extern char INITIAL_FEN[];
|
||||
extern Board INITIAL_BOARD;
|
||||
extern int PIECE_VALUES[];
|
||||
|
||||
#define DOUBLED_PAWN_PENALTY (10)
|
||||
#define ISOLATED_PAWN_PENALTY (20)
|
||||
#define BACKWARDS_PAWN_PENALTY (8)
|
||||
#define PASSED_PAWN_BONUS (20)
|
||||
#define ROOK_SEMI_OPEN_FILE_BONUS (10)
|
||||
#define ROOK_OPEN_FILE_BONUS (15)
|
||||
#define ROOK_ON_SEVENTH_BONUS (20)
|
||||
|
||||
extern int PAWN_BONUS[];
|
||||
extern int KNIGHT_BONUS[];
|
||||
extern int BISHOP_BONUS[];
|
||||
extern int KING_BONUS[];
|
||||
extern int KING_ENDGAME_BONUS[];
|
||||
extern int FLIP_VERTICAL[];
|
||||
|
||||
void getInitialGame(Game* game);
|
||||
void getFenGame(Game* game, char fen[]);
|
||||
void insertPiece(Board* board, Bitboard position, char pieceCode);
|
||||
int loadFen(Position* position, char fen[]);
|
||||
int toFen(char* fen, Position* position);
|
||||
int toMinFen(char* fen, Position* position);
|
||||
void getMovelistGame(Game* game, char moves[]);
|
||||
|
||||
// ========= UTILITY =========
|
||||
|
||||
BOOL fromInitial(Game* game);
|
||||
Bitboard index2bb(int index);
|
||||
int str2index(char* str);
|
||||
Bitboard str2bb(char* str);
|
||||
BOOL isSet(Bitboard bb, int index);
|
||||
Bitboard lsb(Bitboard bb);
|
||||
Bitboard msb(Bitboard bb);
|
||||
int bb2index(Bitboard bb);
|
||||
char* movelist2str(Game* game);
|
||||
Move getLastMove(Game* game);
|
||||
BOOL startsWith(const char* str, const char* pre);
|
||||
int countBookOccurrences(Game* game);
|
||||
Move getBookMove(Game* game);
|
||||
char getFile(int position);
|
||||
char getRank(int position);
|
||||
Move generateMove(int leavingSquare, int arrivingSquare);
|
||||
int getFrom(Move move);
|
||||
int getTo(Move move);
|
||||
int char2piece(char pieceCode);
|
||||
int bb2piece(Bitboard position, Board* board);
|
||||
char bb2char(Bitboard position, Board* board);
|
||||
char* bb2str(Bitboard position, Board* board);
|
||||
void printBitboard(Bitboard bitboard);
|
||||
char getPieceChar(Bitboard position, Board* board);
|
||||
void printBoard(Board* board);
|
||||
void printGame(Game* game);
|
||||
Bitboard not(Bitboard bb);
|
||||
char opponent(char color);
|
||||
int countBits(Bitboard bb);
|
||||
void sortNodes(Node* sortedNodes, Node* nodes, int len, char color);
|
||||
void printMove(Move move);
|
||||
void printFullMove(Move move, Board* board);
|
||||
void printLegalMoves(Position* position);
|
||||
void printNode(Node node);
|
||||
void getTimestamp(char* timestamp);
|
||||
void dumpContent(Game* game);
|
||||
void dumpPGN(Game* game, char color, BOOL hasAI);
|
||||
void move2str(char* str, Game* game, int moveNumber);
|
||||
BOOL isAmbiguous(Position* posBefore, Move move);
|
||||
unsigned long hashPosition(Position* position);
|
||||
void writeToHashFile(Position* position, int evaluation, int depth);
|
||||
|
||||
// ====== BOARD FILTERS ======
|
||||
|
||||
Bitboard getColoredPieces(Board* board, char color);
|
||||
Bitboard getEmptySquares(Board* board);
|
||||
Bitboard getOccupiedSquares(Board* board);
|
||||
Bitboard getTwinPieces(Bitboard position, Board* board);
|
||||
Bitboard fileFilter(Bitboard positions);
|
||||
Bitboard rankFilter(Bitboard positions);
|
||||
|
||||
// ======= DIRECTIONS ========
|
||||
|
||||
Bitboard east(Bitboard bb);
|
||||
Bitboard west(Bitboard bb);
|
||||
Bitboard north(Bitboard bb);
|
||||
Bitboard south(Bitboard bb);
|
||||
Bitboard NE(Bitboard bb);
|
||||
Bitboard NW(Bitboard bb);
|
||||
Bitboard SE(Bitboard bb);
|
||||
Bitboard SW(Bitboard bb);
|
||||
Bitboard WNW(Bitboard moving_piece);
|
||||
Bitboard ENE(Bitboard moving_piece);
|
||||
Bitboard NNW(Bitboard moving_piece);
|
||||
Bitboard NNE(Bitboard moving_piece);
|
||||
Bitboard ESE(Bitboard moving_piece);
|
||||
Bitboard WSW(Bitboard moving_piece);
|
||||
Bitboard SSE(Bitboard moving_piece);
|
||||
Bitboard SSW(Bitboard moving_piece);
|
||||
|
||||
// ========== PAWN ===========
|
||||
|
||||
Bitboard getPawns(Board* board);
|
||||
Bitboard pawnSimplePushes(Bitboard moving_piece, Board* board, char color);
|
||||
Bitboard pawnDoublePushes(Bitboard moving_piece, Board* board, char color);
|
||||
Bitboard pawnPushes(Bitboard moving_piece, Board* board, char color);
|
||||
Bitboard pawnEastAttacks(Bitboard moving_piece, Board* board, char color);
|
||||
Bitboard pawnWestAttacks(Bitboard moving_piece, Board* board, char color);
|
||||
Bitboard pawnAttacks(Bitboard moving_piece, Board* board, char color);
|
||||
Bitboard pawnSimpleCaptures(Bitboard moving_piece, Board* board, char color);
|
||||
Bitboard pawnEpCaptures(Bitboard moving_piece, Position* position, char color);
|
||||
Bitboard pawnCaptures(Bitboard moving_piece, Position* position, char color);
|
||||
Bitboard pawnMoves(Bitboard moving_piece, Position* position, char color);
|
||||
BOOL isDoublePush(int leaving, int arriving);
|
||||
char getEpSquare(int leaving);
|
||||
BOOL isDoubledPawn(Bitboard position, Board* board, char color);
|
||||
BOOL isIsolatedPawn(Bitboard position, Board* board, char color);
|
||||
BOOL isBackwardsPawn(Bitboard position, Board* board, char color);
|
||||
BOOL isPassedPawn(Bitboard position, Board* board, char color);
|
||||
BOOL isOpenFile(Bitboard position, Board* board);
|
||||
BOOL isSemiOpenFile(Bitboard position, Board* board);
|
||||
|
||||
// ========== KNIGHT =========
|
||||
|
||||
Bitboard getKnights(Board* board);
|
||||
Bitboard knightAttacks(Bitboard moving_piece);
|
||||
Bitboard knightMoves(Bitboard moving_piece, Board* board, char color);
|
||||
|
||||
// ========== KING ===========
|
||||
|
||||
Bitboard getKing(Board* board, char color);
|
||||
Bitboard kingAttacks(Bitboard moving_piece);
|
||||
Bitboard kingMoves(Bitboard moving_piece, Board* board, char color);
|
||||
BOOL canCastleKingside(Position* position, char color);
|
||||
BOOL canCastleQueenside(Position* position, char color);
|
||||
char removeCastlingRights(char original_rights, char removed_rights);
|
||||
|
||||
// ========== BISHOP =========
|
||||
|
||||
Bitboard getBishops(Board* board);
|
||||
Bitboard NE_ray(Bitboard bb);
|
||||
Bitboard SE_ray(Bitboard bb);
|
||||
Bitboard NW_ray(Bitboard bb);
|
||||
Bitboard SW_ray(Bitboard bb);
|
||||
Bitboard NE_attack(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard NW_attack(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard SE_attack(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard SW_attack(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard diagonalAttacks(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard antiDiagonalAttacks(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard bishopAttacks(Bitboard moving_pieces, Board* board, char color);
|
||||
Bitboard bishopMoves(Bitboard moving_piece, Board* board, char color);
|
||||
|
||||
// ========== ROOK ===========
|
||||
|
||||
Bitboard getRooks(Board* board);
|
||||
Bitboard northRay(Bitboard moving_pieces);
|
||||
Bitboard southRay(Bitboard moving_pieces);
|
||||
Bitboard eastRay(Bitboard moving_pieces);
|
||||
Bitboard westRay(Bitboard moving_pieces);
|
||||
Bitboard northAttack(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard southAttack(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard fileAttacks(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard eastAttack(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard westAttack(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard rankAttacks(Bitboard single_piece, Board* board, char color);
|
||||
Bitboard rookAttacks(Bitboard moving_piece, Board* board, char color);
|
||||
Bitboard rookMoves(Bitboard moving_piece, Board* board, char color);
|
||||
|
||||
// ========== QUEEN ==========
|
||||
|
||||
Bitboard getQueens(Board* board);
|
||||
Bitboard queenAttacks(Bitboard moving_piece, Board* board, char color);
|
||||
Bitboard queenMoves(Bitboard moving_piece, Board* board, char color);
|
||||
|
||||
// ======== MAKE MOVE ========
|
||||
|
||||
void clearPositions(Board* board, Bitboard positions);
|
||||
void movePiece(Board* board, Move move);
|
||||
void updatePosition(Position* newPosition, Position* position, Move move);
|
||||
void makeMove(Game* game, Move move);
|
||||
void unmakeMove(Game* game);
|
||||
|
||||
// ======== MOVE GEN =========
|
||||
|
||||
Bitboard getMoves(Bitboard movingPiece, Position* position, char color);
|
||||
int pseudoLegalMoves(Move* moves, Position* position, char color);
|
||||
Bitboard getAttacks(Bitboard movingPiece, Board* board, char color);
|
||||
int countAttacks(Bitboard target, Board* board, char color);
|
||||
BOOL isAttacked(Bitboard target, Board* board, char color);
|
||||
BOOL isCheck(Board* board, char color);
|
||||
BOOL isLegalMove(Position* position, Move move);
|
||||
int legalMoves(Move* legalMoves, Position* position, char color);
|
||||
int legalMovesCount(Position* position, char color);
|
||||
int staticOrderLegalMoves(Move* orderedLegalMoves, Position* position, char color);
|
||||
int legalCaptures(Move* legalCaptures, Position* position, char color);
|
||||
|
||||
// ====== GAME CONTROL =======
|
||||
|
||||
BOOL isCheckmate(Position* position);
|
||||
BOOL isStalemate(Position* position);
|
||||
BOOL hasInsufficientMaterial(Board* board);
|
||||
BOOL isEndgame(Board* board);
|
||||
BOOL isOver75MovesRule(Position* position);
|
||||
BOOL hasGameEnded(Position* position);
|
||||
void printOutcome(Position* position);
|
||||
|
||||
// ========== EVAL ===========
|
||||
|
||||
int winScore(char color);
|
||||
int materialSum(Board* board, char color);
|
||||
int materialBalance(Board* board);
|
||||
int positionalBonus(Board* board, char color);
|
||||
int positionalBalance(Board* board);
|
||||
int endNodeEvaluation(Position* position);
|
||||
int staticEvaluation(Position* position);
|
||||
int getCaptureSequence(Move* captures, Position* position, int targetSquare);
|
||||
int staticExchangeEvaluation(Position* position, int targetSquare);
|
||||
int quiescenceEvaluation(Position* position);
|
||||
|
||||
// ========= SEARCH ==========
|
||||
|
||||
Node staticSearch(Position* position);
|
||||
Node quiescenceSearch(Position* position);
|
||||
Node alphaBeta(Position* position, char depth, int alpha, int beta);
|
||||
int alphaBetaNodes(Node* nodes, Position* position, char depth);
|
||||
Node iterativeDeepeningAlphaBeta(Position* position, char depth, int alpha, int beta, BOOL verbose);
|
||||
Node pIDAB(Position* position, char depth, int* p_alpha, int* p_beta);
|
||||
Node pIDABhashed(Position* position, char depth, int* p_alpha, int* p_beta);
|
||||
Move getRandomMove(Position* position);
|
||||
Move getAIMove(Game* game, int depth);
|
||||
Move parseMove(char* move);
|
||||
Move getPlayerMove();
|
||||
Move suggestMove(char fen[], int depth);
|
||||
|
||||
// Parallel processing currently only implemented for Windows
|
||||
#ifdef _WIN32
|
||||
DWORD WINAPI evaluatePositionThreadFunction(LPVOID lpParam);
|
||||
DWORD WINAPI evaluatePositionThreadFunctionHashed(LPVOID lpParam);
|
||||
Node idabThreaded(Position* position, int depth, BOOL verbose);
|
||||
Node idabThreadedBestFirst(Position* position, int depth, BOOL verbose);
|
||||
Node idabThreadedBestFirstHashed(Position* position, int depth, BOOL verbose);
|
||||
#endif
|
||||
|
||||
// ===== PLAY LOOP (TEXT) ====
|
||||
|
||||
void playTextWhite(int depth);
|
||||
void playTextBlack(int depth);
|
||||
void playTextAs(char color, int depth);
|
||||
void playTextRandomColor(int depth);
|
||||
|
||||
// ===========================
|
||||
|
||||
#endif /* FAST_CHESS_H_ */
|
||||
@@ -1,169 +0,0 @@
|
||||
#include "DHT.h"
|
||||
|
||||
#define lineDown() furi_hal_gpio_write(sensor->GPIO, false)
|
||||
#define lineUp() furi_hal_gpio_write(sensor->GPIO, true)
|
||||
#define getLine() furi_hal_gpio_read(sensor->GPIO)
|
||||
#define Delay(d) furi_delay_ms(d)
|
||||
|
||||
DHT_data DHT_getData(DHT_sensor* sensor) {
|
||||
DHT_data data = {-128.0f, -128.0f};
|
||||
|
||||
#if DHT_POLLING_CONTROL == 1
|
||||
/* Ограничение по частоте опроса датчика */
|
||||
//Определение интервала опроса в зависимости от датчика
|
||||
uint16_t pollingInterval;
|
||||
if(sensor->type == DHT11) {
|
||||
pollingInterval = DHT_POLLING_INTERVAL_DHT11;
|
||||
} else {
|
||||
pollingInterval = DHT_POLLING_INTERVAL_DHT22;
|
||||
}
|
||||
|
||||
//Если интервал маленький, то возврат последнего удачного значения
|
||||
if((furi_get_tick() - sensor->lastPollingTime < pollingInterval) &&
|
||||
sensor->lastPollingTime != 0) {
|
||||
data.hum = sensor->lastHum;
|
||||
data.temp = sensor->lastTemp;
|
||||
return data;
|
||||
}
|
||||
sensor->lastPollingTime = furi_get_tick() + 1;
|
||||
#endif
|
||||
|
||||
//Опускание линии данных на 18 мс
|
||||
lineDown();
|
||||
#ifdef DHT_IRQ_CONTROL
|
||||
//Выключение прерываний, чтобы ничто не мешало обработке данных
|
||||
__disable_irq();
|
||||
#endif
|
||||
Delay(18);
|
||||
|
||||
//Подъём линии
|
||||
lineUp();
|
||||
|
||||
/* Ожидание ответа от датчика */
|
||||
uint16_t timeout = 0;
|
||||
while(!getLine()) {
|
||||
timeout++;
|
||||
if(timeout > DHT_TIMEOUT) {
|
||||
#ifdef DHT_IRQ_CONTROL
|
||||
__enable_irq();
|
||||
#endif
|
||||
//Если датчик не отозвался, значит его точно нет
|
||||
//Обнуление последнего удачного значения, чтобы
|
||||
//не получать фантомные значения
|
||||
sensor->lastHum = -128.0f;
|
||||
sensor->lastTemp = -128.0f;
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
//Ожидание спада
|
||||
while(getLine()) {
|
||||
timeout++;
|
||||
if(timeout > DHT_TIMEOUT) {
|
||||
#ifdef DHT_IRQ_CONTROL
|
||||
__enable_irq();
|
||||
#endif
|
||||
//Если датчик не отозвался, значит его точно нет
|
||||
//Обнуление последнего удачного значения, чтобы
|
||||
//не получать фантомные значения
|
||||
sensor->lastHum = -128.0f;
|
||||
sensor->lastTemp = -128.0f;
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
timeout = 0;
|
||||
//Ожидание подъёма
|
||||
while(!getLine()) {
|
||||
timeout++;
|
||||
if(timeout > DHT_TIMEOUT) {
|
||||
if(timeout > DHT_TIMEOUT) {
|
||||
#ifdef DHT_IRQ_CONTROL
|
||||
__enable_irq();
|
||||
#endif
|
||||
//Если датчик не отозвался, значит его точно нет
|
||||
//Обнуление последнего удачного значения, чтобы
|
||||
//не получать фантомные значения
|
||||
sensor->lastHum = -128.0f;
|
||||
sensor->lastTemp = -128.0f;
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
timeout = 0;
|
||||
//Ожидание спада
|
||||
while(getLine()) {
|
||||
timeout++;
|
||||
if(timeout > DHT_TIMEOUT) {
|
||||
#ifdef DHT_IRQ_CONTROL
|
||||
__enable_irq();
|
||||
#endif
|
||||
//Если датчик не отозвался, значит его точно нет
|
||||
//Обнуление последнего удачного значения, чтобы
|
||||
//не получать фантомные значения
|
||||
sensor->lastHum = -128.0f;
|
||||
sensor->lastTemp = -128.0f;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
/* Чтение ответа от датчика */
|
||||
uint8_t rawData[5] = {0, 0, 0, 0, 0};
|
||||
for(uint8_t a = 0; a < 5; a++) {
|
||||
for(uint8_t b = 7; b != 255; b--) {
|
||||
uint16_t hT = 0, lT = 0;
|
||||
//Пока линия в низком уровне, инкремент переменной lT
|
||||
while(!getLine() && lT != 65535) lT++;
|
||||
//Пока линия в высоком уровне, инкремент переменной hT
|
||||
timeout = 0;
|
||||
while(getLine() && hT != 65535) hT++;
|
||||
//Если hT больше lT, то пришла единица
|
||||
if(hT > lT) rawData[a] |= (1 << b);
|
||||
}
|
||||
}
|
||||
#ifdef DHT_IRQ_CONTROL
|
||||
//Включение прерываний после приёма данных
|
||||
__enable_irq();
|
||||
#endif
|
||||
/* Проверка целостности данных */
|
||||
if((uint8_t)(rawData[0] + rawData[1] + rawData[2] + rawData[3]) == rawData[4]) {
|
||||
//Если контрольная сумма совпадает, то конвертация и возврат полученных значений
|
||||
if(sensor->type == DHT22) {
|
||||
data.hum = (float)(((uint16_t)rawData[0] << 8) | rawData[1]) * 0.1f;
|
||||
//Проверка на отрицательность температуры
|
||||
if(!(rawData[2] & (1 << 7))) {
|
||||
data.temp = (float)(((uint16_t)rawData[2] << 8) | rawData[3]) * 0.1f;
|
||||
} else {
|
||||
rawData[2] &= ~(1 << 7);
|
||||
data.temp = (float)(((uint16_t)rawData[2] << 8) | rawData[3]) * -0.1f;
|
||||
}
|
||||
}
|
||||
if(sensor->type == DHT11) {
|
||||
data.hum = (float)rawData[0];
|
||||
data.temp = (float)rawData[2];
|
||||
//DHT11 производства ASAIR имеют дробную часть в температуре
|
||||
//А ещё температуру измеряет от -20 до +60 *С
|
||||
//Вот прикол, да?
|
||||
if(rawData[3] != 0) {
|
||||
//Проверка знака
|
||||
if(!(rawData[3] & (1 << 7))) {
|
||||
//Добавление положительной дробной части
|
||||
data.temp += rawData[3] * 0.1f;
|
||||
} else {
|
||||
//А тут делаем отрицательное значение
|
||||
rawData[3] &= ~(1 << 7);
|
||||
data.temp += rawData[3] * 0.1f;
|
||||
data.temp *= -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if DHT_POLLING_CONTROL == 1
|
||||
sensor->lastHum = data.hum;
|
||||
sensor->lastTemp = data.temp;
|
||||
#endif
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
#ifndef DHT_H_
|
||||
#define DHT_H_
|
||||
|
||||
#include <furi_hal_resources.h>
|
||||
|
||||
/* Настройки */
|
||||
#define DHT_TIMEOUT 65534 //Количество итераций, после которых функция вернёт пустые значения
|
||||
#define DHT_POLLING_CONTROL 1 //Включение проверки частоты опроса датчика
|
||||
#define DHT_POLLING_INTERVAL_DHT11 \
|
||||
2000 //Интервал опроса DHT11 (0.5 Гц по даташиту). Можно поставить 1500, будет работать
|
||||
//Костыль, временно 2 секунды для датчика AM2302
|
||||
#define DHT_POLLING_INTERVAL_DHT22 2000 //Интервал опроса DHT22 (1 Гц по даташиту)
|
||||
#define DHT_IRQ_CONTROL //Выключать прерывания во время обмена данных с датчиком
|
||||
/* Структура возвращаемых датчиком данных */
|
||||
typedef struct {
|
||||
float hum;
|
||||
float temp;
|
||||
} DHT_data;
|
||||
|
||||
/* Тип используемого датчика */
|
||||
typedef enum { DHT11, DHT22 } DHT_type;
|
||||
|
||||
/* Структура объекта датчика */
|
||||
typedef struct {
|
||||
char name[11];
|
||||
const GpioPin* GPIO; //Пин датчика
|
||||
DHT_type type; //Тип датчика (DHT11 или DHT22)
|
||||
|
||||
//Контроль частоты опроса датчика. Значения не заполнять!
|
||||
#if DHT_POLLING_CONTROL == 1
|
||||
uint32_t lastPollingTime; //Время последнего опроса датчика
|
||||
float lastTemp; //Последнее значение температуры
|
||||
float lastHum; //Последнее значение влажности
|
||||
#endif
|
||||
} DHT_sensor;
|
||||
|
||||
/* Прототипы функций */
|
||||
DHT_data DHT_getData(DHT_sensor* sensor); //Получить данные с датчика
|
||||
|
||||
#endif
|
||||
@@ -1,13 +0,0 @@
|
||||
App(
|
||||
appid="DHT_Monitor",
|
||||
name="[DHT] Temp. Monitor",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="quenon_dht_mon_app",
|
||||
cdefines=["QUENON_DHT_MON"],
|
||||
requires=[
|
||||
"gui",
|
||||
],
|
||||
fap_category="GPIO",
|
||||
fap_icon="icon.png",
|
||||
stack_size=2 * 1024,
|
||||
)
|
||||
|
Before Width: | Height: | Size: 136 B |
@@ -1,469 +0,0 @@
|
||||
#include "quenon_dht_mon.h"
|
||||
#include <m-string.h>
|
||||
|
||||
//Порты ввода/вывода, которые не были обозначены в общем списке
|
||||
const GpioPin SWC_10 = {.pin = LL_GPIO_PIN_14, .port = GPIOA};
|
||||
const GpioPin SIO_12 = {.pin = LL_GPIO_PIN_13, .port = GPIOA};
|
||||
const GpioPin TX_13 = {.pin = LL_GPIO_PIN_6, .port = GPIOB};
|
||||
const GpioPin RX_14 = {.pin = LL_GPIO_PIN_7, .port = GPIOB};
|
||||
|
||||
//Количество доступных портов ввода/вывода
|
||||
#define GPIO_ITEMS (sizeof(gpio_item) / sizeof(GpioItem))
|
||||
|
||||
//Перечень достуных портов ввода/вывода
|
||||
static const GpioItem gpio_item[] = {
|
||||
{2, "2 (A7)", &gpio_ext_pa7},
|
||||
{3, "3 (A6)", &gpio_ext_pa6},
|
||||
{4, "4 (A4)", &gpio_ext_pa4},
|
||||
{5, "5 (B3)", &gpio_ext_pb3},
|
||||
{6, "6 (B2)", &gpio_ext_pb2},
|
||||
{7, "7 (C3)", &gpio_ext_pc3},
|
||||
{10, " 10(SWC) ", &SWC_10},
|
||||
{12, "12 (SIO)", &SIO_12},
|
||||
{13, "13 (TX)", &TX_13},
|
||||
{14, "14 (RX)", &RX_14},
|
||||
{15, "15 (C1)", &gpio_ext_pc1},
|
||||
{16, "16 (C0)", &gpio_ext_pc0},
|
||||
{17, "17 (1W)", &ibutton_gpio}};
|
||||
|
||||
//Данные плагина
|
||||
static PluginData* app;
|
||||
|
||||
uint8_t DHTMon_GPIO_to_int(const GpioPin* gpio) {
|
||||
if(gpio == NULL) return 255;
|
||||
for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
|
||||
if(gpio_item[i].pin->pin == gpio->pin && gpio_item[i].pin->port == gpio->port) {
|
||||
return gpio_item[i].num;
|
||||
}
|
||||
}
|
||||
return 255;
|
||||
}
|
||||
|
||||
const GpioPin* DHTMon_GPIO_form_int(uint8_t name) {
|
||||
for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
|
||||
if(gpio_item[i].num == name) {
|
||||
return gpio_item[i].pin;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const GpioPin* DHTMon_GPIO_from_index(uint8_t index) {
|
||||
if(index > GPIO_ITEMS) return NULL;
|
||||
return gpio_item[index].pin;
|
||||
}
|
||||
|
||||
uint8_t DHTMon_GPIO_to_index(const GpioPin* gpio) {
|
||||
if(gpio == NULL) return 255;
|
||||
for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
|
||||
if(gpio_item[i].pin->pin == gpio->pin && gpio_item[i].pin->port == gpio->port) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 255;
|
||||
}
|
||||
|
||||
const char* DHTMon_GPIO_getName(const GpioPin* gpio) {
|
||||
if(gpio == NULL) return NULL;
|
||||
for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
|
||||
if(gpio_item[i].pin->pin == gpio->pin && gpio_item[i].pin->port == gpio->port) {
|
||||
return gpio_item[i].name;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void DHTMon_sensors_init(void) {
|
||||
//Включение 5V если на порту 1 FZ его нет
|
||||
if(furi_hal_power_is_otg_enabled() != true) {
|
||||
furi_hal_power_enable_otg();
|
||||
}
|
||||
|
||||
//Настройка GPIO загруженных датчиков
|
||||
for(uint8_t i = 0; i < app->sensors_count; i++) {
|
||||
//Высокий уровень по умолчанию
|
||||
furi_hal_gpio_write(app->sensors[i].GPIO, true);
|
||||
//Режим работы - OpenDrain, подтяжка включается на всякий случай
|
||||
furi_hal_gpio_init(
|
||||
app->sensors[i].GPIO, //Порт FZ
|
||||
GpioModeOutputOpenDrain, //Режим работы - открытый сток
|
||||
GpioPullUp, //Принудительная подтяжка линии данных к питанию
|
||||
GpioSpeedVeryHigh); //Скорость работы - максимальная
|
||||
}
|
||||
}
|
||||
|
||||
void DHTMon_sensors_deinit(void) {
|
||||
//Возврат исходного состояния 5V
|
||||
if(app->last_OTG_State != true) {
|
||||
furi_hal_power_disable_otg();
|
||||
}
|
||||
|
||||
//Перевод портов GPIO в состояние по умолчанию
|
||||
for(uint8_t i = 0; i < app->sensors_count; i++) {
|
||||
furi_hal_gpio_init(
|
||||
app->sensors[i].GPIO, //Порт FZ
|
||||
GpioModeAnalog, //Режим работы - аналог
|
||||
GpioPullNo, //Отключение подтяжки
|
||||
GpioSpeedLow); //Скорость работы - низкая
|
||||
//Установка низкого уровня
|
||||
furi_hal_gpio_write(app->sensors[i].GPIO, false);
|
||||
}
|
||||
}
|
||||
|
||||
bool DHTMon_sensor_check(DHT_sensor* sensor) {
|
||||
/* Проверка имени */
|
||||
//1) Строка должна быть длиной от 1 до 10 символов
|
||||
//2) Первый символ строки должен быть только 0-9, A-Z, a-z и _
|
||||
if(strlen(sensor->name) == 0 || strlen(sensor->name) > 10 ||
|
||||
(!(sensor->name[0] >= '0' && sensor->name[0] <= '9') &&
|
||||
!(sensor->name[0] >= 'A' && sensor->name[0] <= 'Z') &&
|
||||
!(sensor->name[0] >= 'a' && sensor->name[0] <= 'z') && !(sensor->name[0] == '_'))) {
|
||||
FURI_LOG_D(APP_NAME, "Sensor [%s] name check failed\r\n", sensor->name);
|
||||
return false;
|
||||
}
|
||||
//Проверка GPIO
|
||||
if(DHTMon_GPIO_to_int(sensor->GPIO) == 255) {
|
||||
FURI_LOG_D(
|
||||
APP_NAME,
|
||||
"Sensor [%s] GPIO check failed: %d\r\n",
|
||||
sensor->name,
|
||||
DHTMon_GPIO_to_int(sensor->GPIO));
|
||||
return false;
|
||||
}
|
||||
//Проверка типа датчика
|
||||
if(sensor->type != DHT11 && sensor->type != DHT22) {
|
||||
FURI_LOG_D(APP_NAME, "Sensor [%s] type check failed: %d\r\n", sensor->name, sensor->type);
|
||||
return false;
|
||||
}
|
||||
|
||||
//Возврат истины если всё ок
|
||||
FURI_LOG_D(APP_NAME, "Sensor [%s] all checks passed\r\n", sensor->name);
|
||||
return true;
|
||||
}
|
||||
|
||||
void DHTMon_sensor_delete(DHT_sensor* sensor) {
|
||||
if(sensor == NULL) return;
|
||||
//Делаем параметры датчика неверными
|
||||
sensor->name[0] = '\0';
|
||||
sensor->type = 255;
|
||||
//Теперь сохраняем текущие датчики. Сохранятор не сохранит неисправный датчик
|
||||
DHTMon_sensors_save();
|
||||
//Перезагружаемся с SD-карты
|
||||
DHTMon_sensors_reload();
|
||||
}
|
||||
|
||||
uint8_t DHTMon_sensors_save(void) {
|
||||
//Выделение памяти для потока
|
||||
app->file_stream = file_stream_alloc(app->storage);
|
||||
uint8_t savedSensorsCount = 0;
|
||||
//Переменная пути к файлу
|
||||
FuriString* filepath = furi_string_alloc();
|
||||
//Составление пути к файлу
|
||||
furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME);
|
||||
|
||||
//Открытие потока. Если поток открылся, то выполнение сохранения датчиков
|
||||
if(file_stream_open(
|
||||
app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS)) {
|
||||
const char template[] =
|
||||
"#DHT monitor sensors file\n#Name - name of sensor. Up to 10 sumbols\n#Type - type of sensor. DHT11 - 0, DHT22 - 1\n#GPIO - connection port. May being 2-7, 10, 12-17\n#Name Type GPIO\n";
|
||||
stream_write(app->file_stream, (uint8_t*)template, strlen(template));
|
||||
//Сохранение датчиков
|
||||
for(uint8_t i = 0; i < app->sensors_count; i++) {
|
||||
//Если параметры датчика верны, то сохраняемся
|
||||
if(DHTMon_sensor_check(&app->sensors[i])) {
|
||||
stream_write_format(
|
||||
app->file_stream,
|
||||
"%s %d %d\n",
|
||||
app->sensors[i].name,
|
||||
app->sensors[i].type,
|
||||
DHTMon_GPIO_to_int(app->sensors[i].GPIO));
|
||||
savedSensorsCount++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//TODO: печать ошибки на экран
|
||||
FURI_LOG_E(APP_NAME, "cannot create sensors file\r\n");
|
||||
}
|
||||
stream_free(app->file_stream);
|
||||
|
||||
return savedSensorsCount;
|
||||
}
|
||||
|
||||
bool DHTMon_sensors_load(void) {
|
||||
//Обнуление количества датчиков
|
||||
app->sensors_count = -1;
|
||||
//Очистка предыдущих датчиков
|
||||
memset(app->sensors, 0, sizeof(app->sensors));
|
||||
|
||||
//Открытие файла на SD-карте
|
||||
//Выделение памяти для потока
|
||||
app->file_stream = file_stream_alloc(app->storage);
|
||||
//Переменная пути к файлу
|
||||
FuriString* filepath = furi_string_alloc();
|
||||
//Составление пути к файлу
|
||||
furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME);
|
||||
//Открытие потока к файлу
|
||||
if(!file_stream_open(
|
||||
app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
|
||||
//Если файл отсутствует, то создание болванки
|
||||
FURI_LOG_W(APP_NAME, "Missing sensors file. Creating new file\r\n");
|
||||
app->sensors_count = 0;
|
||||
stream_free(app->file_stream);
|
||||
DHTMon_sensors_save();
|
||||
return false;
|
||||
}
|
||||
//Вычисление размера файла
|
||||
size_t file_size = stream_size(app->file_stream);
|
||||
if(file_size == (size_t)0) {
|
||||
//Выход если файл пустой
|
||||
FURI_LOG_W(APP_NAME, "Sensors file is empty\r\n");
|
||||
app->sensors_count = 0;
|
||||
stream_free(app->file_stream);
|
||||
return false;
|
||||
}
|
||||
|
||||
//Выделение памяти под загрузку файла
|
||||
uint8_t* file_buf = malloc(file_size);
|
||||
//Опустошение буфера файла
|
||||
memset(file_buf, 0, file_size);
|
||||
//Загрузка файла
|
||||
if(stream_read(app->file_stream, file_buf, file_size) != file_size) {
|
||||
//Выход при ошибке чтения
|
||||
FURI_LOG_E(APP_NAME, "Error reading sensor file\r\n");
|
||||
app->sensors_count = 0;
|
||||
stream_free(app->file_stream);
|
||||
return false;
|
||||
}
|
||||
//Построчное чтение файла
|
||||
//Указатель на начало строки
|
||||
FuriString* file = furi_string_alloc_set_str((char*)file_buf);
|
||||
//Сколько байт до конца строки
|
||||
size_t line_end = 0;
|
||||
while(line_end != STRING_FAILURE && app->sensors_count < MAX_SENSORS) {
|
||||
if(((char*)(file_buf + line_end))[1] != '#') {
|
||||
DHT_sensor s = {0};
|
||||
int type, port;
|
||||
char name[11] = {0};
|
||||
sscanf(((char*)(file_buf + line_end)), "%s %d %d", name, &type, &port);
|
||||
s.type = type;
|
||||
s.GPIO = DHTMon_GPIO_form_int(port);
|
||||
|
||||
name[10] = '\0';
|
||||
strcpy(s.name, name);
|
||||
//Если данные корректны, то
|
||||
if(DHTMon_sensor_check(&s) == true) {
|
||||
//Установка нуля при первом датчике
|
||||
if(app->sensors_count == -1) app->sensors_count = 0;
|
||||
//Добавление датчика в общий список
|
||||
app->sensors[app->sensors_count] = s;
|
||||
//Увеличение количества загруженных датчиков
|
||||
app->sensors_count++;
|
||||
}
|
||||
}
|
||||
line_end = furi_string_search_char(file, '\n', line_end + 1);
|
||||
}
|
||||
stream_free(app->file_stream);
|
||||
free(file_buf);
|
||||
|
||||
//Обнуление количества датчиков если ни один из них не был загружен
|
||||
if(app->sensors_count == -1) app->sensors_count = 0;
|
||||
|
||||
//Инициализация портов датчиков если таковые есть
|
||||
if(app->sensors_count > 0) {
|
||||
DHTMon_sensors_init();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DHTMon_sensors_reload(void) {
|
||||
DHTMon_sensors_deinit();
|
||||
return DHTMon_sensors_load();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Обработчик отрисовки экрана
|
||||
*
|
||||
* @param canvas Указатель на холст
|
||||
* @param ctx Данные плагина
|
||||
*/
|
||||
static void render_callback(Canvas* const canvas, void* ctx) {
|
||||
PluginData* app = acquire_mutex((ValueMutex*)ctx, 25);
|
||||
if(app == NULL) {
|
||||
return;
|
||||
}
|
||||
//Вызов отрисовки главного экрана
|
||||
scene_main(canvas, app);
|
||||
|
||||
release_mutex((ValueMutex*)ctx, app);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Обработчик нажатия кнопок главного экрана
|
||||
*
|
||||
* @param input_event Указатель на событие
|
||||
* @param event_queue Указатель на очередь событий
|
||||
*/
|
||||
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
|
||||
furi_assert(event_queue);
|
||||
|
||||
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
|
||||
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Выделение места под переменные плагина
|
||||
*
|
||||
* @return true Если всё прошло успешно
|
||||
* @return false Если в процессе загрузки произошла ошибка
|
||||
*/
|
||||
static bool DHTMon_alloc(void) {
|
||||
//Выделение места под данные плагина
|
||||
app = malloc(sizeof(PluginData));
|
||||
//Выделение места под очередь событий
|
||||
app->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
|
||||
|
||||
//Обнуление количества датчиков
|
||||
app->sensors_count = -1;
|
||||
|
||||
//Инициализация мутекса
|
||||
if(!init_mutex(&app->state_mutex, app, sizeof(PluginData))) {
|
||||
FURI_LOG_E(APP_NAME, "cannot create mutex\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set system callbacks
|
||||
app->view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(app->view_port, render_callback, &app->state_mutex);
|
||||
view_port_input_callback_set(app->view_port, input_callback, app->event_queue);
|
||||
|
||||
// Open GUI and register view_port
|
||||
app->gui = furi_record_open(RECORD_GUI);
|
||||
gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
|
||||
|
||||
app->view_dispatcher = view_dispatcher_alloc();
|
||||
|
||||
sensorActions_sceneCreate(app);
|
||||
sensorEdit_sceneCreate(app);
|
||||
|
||||
app->widget = widget_alloc();
|
||||
view_dispatcher_add_view(app->view_dispatcher, WIDGET_VIEW, widget_get_view(app->widget));
|
||||
|
||||
app->text_input = text_input_alloc();
|
||||
view_dispatcher_add_view(
|
||||
app->view_dispatcher, TEXTINPUT_VIEW, text_input_get_view(app->text_input));
|
||||
|
||||
view_dispatcher_enable_queue(app->view_dispatcher);
|
||||
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
|
||||
|
||||
//Уведомления
|
||||
app->notifications = furi_record_open(RECORD_NOTIFICATION);
|
||||
|
||||
//Подготовка хранилища
|
||||
app->storage = furi_record_open(RECORD_STORAGE);
|
||||
storage_common_mkdir(app->storage, APP_PATH_FOLDER);
|
||||
app->file_stream = file_stream_alloc(app->storage);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Освыбождение памяти после работы приложения
|
||||
*/
|
||||
static void DHTMon_free(void) {
|
||||
//Автоматическое управление подсветкой
|
||||
notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
|
||||
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
furi_record_close(RECORD_NOTIFICATION);
|
||||
|
||||
text_input_free(app->text_input);
|
||||
widget_free(app->widget);
|
||||
sensorEdit_sceneRemove();
|
||||
sensorActions_screneRemove();
|
||||
view_dispatcher_free(app->view_dispatcher);
|
||||
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
view_port_enabled_set(app->view_port, false);
|
||||
gui_remove_view_port(app->gui, app->view_port);
|
||||
|
||||
view_port_free(app->view_port);
|
||||
furi_message_queue_free(app->event_queue);
|
||||
delete_mutex(&app->state_mutex);
|
||||
|
||||
free(app);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Точка входа в приложение
|
||||
*
|
||||
* @return Код ошибки
|
||||
*/
|
||||
int32_t quenon_dht_mon_app() {
|
||||
if(!DHTMon_alloc()) {
|
||||
DHTMon_free();
|
||||
return 255;
|
||||
}
|
||||
//Постоянное свечение подсветки
|
||||
notification_message(app->notifications, &sequence_display_backlight_enforce_on);
|
||||
//Сохранение состояния наличия 5V на порту 1 FZ
|
||||
app->last_OTG_State = furi_hal_power_is_otg_enabled();
|
||||
|
||||
//Загрузка датчиков с SD-карты
|
||||
DHTMon_sensors_load();
|
||||
|
||||
app->currentSensorEdit = &app->sensors[0];
|
||||
|
||||
PluginEvent event;
|
||||
for(bool processing = true; processing;) {
|
||||
FuriStatus event_status = furi_message_queue_get(app->event_queue, &event, 100);
|
||||
|
||||
acquire_mutex_block(&app->state_mutex);
|
||||
|
||||
if(event_status == FuriStatusOk) {
|
||||
// press events
|
||||
if(event.type == EventTypeKey) {
|
||||
if(event.input.type == InputTypePress) {
|
||||
switch(event.input.key) {
|
||||
case InputKeyUp:
|
||||
break;
|
||||
case InputKeyDown:
|
||||
break;
|
||||
case InputKeyRight:
|
||||
break;
|
||||
case InputKeyLeft:
|
||||
break;
|
||||
case InputKeyMAX:
|
||||
break;
|
||||
case InputKeyOk:
|
||||
view_port_update(app->view_port);
|
||||
release_mutex(&app->state_mutex, app);
|
||||
mainMenu_scene(app);
|
||||
break;
|
||||
case InputKeyBack:
|
||||
processing = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_D(APP_NAME, "FuriMessageQueue: event timeout");
|
||||
// event timeout
|
||||
}
|
||||
|
||||
view_port_update(app->view_port);
|
||||
release_mutex(&app->state_mutex, app);
|
||||
}
|
||||
//Освобождение памяти и деинициализация
|
||||
DHTMon_sensors_deinit();
|
||||
DHTMon_free();
|
||||
|
||||
return 0;
|
||||
}
|
||||
//TODO: Обработка ошибок
|
||||
//TODO: Пропуск использованных портов в меню добавления датчиков
|
||||
@@ -1,176 +0,0 @@
|
||||
#ifndef QUENON_DHT_MON
|
||||
#define QUENON_DHT_MON
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal_power.h>
|
||||
|
||||
#include <gui/gui.h>
|
||||
#include <gui/modules/submenu.h>
|
||||
#include <gui/modules/variable_item_list.h>
|
||||
#include <gui/modules/text_input.h>
|
||||
#include <gui/modules/widget.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
#include <gui/scene_manager.h>
|
||||
|
||||
#include <notification/notification.h>
|
||||
#include <notification/notification_messages.h>
|
||||
|
||||
#include <toolbox/stream/file_stream.h>
|
||||
#include <input/input.h>
|
||||
|
||||
#include "DHT.h"
|
||||
|
||||
#define APP_NAME "DHT_monitor"
|
||||
#define APP_PATH_FOLDER "/ext/dht_monitor"
|
||||
#define APP_FILENAME "sensors.txt"
|
||||
#define MAX_SENSORS 5
|
||||
|
||||
// //Виды менюшек
|
||||
typedef enum {
|
||||
MAIN_MENU_VIEW,
|
||||
ADDSENSOR_MENU_VIEW,
|
||||
TEXTINPUT_VIEW,
|
||||
SENSOR_ACTIONS_VIEW,
|
||||
WIDGET_VIEW,
|
||||
} MENU_VIEWS;
|
||||
|
||||
typedef enum {
|
||||
EventTypeTick,
|
||||
EventTypeKey,
|
||||
} EventType;
|
||||
|
||||
typedef struct {
|
||||
EventType type;
|
||||
InputEvent input;
|
||||
} PluginEvent;
|
||||
|
||||
typedef struct {
|
||||
const uint8_t num;
|
||||
const char* name;
|
||||
const GpioPin* pin;
|
||||
} GpioItem;
|
||||
|
||||
//Структура с данными плагина
|
||||
typedef struct {
|
||||
//Очередь сообщений
|
||||
FuriMessageQueue* event_queue;
|
||||
//Мутекс
|
||||
ValueMutex state_mutex;
|
||||
//Вьюпорт
|
||||
ViewPort* view_port;
|
||||
//GUI
|
||||
Gui* gui;
|
||||
NotificationApp* notifications;
|
||||
ViewDispatcher* view_dispatcher;
|
||||
View* view;
|
||||
TextInput* text_input;
|
||||
VariableItem* item;
|
||||
Widget* widget;
|
||||
|
||||
char txtbuff[30]; //Буффер для печати строк на экране
|
||||
bool last_OTG_State; //Состояние OTG до запуска приложения
|
||||
Storage* storage; //Хранилище датчиков
|
||||
Stream* file_stream; //Поток файла с датчиками
|
||||
int8_t sensors_count; // Количество загруженных датчиков
|
||||
DHT_sensor sensors[MAX_SENSORS]; //Сохранённые датчики
|
||||
DHT_data data; //Инфа из датчика
|
||||
DHT_sensor* currentSensorEdit; //Указатель на редактируемый датчик
|
||||
|
||||
} PluginData;
|
||||
|
||||
/* ================== Работа с GPIO ================== */
|
||||
/**
|
||||
* @brief Конвертация GPIO в его номер на корпусе FZ
|
||||
*
|
||||
* @param gpio Указатель на преобразовываемый GPIO
|
||||
* @return Номер порта на корпусе FZ
|
||||
*/
|
||||
uint8_t DHTMon_GPIO_to_int(const GpioPin* gpio);
|
||||
/**
|
||||
* @brief Конвертация номера порта на корпусе FZ в GPIO
|
||||
*
|
||||
* @param name Номер порта на корпусе FZ
|
||||
* @return Указатель на GPIO при успехе, NULL при ошибке
|
||||
*/
|
||||
const GpioPin* DHTMon_GPIO_form_int(uint8_t name);
|
||||
/**
|
||||
* @brief Преобразование порядкового номера порта в GPIO
|
||||
*
|
||||
* @param index Индекс порта от 0 до GPIO_ITEMS-1
|
||||
* @return Указатель на GPIO при успехе, NULL при ошибке
|
||||
*/
|
||||
const GpioPin* DHTMon_GPIO_from_index(uint8_t index);
|
||||
/**
|
||||
* @brief Преобразование GPIO в порядковый номер порта
|
||||
*
|
||||
* @param gpio Указатель на GPIO
|
||||
* @return index при успехе, 255 при ошибке
|
||||
*/
|
||||
uint8_t DHTMon_GPIO_to_index(const GpioPin* gpio);
|
||||
|
||||
/**
|
||||
* @brief Получить имя GPIO в виде строки
|
||||
*
|
||||
* @param gpio Искомый порт
|
||||
* @return char* Указатель на строку с именем порта
|
||||
*/
|
||||
const char* DHTMon_GPIO_getName(const GpioPin* gpio);
|
||||
|
||||
/* ================== Работа с датчиками ================== */
|
||||
/**
|
||||
* @brief Инициализация портов ввода/вывода датчиков
|
||||
*/
|
||||
void DHTMon_sensors_init(void);
|
||||
/**
|
||||
* @brief Функция деинициализации портов ввода/вывода датчиков
|
||||
*/
|
||||
void DHTMon_sensors_deinit(void);
|
||||
/**
|
||||
* @brief Проверка корректности параметров датчика
|
||||
*
|
||||
* @param sensor Указатель на проверяемый датчик
|
||||
* @return true Параметры датчика корректные
|
||||
* @return false Параметры датчика некорректные
|
||||
*/
|
||||
bool DHTMon_sensor_check(DHT_sensor* sensor);
|
||||
/**
|
||||
* @brief Удаление датчика из списка и перезагрузка
|
||||
*
|
||||
* @param sensor Указатель на удаляемый датчик
|
||||
*/
|
||||
void DHTMon_sensor_delete(DHT_sensor* sensor);
|
||||
/**
|
||||
* @brief Сохранение датчиков на SD-карту
|
||||
*
|
||||
* @return Количество сохранённых датчиков
|
||||
*/
|
||||
uint8_t DHTMon_sensors_save(void);
|
||||
/**
|
||||
* @brief Загрузка датчиков с SD-карты
|
||||
*
|
||||
* @return true Был загружен хотя бы 1 датчик
|
||||
* @return false Датчики отсутствуют
|
||||
*/
|
||||
bool DHTMon_sensors_load(void);
|
||||
/**
|
||||
* @brief Перезагрузка датчиков с SD-карты
|
||||
*
|
||||
* @return true Когда был загружен хотя бы 1 датчик
|
||||
* @return false Ни один из датчиков не был загружен
|
||||
*/
|
||||
bool DHTMon_sensors_reload(void);
|
||||
|
||||
void scene_main(Canvas* const canvas, PluginData* app);
|
||||
void mainMenu_scene(PluginData* app);
|
||||
|
||||
void sensorEdit_sceneCreate(PluginData* app);
|
||||
void sensorEdit_scene(PluginData* app);
|
||||
void sensorEdit_sceneRemove(void);
|
||||
|
||||
void sensorActions_sceneCreate(PluginData* app);
|
||||
void sensorActions_scene(PluginData* app);
|
||||
void sensorActions_screneRemove(void);
|
||||
#endif
|
||||
@@ -1,157 +0,0 @@
|
||||
#include "../quenon_dht_mon.h"
|
||||
//Текущий вид
|
||||
static View* view;
|
||||
//Список
|
||||
static VariableItemList* variable_item_list;
|
||||
|
||||
/**
|
||||
* @brief Функция обработки нажатия кнопки "Назад"
|
||||
*
|
||||
* @param context Указатель на данные приложения
|
||||
* @return ID вида в который нужно переключиться
|
||||
*/
|
||||
static uint32_t actions_exitCallback(void* context) {
|
||||
PluginData* app = context;
|
||||
UNUSED(app);
|
||||
//Возвращаем ID вида, в который нужно вернуться
|
||||
return VIEW_NONE;
|
||||
}
|
||||
/**
|
||||
* @brief Функция обработки нажатия средней кнопки
|
||||
*
|
||||
* @param context Указатель на данные приложения
|
||||
* @param index На каком элементе списка была нажата кнопка
|
||||
*/
|
||||
static void enterCallback(void* context, uint32_t index) {
|
||||
PluginData* app = context;
|
||||
if((uint8_t)index < (uint8_t)app->sensors_count) {
|
||||
app->currentSensorEdit = &app->sensors[index];
|
||||
sensorActions_scene(app);
|
||||
}
|
||||
if((uint8_t)index == (uint8_t)app->sensors_count) {
|
||||
app->currentSensorEdit = &app->sensors[app->sensors_count++];
|
||||
strcpy(app->currentSensorEdit->name, "NewSensor");
|
||||
app->currentSensorEdit->GPIO = DHTMon_GPIO_from_index(0);
|
||||
app->currentSensorEdit->type = DHT11;
|
||||
sensorEdit_scene(app);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Создание списка действий с указанным датчиком
|
||||
*
|
||||
* @param app Указатель на данные плагина
|
||||
*/
|
||||
void mainMenu_scene(PluginData* app) {
|
||||
variable_item_list = variable_item_list_alloc();
|
||||
//Сброс всех элементов меню
|
||||
variable_item_list_reset(variable_item_list);
|
||||
//Добавление названий датчиков в качестве элементов списка
|
||||
for(uint8_t i = 0; i < app->sensors_count; i++) {
|
||||
variable_item_list_add(variable_item_list, app->sensors[i].name, 1, NULL, NULL);
|
||||
}
|
||||
if(app->sensors_count < (uint8_t)MAX_SENSORS) {
|
||||
variable_item_list_add(variable_item_list, " + Add new sensor +", 1, NULL, NULL);
|
||||
}
|
||||
|
||||
//Добавление колбека на нажатие средней кнопки
|
||||
variable_item_list_set_enter_callback(variable_item_list, enterCallback, app);
|
||||
|
||||
//Создание вида из списка
|
||||
view = variable_item_list_get_view(variable_item_list);
|
||||
//Добавление колбека на нажатие кнопки "Назад"
|
||||
view_set_previous_callback(view, actions_exitCallback);
|
||||
//Добавление вида в диспетчер
|
||||
view_dispatcher_add_view(app->view_dispatcher, MAIN_MENU_VIEW, view);
|
||||
|
||||
view_dispatcher_enable_queue(app->view_dispatcher);
|
||||
|
||||
//Переключение на наш вид
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, MAIN_MENU_VIEW);
|
||||
|
||||
//Запуск диспетчера
|
||||
view_dispatcher_run(app->view_dispatcher);
|
||||
|
||||
//Очистка списка элементов
|
||||
variable_item_list_free(variable_item_list);
|
||||
//Удаление вида после обработки
|
||||
view_dispatcher_remove_view(app->view_dispatcher, MAIN_MENU_VIEW);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
// static VariableItemList* variable_item_list;
|
||||
// /* ============== Главное меню ============== */
|
||||
// static uint32_t mainMenu_exitCallback(void* context) {
|
||||
// UNUSED(context);
|
||||
// variable_item_list_free(variable_item_list);
|
||||
// DHT_sensors_reload();
|
||||
// return VIEW_NONE;
|
||||
// }
|
||||
// static void mainMenu_enterCallback(void* context, uint32_t index) {
|
||||
// PluginData* app = context;
|
||||
// if((uint8_t)index == (uint8_t)app->sensors_count) {
|
||||
// addSensor_scene(app);
|
||||
// view_dispatcher_run(app->view_dispatcher);
|
||||
// }
|
||||
// }
|
||||
// void mainMenu_scene(PluginData* app) {
|
||||
// variable_item_list = variable_item_list_alloc();
|
||||
// variable_item_list_reset(variable_item_list);
|
||||
// for(uint8_t i = 0; i < app->sensors_count; i++) {
|
||||
// variable_item_list_add(variable_item_list, app->sensors[i].name, 1, NULL, NULL);
|
||||
// }
|
||||
// variable_item_list_add(variable_item_list, "+ Add new sensor +", 1, NULL, NULL);
|
||||
|
||||
// app->view = variable_item_list_get_view(variable_item_list);
|
||||
// app->view_dispatcher = view_dispatcher_alloc();
|
||||
|
||||
// view_dispatcher_enable_queue(app->view_dispatcher);
|
||||
// view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
|
||||
// view_dispatcher_add_view(app->view_dispatcher, MAIN_MENU_VIEW, app->view);
|
||||
// view_dispatcher_switch_to_view(app->view_dispatcher, MAIN_MENU_VIEW);
|
||||
|
||||
// variable_item_list_set_enter_callback(variable_item_list, mainMenu_enterCallback, app);
|
||||
// view_set_previous_callback(app->view, mainMenu_exitCallback);
|
||||
// }
|
||||
@@ -1,40 +0,0 @@
|
||||
#include "../quenon_dht_mon.h"
|
||||
|
||||
/* ============== Главный экран ============== */
|
||||
void scene_main(Canvas* const canvas, PluginData* app) {
|
||||
//Рисование бара
|
||||
canvas_draw_box(canvas, 0, 0, 128, 14);
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 32, 11, "DHT Monitor");
|
||||
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
if(app->sensors_count > 0) {
|
||||
if(!furi_hal_power_is_otg_enabled()) {
|
||||
furi_hal_power_enable_otg();
|
||||
}
|
||||
for(uint8_t i = 0; i < app->sensors_count; i++) {
|
||||
app->data = DHT_getData(&app->sensors[i]);
|
||||
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 0, 24 + 10 * i, app->sensors[i].name);
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
if(app->data.hum == -128.0f && app->data.temp == -128.0f) {
|
||||
canvas_draw_str(canvas, 96, 24 + 10 * i, "timeout");
|
||||
} else {
|
||||
snprintf(
|
||||
app->txtbuff,
|
||||
sizeof(app->txtbuff),
|
||||
"%2.1f*C/%d%%",
|
||||
(double)app->data.temp,
|
||||
(int8_t)app->data.hum);
|
||||
canvas_draw_str(canvas, 64, 24 + 10 * i, app->txtbuff);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
if(app->sensors_count == 0) canvas_draw_str(canvas, 0, 24, "Sensors not found");
|
||||
if(app->sensors_count == -1) canvas_draw_str(canvas, 0, 24, "Loading...");
|
||||
}
|
||||
}
|
||||
@@ -1,194 +0,0 @@
|
||||
#include "../quenon_dht_mon.h"
|
||||
|
||||
//Текущий вид
|
||||
static View* view;
|
||||
//Список
|
||||
static VariableItemList* variable_item_list;
|
||||
|
||||
/* ================== Информация о датчике ================== */
|
||||
/**
|
||||
* @brief Функция обработки нажатия кнопки "Назад"
|
||||
*
|
||||
* @param context Указатель на данные приложения
|
||||
* @return ID вида в который нужно переключиться
|
||||
*/
|
||||
static uint32_t infoWidget_exitCallback(void* context) {
|
||||
PluginData* app = context;
|
||||
UNUSED(app);
|
||||
//Возвращаем ID вида, в который нужно вернуться
|
||||
return SENSOR_ACTIONS_VIEW;
|
||||
}
|
||||
/**
|
||||
* @brief Обработчик нажатий на кнопку в виджете
|
||||
*
|
||||
* @param result Какая из кнопок была нажата
|
||||
* @param type Тип нажатия
|
||||
* @param context Указатель на данные плагина
|
||||
*/
|
||||
static void infoWidget_callback(GuiButtonType result, InputType type, void* context) {
|
||||
PluginData* app = context;
|
||||
//Коротко нажата левая кнопка (Back)
|
||||
if(result == GuiButtonTypeLeft && type == InputTypeShort) {
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, SENSOR_ACTIONS_VIEW);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief Создание виджета информации о датчике
|
||||
*
|
||||
* @param app Указатель на данные плагина
|
||||
*/
|
||||
static void sensorInfo_widget(PluginData* app) {
|
||||
//Очистка виджета
|
||||
widget_reset(app->widget);
|
||||
//Добавление кнопок
|
||||
widget_add_button_element(app->widget, GuiButtonTypeLeft, "Back", infoWidget_callback, app);
|
||||
|
||||
char str[32];
|
||||
snprintf(str, sizeof(str), "\e#%s\e#", app->currentSensorEdit->name);
|
||||
widget_add_text_box_element(app->widget, 0, 0, 128, 23, AlignCenter, AlignCenter, str, false);
|
||||
snprintf(str, sizeof(str), "\e#Type:\e# %s", app->currentSensorEdit->type ? "DHT22" : "DHT11");
|
||||
widget_add_text_box_element(app->widget, 0, 0, 128, 47, AlignLeft, AlignCenter, str, false);
|
||||
snprintf(
|
||||
str, sizeof(str), "\e#GPIO:\e# %s", DHTMon_GPIO_getName(app->currentSensorEdit->GPIO));
|
||||
widget_add_text_box_element(app->widget, 0, 0, 128, 72, AlignLeft, AlignCenter, str, false);
|
||||
view_set_previous_callback(widget_get_view(app->widget), infoWidget_exitCallback);
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, WIDGET_VIEW);
|
||||
}
|
||||
|
||||
/* ================== Подтверждение удаления ================== */
|
||||
/**
|
||||
* @brief Функция обработки нажатия кнопки "Назад"
|
||||
*
|
||||
* @param context Указатель на данные приложения
|
||||
* @return ID вида в который нужно переключиться
|
||||
*/
|
||||
static uint32_t deleteWidget_exitCallback(void* context) {
|
||||
PluginData* app = context;
|
||||
UNUSED(app);
|
||||
//Возвращаем ID вида, в который нужно вернуться
|
||||
return SENSOR_ACTIONS_VIEW;
|
||||
}
|
||||
/**
|
||||
* @brief Обработчик нажатий на кнопку в виджете
|
||||
*
|
||||
* @param result Какая из кнопок была нажата
|
||||
* @param type Тип нажатия
|
||||
* @param context Указатель на данные плагина
|
||||
*/
|
||||
static void deleteWidget_callback(GuiButtonType result, InputType type, void* context) {
|
||||
PluginData* app = context;
|
||||
//Коротко нажата левая кнопка (Cancel)
|
||||
if(result == GuiButtonTypeLeft && type == InputTypeShort) {
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, SENSOR_ACTIONS_VIEW);
|
||||
}
|
||||
//Коротко нажата правая кнопка (Delete)
|
||||
if(result == GuiButtonTypeRight && type == InputTypeShort) {
|
||||
//Удаление датчика
|
||||
DHTMon_sensor_delete(app->currentSensorEdit);
|
||||
//Выход из меню
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_NONE);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief Создание виджета удаления датчика
|
||||
*
|
||||
* @param app Указатель на данные плагина
|
||||
*/
|
||||
static void sensorDelete_widget(PluginData* app) {
|
||||
//Очистка виджета
|
||||
widget_reset(app->widget);
|
||||
//Добавление кнопок
|
||||
widget_add_button_element(
|
||||
app->widget, GuiButtonTypeLeft, "Cancel", deleteWidget_callback, app);
|
||||
widget_add_button_element(
|
||||
app->widget, GuiButtonTypeRight, "Delete", deleteWidget_callback, app);
|
||||
|
||||
char delete_str[32];
|
||||
snprintf(delete_str, sizeof(delete_str), "\e#Delete %s?\e#", app->currentSensorEdit->name);
|
||||
widget_add_text_box_element(
|
||||
app->widget, 0, 0, 128, 23, AlignCenter, AlignCenter, delete_str, false);
|
||||
snprintf(
|
||||
delete_str,
|
||||
sizeof(delete_str),
|
||||
"\e#Type:\e# %s",
|
||||
app->currentSensorEdit->type ? "DHT22" : "DHT11");
|
||||
widget_add_text_box_element(
|
||||
app->widget, 0, 0, 128, 47, AlignLeft, AlignCenter, delete_str, false);
|
||||
snprintf(
|
||||
delete_str,
|
||||
sizeof(delete_str),
|
||||
"\e#GPIO:\e# %s",
|
||||
DHTMon_GPIO_getName(app->currentSensorEdit->GPIO));
|
||||
widget_add_text_box_element(
|
||||
app->widget, 0, 0, 128, 72, AlignLeft, AlignCenter, delete_str, false);
|
||||
view_set_previous_callback(widget_get_view(app->widget), deleteWidget_exitCallback);
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, WIDGET_VIEW);
|
||||
}
|
||||
|
||||
/* ================== Меню действий ================== */
|
||||
/**
|
||||
* @brief Функция обработки нажатия средней кнопки
|
||||
*
|
||||
* @param context Указатель на данные приложения
|
||||
* @param index На каком элементе списка была нажата кнопка
|
||||
*/
|
||||
static void enterCallback(void* context, uint32_t index) {
|
||||
PluginData* app = context;
|
||||
if(index == 0) {
|
||||
sensorInfo_widget(app);
|
||||
}
|
||||
if(index == 1) {
|
||||
sensorEdit_scene(app);
|
||||
}
|
||||
if(index == 2) {
|
||||
sensorDelete_widget(app);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Функция обработки нажатия кнопки "Назад"
|
||||
*
|
||||
* @param context Указатель на данные приложения
|
||||
* @return ID вида в который нужно переключиться
|
||||
*/
|
||||
static uint32_t actions_exitCallback(void* context) {
|
||||
PluginData* app = context;
|
||||
UNUSED(app);
|
||||
//Возвращаем ID вида, в который нужно вернуться
|
||||
return MAIN_MENU_VIEW;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Создание списка действий с указанным датчиком
|
||||
*
|
||||
* @param app Указатель на данные плагина
|
||||
*/
|
||||
void sensorActions_sceneCreate(PluginData* app) {
|
||||
variable_item_list = variable_item_list_alloc();
|
||||
//Сброс всех элементов меню
|
||||
variable_item_list_reset(variable_item_list);
|
||||
//Добавление элементов в список
|
||||
variable_item_list_add(variable_item_list, "Info", 0, NULL, NULL);
|
||||
variable_item_list_add(variable_item_list, "Edit", 0, NULL, NULL);
|
||||
variable_item_list_add(variable_item_list, "Delete", 0, NULL, NULL);
|
||||
|
||||
//Добавление колбека на нажатие средней кнопки
|
||||
variable_item_list_set_enter_callback(variable_item_list, enterCallback, app);
|
||||
|
||||
//Создание вида из списка
|
||||
view = variable_item_list_get_view(variable_item_list);
|
||||
//Добавление колбека на нажатие кнопки "Назад"
|
||||
view_set_previous_callback(view, actions_exitCallback);
|
||||
//Добавление вида в диспетчер
|
||||
view_dispatcher_add_view(app->view_dispatcher, SENSOR_ACTIONS_VIEW, view);
|
||||
}
|
||||
void sensorActions_scene(PluginData* app) {
|
||||
//Сброс выбранного пункта в ноль
|
||||
variable_item_list_set_selected_item(variable_item_list, 0);
|
||||
//Переключение на наш вид
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, SENSOR_ACTIONS_VIEW);
|
||||
}
|
||||
|
||||
void sensorActions_screneRemove(void) {
|
||||
variable_item_list_free(variable_item_list);
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
#include "../quenon_dht_mon.h"
|
||||
|
||||
static VariableItem* nameItem;
|
||||
static VariableItemList* variable_item_list;
|
||||
|
||||
static const char* const sensorsTypes[2] = {
|
||||
"DHT11",
|
||||
"DHT22",
|
||||
};
|
||||
|
||||
// /* ============== Добавление датчика ============== */
|
||||
static uint32_t addSensor_exitCallback(void* context) {
|
||||
UNUSED(context);
|
||||
DHTMon_sensors_reload();
|
||||
return VIEW_NONE;
|
||||
}
|
||||
|
||||
static void addSensor_sensorTypeChanged(VariableItem* item) {
|
||||
uint8_t index = variable_item_get_current_value_index(item);
|
||||
PluginData* app = variable_item_get_context(item);
|
||||
variable_item_set_current_value_text(item, sensorsTypes[index]);
|
||||
app->currentSensorEdit->type = index;
|
||||
}
|
||||
|
||||
static void addSensor_GPIOChanged(VariableItem* item) {
|
||||
uint8_t index = variable_item_get_current_value_index(item);
|
||||
variable_item_set_current_value_text(item, DHTMon_GPIO_getName(DHTMon_GPIO_from_index(index)));
|
||||
PluginData* app = variable_item_get_context(item);
|
||||
app->currentSensorEdit->GPIO = DHTMon_GPIO_from_index(index);
|
||||
}
|
||||
|
||||
static void addSensor_sensorNameChanged(void* context) {
|
||||
PluginData* app = context;
|
||||
variable_item_set_current_value_text(nameItem, app->currentSensorEdit->name);
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, ADDSENSOR_MENU_VIEW);
|
||||
}
|
||||
static void addSensor_sensorNameChange(PluginData* app) {
|
||||
text_input_set_header_text(app->text_input, "Sensor name");
|
||||
//По неясной мне причине в длину строки входит терминатор. Поэтому при длине 10 приходится указывать 11
|
||||
text_input_set_result_callback(
|
||||
app->text_input, addSensor_sensorNameChanged, app, app->currentSensorEdit->name, 11, true);
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, TEXTINPUT_VIEW);
|
||||
}
|
||||
|
||||
static void addSensor_enterCallback(void* context, uint32_t index) {
|
||||
PluginData* app = context;
|
||||
if(index == 0) {
|
||||
addSensor_sensorNameChange(app);
|
||||
}
|
||||
if(index == 3) {
|
||||
//Сохранение датчика
|
||||
DHTMon_sensors_save();
|
||||
DHTMon_sensors_reload();
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
void sensorEdit_sceneCreate(PluginData* app) {
|
||||
variable_item_list = variable_item_list_alloc();
|
||||
|
||||
variable_item_list_reset(variable_item_list);
|
||||
|
||||
variable_item_list_set_enter_callback(variable_item_list, addSensor_enterCallback, app);
|
||||
|
||||
app->view = variable_item_list_get_view(variable_item_list);
|
||||
|
||||
view_set_previous_callback(app->view, addSensor_exitCallback);
|
||||
|
||||
view_dispatcher_add_view(app->view_dispatcher, ADDSENSOR_MENU_VIEW, app->view);
|
||||
}
|
||||
void sensorEdit_scene(PluginData* app) {
|
||||
//Очистка списка
|
||||
variable_item_list_reset(variable_item_list);
|
||||
|
||||
//Имя редактируемого датчика
|
||||
nameItem = variable_item_list_add(variable_item_list, "Name: ", 1, NULL, NULL);
|
||||
variable_item_set_current_value_index(nameItem, 0);
|
||||
variable_item_set_current_value_text(nameItem, app->currentSensorEdit->name);
|
||||
|
||||
//Тип датчика
|
||||
app->item =
|
||||
variable_item_list_add(variable_item_list, "Type:", 2, addSensor_sensorTypeChanged, app);
|
||||
|
||||
variable_item_set_current_value_index(app->item, app->currentSensorEdit->type);
|
||||
variable_item_set_current_value_text(app->item, sensorsTypes[app->currentSensorEdit->type]);
|
||||
|
||||
//GPIO
|
||||
app->item =
|
||||
variable_item_list_add(variable_item_list, "GPIO:", 13, addSensor_GPIOChanged, app);
|
||||
variable_item_set_current_value_index(
|
||||
app->item, DHTMon_GPIO_to_index(app->currentSensorEdit->GPIO));
|
||||
variable_item_set_current_value_text(
|
||||
app->item, DHTMon_GPIO_getName(app->currentSensorEdit->GPIO));
|
||||
variable_item_list_add(variable_item_list, "Save", 1, NULL, app);
|
||||
|
||||
//Сброс выбранного пункта в ноль
|
||||
variable_item_list_set_selected_item(variable_item_list, 0);
|
||||
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, ADDSENSOR_MENU_VIEW);
|
||||
}
|
||||
void sensorEdit_sceneRemove(void) {
|
||||
variable_item_list_free(variable_item_list);
|
||||
}
|
||||
@@ -1,674 +0,0 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
@@ -1,21 +0,0 @@
|
||||
# Flipper Zero DnD Dice
|
||||
|
||||
<div style="text-align:center"><img src="sources/flipper-screen.png"/></div>
|
||||
<br />
|
||||
|
||||
**DnD Dice** is a dice rolling application for your **Flipper Zero**.
|
||||
|
||||
Dice types: Coin, d4, d6, d8, d10, d12, d20, d100
|
||||
|
||||
## Screenshots
|
||||
|
||||
<div style="text-align:center"><img src="sources/main-screen.png"/></div>
|
||||
<br />
|
||||
<div style="text-align:center"><img src="sources/roll-screen.png"/></div>
|
||||
|
||||
## Compiling
|
||||
|
||||
1. Clone the [flipperzero-firmware](https://github.com/flipperdevices/flipperzero-firmware) repository or another firmware that you use (for example [unleashed-firmware](https://github.com/DarkFlippers/unleashed-firmware)).
|
||||
2. Create a symbolic link in `applications_user` named **dice**, pointing to this repository.
|
||||
3. Compile by command `./fbt fap_dice_dnd_app`
|
||||
4. Copy `build/f7-firmware-D/.extapps/dice_dnd_app.fap` to **apps/Games** on the SD card or by [qFlipper](https://flipperzero.one/update) app.
|
||||
@@ -1,13 +0,0 @@
|
||||
App(
|
||||
appid="DND_Dice_app",
|
||||
name="DnD Dice [Ka3u6y6a]",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="dice_dnd_app",
|
||||
cdefines=["APP_DICE"],
|
||||
requires=["gui"],
|
||||
stack_size=1 * 1024,
|
||||
order=90,
|
||||
fap_icon="icon.png",
|
||||
fap_category="Games",
|
||||
fap_icon_assets="assets",
|
||||
)
|
||||
|
Before Width: | Height: | Size: 539 B |
|
Before Width: | Height: | Size: 535 B |
|
Before Width: | Height: | Size: 204 B |
|
Before Width: | Height: | Size: 546 B |
|
Before Width: | Height: | Size: 530 B |
|
Before Width: | Height: | Size: 546 B |
|
Before Width: | Height: | Size: 535 B |
|
Before Width: | Height: | Size: 628 B |
|
Before Width: | Height: | Size: 618 B |
|
Before Width: | Height: | Size: 630 B |
|
Before Width: | Height: | Size: 635 B |
|
Before Width: | Height: | Size: 544 B |
|
Before Width: | Height: | Size: 531 B |