dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_annunc.c
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 /* Project Includes */
31 #include "pios.h"
32 
33 #if defined(PIOS_INCLUDE_ANNUNC)
34 
35 #include <pios_annunc_priv.h>
36 
37 const static struct pios_annunc_cfg * annunciator_cfg;
38 
39 #define MAXANNUNC 8
40 
41 static uint8_t annunc_state;
42 
43 static bool get_annunc_state(int idx) {
44  if (annunc_state & (1 << idx)) {
45  return true;
46  } else {
47  return false;
48  }
49 }
50 
51 static void set_annunc_state(int idx, bool active) {
52  uint8_t mask = 1 << idx;
53  uint8_t val = 0;
54 
55  if (active) {
56  val = mask;
57  }
58 
59  annunc_state = (annunc_state & (~mask)) | val;
60 }
61 
65 int32_t PIOS_ANNUNC_Init(const struct pios_annunc_cfg * cfg)
66 {
67  PIOS_Assert(cfg);
68 
69  /* Store away the config in a global used by API functions */
70  annunciator_cfg = cfg;
71 
72  PIOS_Assert(cfg->num_annunciators <= MAXANNUNC);
73 
74  for (uint8_t i = 0; i < cfg->num_annunciators; i++) {
75  const struct pios_annunc * annunciator = &(cfg->annunciators[i]);
76 
77  if (annunciator->remap) {
78 #ifdef STM32F10X_MD
79  GPIO_PinRemapConfig(annunciator->remap, ENABLE);
80 #else
81  GPIO_PinAFConfig(annunciator->pin.gpio, annunciator->pin.init.GPIO_Pin, annunciator->remap);
82 #endif
83 
84  }
85 
86  GPIO_Init(annunciator->pin.gpio, (GPIO_InitTypeDef*)&annunciator->pin.init);
87 
89  }
90 
91  return 0;
92 }
93 
98 void PIOS_ANNUNC_On(uint32_t annunc_id)
99 {
100  PIOS_Assert(annunciator_cfg);
101 
102  if (annunc_id >= annunciator_cfg->num_annunciators) {
103  /* Annunciator index out of range */
104  return;
105  }
106 
107  const struct pios_annunc * annunciator =
108  &(annunciator_cfg->annunciators[annunc_id]);
109 
110  set_annunc_state(annunc_id, true);
111 
112  if (annunciator->handler) {
113  annunciator->handler(true);
114  return;
115  }
116 
117  if (!annunciator->pin.gpio) {
118  return;
119  }
120 
121  if (annunciator->active_high) {
122  GPIO_SetBits(annunciator->pin.gpio,
123  annunciator->pin.init.GPIO_Pin);
124  } else {
125  GPIO_ResetBits(annunciator->pin.gpio,
126  annunciator->pin.init.GPIO_Pin);
127  }
128 }
129 
134 void PIOS_ANNUNC_Off(uint32_t annunc_id)
135 {
136  PIOS_Assert(annunciator_cfg);
137 
138  if (annunc_id >= annunciator_cfg->num_annunciators) {
139  /* Annunciator index out of range */
140  return;
141  }
142 
143  const struct pios_annunc * annunciator =
144  &(annunciator_cfg->annunciators[annunc_id]);
145 
146  set_annunc_state(annunc_id, false);
147 
148  if (annunciator->handler) {
149  annunciator->handler(false);
150  return;
151  }
152 
153  if (!annunciator->pin.gpio) {
154  return;
155  }
156 
157  if (annunciator->active_high) {
158  GPIO_ResetBits(annunciator->pin.gpio,
159  annunciator->pin.init.GPIO_Pin);
160  } else {
161  GPIO_SetBits(annunciator->pin.gpio,
162  annunciator->pin.init.GPIO_Pin);
163  }
164 }
165 
171 void PIOS_ANNUNC_Toggle(uint32_t annunc_id)
172 {
173  PIOS_Assert(annunciator_cfg);
174 
175  if (annunc_id >= annunciator_cfg->num_annunciators) {
176  /* Annunciator index out of range */
177  return;
178  }
179 
180  if (get_annunc_state(annunc_id)) {
181  PIOS_ANNUNC_Off(annunc_id);
182  } else {
183  PIOS_ANNUNC_On(annunc_id);
184  }
185 }
186 
187 #endif
188 
struct stm32_gpio pin
Main PiOS header to include all the compiled in PiOS options.
uint8_t num_annunciators
GPIO_InitTypeDef init
Definition: pios_stm32.h:61
GPIO_TypeDef * gpio
Definition: pios_stm32.h:60
void PIOS_ANNUNC_Off(uint32_t annunc_id)
static struct flyingpicmd_cfg_fa cfg
Definition: main.c:49
const struct pios_annunc * annunciators
int32_t PIOS_ANNUNC_Init(const struct pios_annunc_cfg *cfg)
void PIOS_ANNUNC_On(uint32_t annunc_id)
uint8_t i
Definition: msp_messages.h:97
pios_annunc_func_t handler
void PIOS_ANNUNC_Toggle(uint32_t annunc_id)
uint32_t remap
#define PIOS_Assert(test)
Definition: pios_debug.h:52
LED private definitions.