Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / modules / Notify / notify.c
blob56bde610d5688636310ec67c4bf0acc912d25dee
1 /**
2 ******************************************************************************
4 * @file notify.c
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @brief Notify module, show events and status on external led.
8 * @see The GNU Public License (GPL) Version 3
10 *****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "openpilot.h"
28 #include <flightstatus.h>
29 #include <systemalarms.h>
30 #include <flightbatterystate.h>
31 #include <lednotification.h>
32 #include <optypes.h>
33 #include <pios_notify.h>
34 #include <FreeRTOS.h>
35 #include <task.h>
36 #include <eventdispatcher.h>
37 #include "inc/notify.h"
38 #include "inc/sequences.h"
39 #include <pios_mem.h>
41 #define SAMPLE_PERIOD_MS 250
42 // private types
43 typedef struct {
44 uint32_t lastAlarmTime;
45 uint8_t lastAlarm;
46 } AlarmStatus_t;
47 // function declarations
48 static void updatedCb(UAVObjEvent *ev);
49 static void onTimerCb(UAVObjEvent *ev);
50 static void checkAlarm(uint8_t alarm, uint8_t *last_alarm, uint32_t *last_alm_time,
51 uint8_t warn_sequence, uint8_t critical_sequence, uint8_t error_sequence,
52 uint32_t timeBetweenNotifications);
53 static AlarmStatus_t *alarmStatus;
54 int32_t NotifyInitialize(void)
56 if (PIOS_WS2811_DEVICE) {
57 alarmStatus = (AlarmStatus_t *)pios_malloc(sizeof(AlarmStatus_t) * alarmsMapSize);
58 for (uint8_t i = 0; i < alarmsMapSize; i++) {
59 alarmStatus[i].lastAlarm = SYSTEMALARMS_ALARM_OK;
60 alarmStatus[i].lastAlarmTime = 0;
63 FlightStatusConnectCallback(&updatedCb);
64 static UAVObjEvent ev;
65 memset(&ev, 0, sizeof(UAVObjEvent));
66 EventPeriodicCallbackCreate(&ev, onTimerCb, SAMPLE_PERIOD_MS / portTICK_RATE_MS);
68 updatedCb(0);
70 return 0;
72 MODULE_INITCALL(NotifyInitialize, 0);
75 void updatedCb(UAVObjEvent *ev)
77 if (!ev || ev->obj == FlightStatusHandle()) {
78 static uint8_t last_armed = 0xff;
79 static uint8_t last_flightmode = 0xff;
80 uint8_t armed;
81 uint8_t flightmode;
82 FlightStatusArmedGet(&armed);
83 FlightStatusFlightModeGet(&flightmode);
84 if (last_armed != armed || (armed && flightmode != last_flightmode)) {
85 if (armed) {
86 PIOS_NOTIFICATION_Default_Ext_Led_Play(flightModeMap[flightmode], NOTIFY_PRIORITY_BACKGROUND);
87 } else {
88 PIOS_NOTIFICATION_Default_Ext_Led_Play(&notifications[NOTIFY_SEQUENCE_DISARMED], NOTIFY_PRIORITY_BACKGROUND);
91 last_armed = armed;
92 last_flightmode = flightmode;
96 void onTimerCb(__attribute__((unused)) UAVObjEvent *ev)
98 static SystemAlarmsAlarmData alarms;
100 SystemAlarmsAlarmGet(&alarms);
101 for (uint8_t i = 0; i < alarmsMapSize; i++) {
102 uint8_t alarm = SystemAlarmsAlarmToArray(alarms)[alarmsMap[i].alarmIndex];
103 checkAlarm(alarm,
104 &alarmStatus[i].lastAlarm,
105 &alarmStatus[i].lastAlarmTime,
106 alarmsMap[i].warnNotification,
107 alarmsMap[i].criticalNotification,
108 alarmsMap[i].errorNotification,
109 alarmsMap[i].timeBetweenNotifications);
113 void checkAlarm(uint8_t alarm, uint8_t *last_alarm, uint32_t *last_alm_time, uint8_t warn_sequence, uint8_t critical_sequence, uint8_t error_sequence, uint32_t timeBetweenNotifications)
115 if (alarm > SYSTEMALARMS_ALARM_OK) {
116 uint32_t current_time = PIOS_DELAY_GetuS();
117 if (*last_alarm < alarm || (*last_alm_time + timeBetweenNotifications * 1000) < current_time) {
118 uint8_t sequence = (alarm == SYSTEMALARMS_ALARM_WARNING) ? warn_sequence :
119 ((alarm == SYSTEMALARMS_ALARM_CRITICAL) ? critical_sequence : error_sequence);
120 bool updated = true;
121 if (sequence != NOTIFY_SEQUENCE_NULL) {
122 updated = PIOS_NOTIFICATION_Default_Ext_Led_Play(
123 &notifications[sequence],
124 alarm == SYSTEMALARMS_ALARM_WARNING ? NOTIFY_PRIORITY_REGULAR : NOTIFY_PRIORITY_CRITICAL);
126 if (updated) {
127 *last_alarm = alarm;
128 *last_alm_time = current_time;
131 // workaround timer overflow
132 if (*last_alm_time > current_time) {
133 *last_alm_time = 0;
135 } else {
136 *last_alarm = SYSTEMALARMS_ALARM_OK;