Sensor Fusion Library 0.6.1
Orientation sensing for Espressif (ESP32, ESP8266) processors
Loading...
Searching...
No Matches
status.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2017 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
18#include "sensor_fusion.h" // Sensor fusion structures and functions
19#include "board.h"
20#include "driver_sensors.h" // hardware-specific drivers
21#include "status.h" // Header for this .c file
22
23// bit-field definitions
24#define N 0x00 // No color
25#define R 0x04 // Red LED
26#define G 0x02 // Green LED
27#define B 0x01 // Blue LED
28
29// set RGB LED as a function of bit-field values
30void ssSetLeds(int8_t RGB)
31{
32 if (RGB & R) {
33 LED_RED_ON();
34 }else{
35 LED_RED_OFF();
36 }
37 if (RGB & G) {
38 LED_GREEN_ON();
39 }else {
40 LED_GREEN_OFF();
41 }
42 if (RGB & B) {
43 LED_BLUE_ON();
44 }else {
45 LED_BLUE_OFF();
46 }
47}
48
49// Do an immediate status update
50void ssSetStatusNow(StatusSubsystem *pStatus, fusion_status_t status)
51{
52 pStatus->status = status;
53 pStatus->next = status;
54
55 uint8_t blink = false;
56 uint8_t RGB = N;
57
58 // This is where we actually change the visual indicator
59 switch (status)
60 {
61 case INITIALIZING: // solid GREEN
62 RGB = G;
63 break;
64 case NORMAL: // blinking GREEN
65 RGB = G;
66 blink = true;
67 break;
68 case LOWPOWER: // blinking YELLOW
69 RGB = R|G;
70 blink = true;
71 break;
72 case SOFT_FAULT: // solid RED (usually momentary)
73 case HARD_FAULT: // solid RED
74 RGB = R;
75 break;
76 default: // default = off;
77 RGB = N;
78 }
79
80 if ((!blink) | (status != pStatus->previous))
81 {
82 ssSetLeds(RGB);
83 pStatus->toggle = true;
84 }
85 else
86 {
87 if (pStatus->toggle)
88 {
89 ssSetLeds(N);
90 }
91 else
92 {
93 ssSetLeds(RGB);
94 }
95
96 pStatus->toggle = !pStatus->toggle;
97 }
98 while (status == HARD_FAULT) ; // Never return on hard fault
99 // while (status == SOFT_FAULT) ; // DEBUG ONLY Never return on soft fault
100}
101
102// Unit test for status sub-system
103void ssTest(StatusSubsystem *pStatus)
104{
105 switch (pStatus->status)
106 {
107 case OFF:
108 ssSetStatusNow(pStatus, INITIALIZING);
109 break;
110 case INITIALIZING:
111 ssSetStatusNow(pStatus, LOWPOWER);
112 break;
113 case LOWPOWER:
114 ssSetStatusNow(pStatus, NORMAL);
115 break;
116 case NORMAL:
117 ssSetStatusNow(pStatus, RECEIVING_WIRED);
118 break;
119 case RECEIVING_WIRED:
120 ssSetStatusNow(pStatus, RECEIVING_WIRELESS);
121 break;
123 ssSetStatusNow(pStatus, SOFT_FAULT);
124 break;
125 case SOFT_FAULT:
126 ssSetStatusNow(pStatus, HARD_FAULT);
127 break;
128 case HARD_FAULT:
129 ssSetStatusNow(pStatus, OFF);
130 break;
131 }
132}
133
134// undefine these just in case some other library needs them
135#undef N
136#undef R
137#undef G
138#undef B
139
140
141// queue up a status change (which will take place at the next updateStatus)
142void ssQueueStatus(StatusSubsystem *pStatus, fusion_status_t status)
143{
144 pStatus->next = status;
145}
146
147// promote any previously queued status update
148void ssUpdateStatus(StatusSubsystem *pStatus)
149{
150 pStatus->previous = pStatus->status;
151 ssSetStatusNow(pStatus, pStatus->next);
152}
153
154// make an immediate update to the system status
155void ssSetStatus(StatusSubsystem *pStatus, fusion_status_t status)
156{
157 pStatus->next = status;
158 ssUpdateStatus(pStatus);
159}
160
161// return the system status
162fusion_status_t ssGetStatus(StatusSubsystem *pStatus)
163{
164 return pStatus->status;
165}
166
170{
171 pStatus->set = ssSetStatus;
172 pStatus->get = ssGetStatus;
173 pStatus->queue = ssQueueStatus;
174 pStatus->update = ssUpdateStatus;
175 pStatus->test = ssTest;
176 pStatus->previous = OFF;
177 pStatus->set(pStatus, OFF);
178 pStatus->queue(pStatus, OFF);
179 pStatus->toggle = false;
180
181 /* set initial values */
182 LED_RED_INIT(LOGIC_LED_OFF);
183 LED_GREEN_INIT(LOGIC_LED_OFF);
184 LED_BLUE_INIT(LOGIC_LED_OFF);
185}
Board configuration file.
Provides function prototypes for driver level interfaces It does not have a corresponding ....
The sensor_fusion.h file implements the top level programming interface.
fusion_status_t
Application-specific serial communications system.
@ RECEIVING_WIRED
Receiving commands over wired interface (momentary)
@ HARD_FAULT
Non-recoverable FAULT = something went very wrong.
@ LOWPOWER
Running in reduced power mode.
@ NORMAL
Operation is Nominal.
@ RECEIVING_WIRELESS
Receiving commands over wireless interface (momentary)
@ INITIALIZING
Initializing sensors and algorithms.
@ OFF
These are the state definitions for the status subsystem.
@ SOFT_FAULT
Recoverable FAULT = something went wrong, but we can keep going.
void initializeStatusSubsystem(StatusSubsystem *pStatus)
Definition status.c:169
Application-specific status subsystem.
StatusSubsystem() provides an object-like interface for communicating status to the user.
Definition status.h:26
fusion_status_t previous
Previous status state - fusion_status_t is defined in sensor_fusion.h.
Definition status.h:28
ssUpdateStatus_t * update
make pending status active/visible
Definition status.h:35
uint8_t toggle
This implementation can change LED color and have either solid/toggle.
Definition status.h:38
ssUpdateStatus_t * test
unit test which simply increments to next state
Definition status.h:36
ssSetStatus_t * set
change status immediately - no delay
Definition status.h:32
ssSetStatus_t * queue
queue status change for next regular interval
Definition status.h:34
ssGetStatus_t * get
return status
Definition status.h:33
fusion_status_t status
Current status.
Definition status.h:29
fusion_status_t next
Pending status change.
Definition status.h:30