annotate Discovery/Src/motion.c @ 383:49a02dea8ae3 MotionDetection

Combine variables in a structure to improve readability of code Add function to compensate offsets from calculated to real sector center position
author ideenmodellierer
date Thu, 17 Oct 2019 21:15:42 +0200
parents 695434a6dcf6
children 427ae9f8e28e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
359
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
1 /*
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
2 * motion.c
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
3 *
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
4 * Created on: 20.05.2019
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
5 * Author: Thorsten Sonntag
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
6 */
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
7
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
8 #include <stdint.h>
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
9 #include <string.h>
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
10 #include <stdlib.h>
359
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
11 #include <math.h>
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
12 #include "motion.h"
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
13 #include "data_central.h"
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
14 #include "t7.h"
371
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
15 #include "settings.h"
359
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
16
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
17 #define STABLE_STATE_COUNT 2 /* number of count to declare a state as stable (at the moment based on 100ms) */
363
bdf978d2a5d4 Reworked detection function
ideenmodellierer
parents: 362
diff changeset
18 #define STABLE_STATE_TIMEOUT 5 /* Detection shall be aborted if a movement state is stable for more than 500ms */
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
19
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
20 #define SECTOR_WINDOW 80.0 /* Pitch window which is used for custom view projection */
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
21 #define SECTOR_WINDOW_MAX 120.0 /* Pitch window which will be greater than the divers field of view */
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
22 #define SECTOR_HYSTERY 3 /* Additional offset to avoid fast changing displays */
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
23 #define SECTOR_BORDER 400.0 /* Define a value which is out of limit to avoid not wanted key events */
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
24 #define SECTOR_FILTER 10 /* Define speed for calculated angle to follow real value */
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
25
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
26 #define SECTOR_MAX 30 /* maximum number of sectors */
378
834e087505ec Incremented "idle window" for scroll detection
ideenmodellierer
parents: 373
diff changeset
27 #define SECTOR_SCROLL 7 /* number of sectors used for scroll detection */
359
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
28
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
29 static detectionState_t detectionState = DETECT_NOTHING;
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
30 static SSector sectorDetection;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
31
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
32
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
33 uint8_t GetSectorForPitch(float pitch)
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
34 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
35 static uint8_t lastsector = 0;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
36 float newPitch;
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
37 uint8_t sector = 0;
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
38
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
39 newPitch = pitch + sectorDetection.offset + sectorDetection.center; /* do not use negative values and consider offset to center position */
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
40 if (newPitch < 0.0) /* clip value */
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
41 {
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
42 newPitch = 0.0;
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
43 }
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
44 if (newPitch > sectorDetection.window) /* clip value */
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
45 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
46 newPitch = sectorDetection.window;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
47 }
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
48
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
49 /* switch to other sector? */
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
50 if((newPitch > sectorDetection.upperborder) || (newPitch <= sectorDetection.lowerborder))
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
51 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
52 sector = (uint16_t) newPitch / sectorDetection.size;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
53 sectorDetection.lowerborder = sector * sectorDetection.size - SECTOR_HYSTERY;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
54 sectorDetection.upperborder = (sector + 1) * sectorDetection.size + SECTOR_HYSTERY;
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
55 lastsector = sector;
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
56 }
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
57
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
58 return lastsector;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
59 }
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
60
371
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
61 void DefinePitchSectors(float centerPitch,uint8_t numOfSectors)
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
62 {
371
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
63 if(numOfSectors == CUSTOMER_DEFINED_VIEWS)
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
64 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
65 sectorDetection.count = t7_GetEnabled_customviews();
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
66 if(sectorDetection.count > 7)
371
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
67 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
68 sectorDetection.count = 7; /* more views are hard to manually control */
371
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
69 }
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
70 }
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
71 else
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
72 if(numOfSectors != CUSTOMER_KEEP_LAST_SECTORS)
371
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
73 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
74 sectorDetection.count = numOfSectors;
371
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
75 }
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
76
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
77 if(sectorDetection.count == SECTOR_MAX)
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
78 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
79 sectorDetection.window = SECTOR_WINDOW_MAX;
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
80 }
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
81 else
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
82 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
83 sectorDetection.window = SECTOR_WINDOW;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
84 }
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
85
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
86 sectorDetection.offset = (centerPitch - (sectorDetection.window / 2)) * -1.0;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
87 sectorDetection.size = sectorDetection.window / sectorDetection.count;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
88 sectorDetection.center = 0;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
89
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
90 /* reset border values */
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
91 sectorDetection.lowerborder = SECTOR_BORDER;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
92 sectorDetection.upperborder = SECTOR_BORDER * -1.0;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
93 /* get the current sector */
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
94 sectorDetection.current = GetSectorForPitch(stateRealGetPointer()->lifeData.compass_pitch);
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
95 sectorDetection.target = sectorDetection.current;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
96 /* do a small adjustment to center pitch to make sure the actual pitch is in the center of the current sector */
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
97 sectorDetection.center = (sectorDetection.upperborder) - ((sectorDetection.size + 2 *SECTOR_HYSTERY) / 2.0) - (centerPitch + sectorDetection.offset);
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
98
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
99 }
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
100
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
101 void InitMotionDetection(void)
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
102 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
103 sectorDetection.target = 0;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
104 sectorDetection.current = 0;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
105 sectorDetection.size = 0;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
106 sectorDetection.count = 0;
371
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
107
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
108 switch(settingsGetPointer()->MotionDetection)
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
109 {
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
110 case MOTION_DETECT_SECTOR: DefinePitchSectors(0,CUSTOMER_DEFINED_VIEWS);
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
111 break;
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
112 case MOTION_DETECT_MOVE: DefinePitchSectors(0,SECTOR_MAX);
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
113 break;
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
114 case MOTION_DETECT_SCROLL: DefinePitchSectors(0,SECTOR_SCROLL);
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
115 break;
371
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
116 default:
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
117 break;
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
118 }
fca370f847f8 Added parameter for number of sectors to be defined
ideenmodellierer
parents: 370
diff changeset
119
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
120 }
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
121
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
122 /* Map the current pitch value to a sector and create button event in case the sector is left */
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
123 detectionState_t detectSectorButtonEvent(float curPitch)
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
124 {
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
125 static uint8_t lastTargetSector = 0;
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
126 uint8_t newTargetSector;
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
127 uint8_t PitchEvent = DETECT_NOTHING;
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
128
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
129 /* only change sector if reading is stable */
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
130 newTargetSector = GetSectorForPitch(stateRealGetPointer()->lifeData.compass_pitch);
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
131 if(lastTargetSector == newTargetSector)
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
132 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
133 sectorDetection.target = newTargetSector;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
134 }
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
135 lastTargetSector = newTargetSector;
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
136 if(sectorDetection.target != sectorDetection.current)
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
137 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
138 if(sectorDetection.target > sectorDetection.current)
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
139 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
140 sectorDetection.current++;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
141 PitchEvent = DETECT_POS_PITCH;
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
142 }
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
143 else
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
144 {
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
145 sectorDetection.current--;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
146 PitchEvent = DETECT_NEG_PITCH;
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
147 }
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
148 }
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
149 return PitchEvent;
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
150 }
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
151
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
152 /* Check if pitch is not in center position and trigger a button action if needed */
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
153 detectionState_t detectScrollButtonEvent(float curPitch)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
154 {
378
834e087505ec Incremented "idle window" for scroll detection
ideenmodellierer
parents: 373
diff changeset
155 static uint8_t delayscroll = 0; /* slow down the number of scroll events */
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
156
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
157 uint8_t PitchEvent = DETECT_NOTHING;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
158 uint8_t newSector;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
159
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
160 if(delayscroll == 0)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
161 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
162 newSector = GetSectorForPitch(stateRealGetPointer()->lifeData.compass_pitch);
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
163 /* for scroll detection the motion windoe is split into 6 sectors => set event accoring to the sector number*/
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
164 switch(newSector)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
165 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
166 case 0:
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
167 case 1: PitchEvent = DETECT_POS_PITCH;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
168 break;
378
834e087505ec Incremented "idle window" for scroll detection
ideenmodellierer
parents: 373
diff changeset
169 case 5:
834e087505ec Incremented "idle window" for scroll detection
ideenmodellierer
parents: 373
diff changeset
170 case 6: PitchEvent = DETECT_NEG_PITCH;
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
171 break;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
172 default:
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
173 break;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
174 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
175 if(PitchEvent != DETECT_NOTHING)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
176 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
177 delayscroll = 5;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
178 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
179 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
180 else
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
181 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
182 delayscroll--;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
183 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
184 return PitchEvent;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
185 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
186
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
187
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
188 uint8_t sectorhist[20];
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
189 uint8_t sectorindex = 0;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
190 /* Detect if user is generating an pitch including return to starting position */
359
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
191 /* This is done by feeding the past movements value per value into a state machine */
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
192 detectionState_t detectPitch(float currentPitch)
359
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
193 {
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
194 static uint8_t lastSector = 0;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
195 static uint8_t startSector = 0;
359
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
196 static uint8_t stableCnt = 0;
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
197
383
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
198 uint8_t curSector;
49a02dea8ae3 Combine variables in a structure to improve readability of code
ideenmodellierer
parents: 381
diff changeset
199
370
77cdfbdaca8c Changed function names from shake to pitch and improved detection function: Shake might be confusing for people reading the code because pitch values are ased for calculation => changed name to pitch to be more transparent
ideenmodellierer
parents: 363
diff changeset
200 if((detectionState == DETECT_NEG_PITCH) || (detectionState == DETECT_POS_PITCH)) /* discard last detection */
359
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
201 {
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
202 detectionState = DETECT_NOTHING;
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
203 }
363
bdf978d2a5d4 Reworked detection function
ideenmodellierer
parents: 362
diff changeset
204
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
205 curSector = GetSectorForPitch(stateRealGetPointer()->lifeData.compass_pitch);
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
206
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
207 /* feed value into state machine */
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
208 switch (detectionState)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
209 {
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
210 case DETECT_NOTHING: if(curSector != lastSector) /* detect a stable condition before evaluating for the next move */
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
211 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
212 stableCnt=0;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
213 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
214
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
215 if(stableCnt > STABLE_STATE_COUNT)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
216 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
217 detectionState = DETECT_START;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
218 stableCnt = 0;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
219 startSector = lastSector;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
220 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
221 break;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
222 case DETECT_START: if(curSector != lastSector)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
223 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
224 if(abs(curSector - startSector) > 1)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
225 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
226 if(curSector > lastSector)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
227 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
228 detectionState = DETECT_POS_MOVE;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
229 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
230 else
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
231 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
232 detectionState = DETECT_NEG_MOVE;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
233 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
234 stableCnt = 0;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
235 startSector = lastSector;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
236 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
237 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
238 break;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
239 case DETECT_NEG_MOVE:
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
240 case DETECT_POS_MOVE: if(curSector == lastSector) /* Moved to a max? */
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
241 {
378
834e087505ec Incremented "idle window" for scroll detection
ideenmodellierer
parents: 373
diff changeset
242 if(abs(startSector - curSector) > 2)
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
243 {
378
834e087505ec Incremented "idle window" for scroll detection
ideenmodellierer
parents: 373
diff changeset
244 detectionState++;
834e087505ec Incremented "idle window" for scroll detection
ideenmodellierer
parents: 373
diff changeset
245 stableCnt = 0;
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
246 }
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
247 if(stableCnt > 2)
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
248 {
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
249 detectionState = DETECT_NOTHING;
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
250 stableCnt = 0;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
251 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
252 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
253 break;
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
254 case DETECT_MAXIMA:
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
255 case DETECT_MINIMA: if(curSector != lastSector) /* reset timeout detection */
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
256 {
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
257 detectionState++;
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
258 stableCnt = 0;
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
259 }
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
260 break;
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
261 case DETECT_RISEBACK:
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
262 case DETECT_FALLBACK:
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
263 if(curSector == lastSector) /* check if we are back at start position at end of movement */
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
264 {
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
265 if(abs(startSector - curSector) <= 1)
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
266 {
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
267 if(stableCnt > 2)
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
268 {
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
269 detectionState++;
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
270 stableCnt = 0;
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
271 }
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
272 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
273 }
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
274 break;
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
275 default:
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
276 detectionState = DETECT_NOTHING;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
277 break;
381
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
278 }
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
279 if(detectionState != DETECT_START)
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
280 {
695434a6dcf6 Simplified pitch detection state machine:
ideenmodellierer
parents: 378
diff changeset
281 stableCnt++;
373
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
282 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
283 lastSector = curSector;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
284 if(stableCnt > STABLE_STATE_TIMEOUT)
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
285 {
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
286 detectionState = DETECT_NOTHING;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
287 stableCnt = 0;
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
288 }
7b981f8bdd41 Add scroll event by pitch angle detection:
ideenmodellierer
parents: 371
diff changeset
289
359
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
290 return detectionState;
4258ea9b67fa Added new files for motion detection (shaking) detection
ideenmodellierer
parents:
diff changeset
291 }