xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
Files | Data Structures | Functions | Variables
UART Menu

Enables user interaction with an UART Menu. More...

Files

file  uartMenu.h
 UART Menu API Header.
 
file  uartMenu.c
 UART Menu API Implementation.
 

Data Structures

struct  MenuEntry
 Data Structure for Single-Linked-List for UART Menu. More...
 

Functions

uint8_t addMenuCommand (uint8_t cmd, PGM_P help, Task f)
 Add a command to the UART Menu. More...
 
void uartMenuPrintHelp (void)
 Print all registered commands. More...
 
void uartMenuRegisterHandler (void(*handler)(char))
 Register a Handler for unhandled menu commands. More...
 
void uartMenuTask (void)
 Task to work the UART Menu. More...
 
MenuEntryfindEntry (uint8_t cmd)
 Search the uartMenu Linked List. More...
 
MenuEntryreverseList (MenuEntry *root)
 Reverse the UART Menu List. More...
 

Variables

MenuEntryuartMenu = NULL
 Single-Linked-List for commands. More...
 
void(* unHandler )(char) = NULL
 Handler for unhandled commands. More...
 

Detailed Description

Enables user interaction with an UART Menu.

Function Documentation

uint8_t addMenuCommand ( uint8_t  cmd,
PGM_P  help,
Task  f 
)

Add a command to the UART Menu.

Parameters
cmdByte that triggers command
helpHelp Text String in Flash
fTask to be executed
Returns
0 on success, 1 if already registered or not enough memory.
Examples:
hardwareTest.c, and uartFlight.c.

Definition at line 69 of file uartMenu.c.

References BANK_GENERIC, MenuEntry::cmd, MenuEntry::f, findEntry(), MenuEntry::helpText, MenuEntry::next, uartMenu, xmemGetBank(), and xmemSetBank().

Referenced by xyInit().

69  {
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 }
MenuEntry* findEntry ( uint8_t  cmd)

Search the uartMenu Linked List.

Parameters
cmdCommand to search for
Returns
MenuEntry for command cmd, or NULL

Definition at line 58 of file uartMenu.c.

References MenuEntry::cmd, MenuEntry::next, and uartMenu.

Referenced by addMenuCommand().

58  {
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 }
MenuEntry* reverseList ( MenuEntry root)

Reverse the UART Menu List.

Parameters
rootRoot of the Single-Linked-List.
Returns
New root of reversed list.

Definition at line 93 of file uartMenu.c.

References MenuEntry::next.

Referenced by uartMenuPrintHelp().

93  {
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 }
void uartMenuPrintHelp ( void  )

Print all registered commands.

Definition at line 104 of file uartMenu.c.

References BANK_GENERIC, MenuEntry::cmd, MenuEntry::helpText, MenuEntry::next, reverseList(), uartMenu, xmemGetBank(), and xmemSetBank().

Referenced by xyInit().

104  {
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;
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 }
void uartMenuRegisterHandler ( void(*)(char)  handler)

Register a Handler for unhandled menu commands.

Parameters
handlerWill be called if an unknown command is received.

Definition at line 127 of file uartMenu.c.

References unHandler.

127  {
128  unHandler = handler;
129 }
void uartMenuTask ( void  )

Task to work the UART Menu.

Definition at line 131 of file uartMenu.c.

References BANK_GENERIC, MenuEntry::cmd, MenuEntry::f, MenuEntry::next, serialAvailable(), serialGet(), serialHasChar(), uartMenu, unHandler, xmemGetBank(), and xmemSetBank().

Referenced by xyInit().

131  {
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 }

Variable Documentation

MenuEntry* uartMenu = NULL

Single-Linked-List for commands.

Definition at line 51 of file uartMenu.c.

Referenced by addMenuCommand(), findEntry(), uartMenuPrintHelp(), and uartMenuTask().

void(* unHandler)(char) = NULL

Handler for unhandled commands.

Definition at line 52 of file uartMenu.c.

Referenced by uartMenuRegisterHandler(), and uartMenuTask().