xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
uartMenu.c
Go to the documentation of this file.
1 /*
2  * uartMenu.c
3  *
4  * Copyright (c) 2013, Thomas Buck <xythobuz@me.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * - Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include <avr/io.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <avr/pgmspace.h>
35 
36 #include <xycontrol.h>
37 #include <xmem.h>
38 #include <tasks.h>
39 #include <serial.h>
40 #include <uartMenu.h>
41 
52 void (*unHandler)(char) = NULL;
58 MenuEntry *findEntry(uint8_t cmd) {
59  MenuEntry *p = uartMenu;
60  while (p != NULL) {
61  if (p->cmd == cmd) {
62  return p;
63  }
64  p = p->next;
65  }
66  return NULL;
67 }
68 
69 uint8_t addMenuCommand(uint8_t cmd, PGM_P help, Task f) {
70  uint8_t lastBank = xmemGetBank();
72  if (findEntry(cmd) != NULL) {
73  return 1;
74  } else {
75  MenuEntry *p = (MenuEntry *)malloc(sizeof(MenuEntry));
76  if (p == NULL) {
77  return 1;
78  }
79  p->cmd = cmd;
80  p->helpText = help;
81  p->f = f;
82  p->next = uartMenu;
83  uartMenu = p;
84  return 0;
85  }
86  xmemSetBank(lastBank);
87 }
88 
94  MenuEntry *new = NULL;
95  while (root != NULL) {
96  MenuEntry *next = root->next;
97  root->next = new;
98  new = root;
99  root = next;
100  }
101  return new;
102 }
103 
104 void uartMenuPrintHelp(void) {
105  static uint8_t reversed = 0;
106  uint8_t lastBank = xmemGetBank();
108  char *buffer = (char *)malloc(35);
109  if (buffer == NULL) {
110  printf("!");
111  return;
112  }
113  if (!reversed) {
114  reversed = 1;
115  uartMenu = reverseList(uartMenu);
116  }
117  MenuEntry *p = uartMenu;
118  while (p != NULL) {
119  strcpy_P(buffer, p->helpText);
120  printf("%c: %s\n", p->cmd, buffer);
121  p = p->next;
122  }
123  free(buffer);
124  xmemSetBank(lastBank);
125 }
126 
127 void uartMenuRegisterHandler(void (*handler)(char)) {
128  unHandler = handler;
129 }
130 
131 void uartMenuTask(void) {
132  for (uint8_t i = 0; i < serialAvailable(); i++) {
133  if (serialHasChar(i)) {
134  uint8_t lastBank = xmemGetBank();
136  uint8_t c = serialGet(i);
137  MenuEntry *p = uartMenu;
138  while (p != NULL) {
139  if (p->cmd == c) {
140  p->f();
141  xmemSetBank(lastBank);
142  return;
143  }
144  p = p->next;
145  }
146  if (unHandler != NULL)
147  unHandler(c);
148  xmemSetBank(lastBank);
149  }
150  }
151 }