うおーるぼっとをWiiリモコンでコントロールする新しいプログラムです。 以前のものより、Wiiリモコンが早く繋がる様になりました。 It is a program which controls A with the Wii remote. ※ A Bluetooth dongle and a Wii remote control are needed.

Dependencies:   USBHost mbed FATFileSystem mbed-rtos

Committer:
jksoft
Date:
Mon Jun 10 16:01:50 2013 +0000
Revision:
0:fccb789424fc
1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:fccb789424fc 1 /*
jksoft 0:fccb789424fc 2 * Copyright (C) 2009-2012 by Matthias Ringwald
jksoft 0:fccb789424fc 3 *
jksoft 0:fccb789424fc 4 * Redistribution and use in source and binary forms, with or without
jksoft 0:fccb789424fc 5 * modification, are permitted provided that the following conditions
jksoft 0:fccb789424fc 6 * are met:
jksoft 0:fccb789424fc 7 *
jksoft 0:fccb789424fc 8 * 1. Redistributions of source code must retain the above copyright
jksoft 0:fccb789424fc 9 * notice, this list of conditions and the following disclaimer.
jksoft 0:fccb789424fc 10 * 2. Redistributions in binary form must reproduce the above copyright
jksoft 0:fccb789424fc 11 * notice, this list of conditions and the following disclaimer in the
jksoft 0:fccb789424fc 12 * documentation and/or other materials provided with the distribution.
jksoft 0:fccb789424fc 13 * 3. Neither the name of the copyright holders nor the names of
jksoft 0:fccb789424fc 14 * contributors may be used to endorse or promote products derived
jksoft 0:fccb789424fc 15 * from this software without specific prior written permission.
jksoft 0:fccb789424fc 16 * 4. Any redistribution, use, or modification is done solely for
jksoft 0:fccb789424fc 17 * personal benefit and not for any commercial purpose or for
jksoft 0:fccb789424fc 18 * monetary gain.
jksoft 0:fccb789424fc 19 *
jksoft 0:fccb789424fc 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
jksoft 0:fccb789424fc 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
jksoft 0:fccb789424fc 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
jksoft 0:fccb789424fc 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
jksoft 0:fccb789424fc 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
jksoft 0:fccb789424fc 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
jksoft 0:fccb789424fc 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
jksoft 0:fccb789424fc 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
jksoft 0:fccb789424fc 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
jksoft 0:fccb789424fc 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
jksoft 0:fccb789424fc 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
jksoft 0:fccb789424fc 31 * SUCH DAMAGE.
jksoft 0:fccb789424fc 32 *
jksoft 0:fccb789424fc 33 * Please inquire about commercial licensing options at btstack@ringwald.ch
jksoft 0:fccb789424fc 34 *
jksoft 0:fccb789424fc 35 */
jksoft 0:fccb789424fc 36
jksoft 0:fccb789424fc 37 /*
jksoft 0:fccb789424fc 38 * run_loop_embedded.c
jksoft 0:fccb789424fc 39 *
jksoft 0:fccb789424fc 40 * For this run loop, we assume that there's no global way to wait for a list
jksoft 0:fccb789424fc 41 * of data sources to get ready. Instead, each data source has to queried
jksoft 0:fccb789424fc 42 * individually. Calling ds->isReady() before calling ds->process() doesn't
jksoft 0:fccb789424fc 43 * make sense, so we just poll each data source round robin.
jksoft 0:fccb789424fc 44 *
jksoft 0:fccb789424fc 45 * To support an idle state, where an MCU could go to sleep, the process function
jksoft 0:fccb789424fc 46 * has to return if it has to called again as soon as possible
jksoft 0:fccb789424fc 47 *
jksoft 0:fccb789424fc 48 * After calling process() on every data source and evaluating the pending timers,
jksoft 0:fccb789424fc 49 * the idle hook gets called if no data source did indicate that it needs to be
jksoft 0:fccb789424fc 50 * called right away.
jksoft 0:fccb789424fc 51 *
jksoft 0:fccb789424fc 52 */
jksoft 0:fccb789424fc 53
jksoft 0:fccb789424fc 54
jksoft 0:fccb789424fc 55 #include <btstack/run_loop.h>
jksoft 0:fccb789424fc 56 #include <btstack/linked_list.h>
jksoft 0:fccb789424fc 57 #include <btstack/hal_tick.h>
jksoft 0:fccb789424fc 58 #include <btstack/hal_cpu.h>
jksoft 0:fccb789424fc 59
jksoft 0:fccb789424fc 60 #include "run_loop_private.h"
jksoft 0:fccb789424fc 61 #include "debug.h"
jksoft 0:fccb789424fc 62
jksoft 0:fccb789424fc 63 #include <stddef.h> // NULL
jksoft 0:fccb789424fc 64
jksoft 0:fccb789424fc 65 // the run loop
jksoft 0:fccb789424fc 66 static linked_list_t data_sources;
jksoft 0:fccb789424fc 67
jksoft 0:fccb789424fc 68 static linked_list_t timers;
jksoft 0:fccb789424fc 69
jksoft 0:fccb789424fc 70 #ifdef HAVE_TICK
jksoft 0:fccb789424fc 71 static uint32_t system_ticks;
jksoft 0:fccb789424fc 72 #endif
jksoft 0:fccb789424fc 73
jksoft 0:fccb789424fc 74 static int trigger_event_received = 0;
jksoft 0:fccb789424fc 75
jksoft 0:fccb789424fc 76 /**
jksoft 0:fccb789424fc 77 * trigger run loop iteration
jksoft 0:fccb789424fc 78 */
jksoft 0:fccb789424fc 79 void embedded_trigger(void){
jksoft 0:fccb789424fc 80 trigger_event_received = 1;
jksoft 0:fccb789424fc 81 }
jksoft 0:fccb789424fc 82
jksoft 0:fccb789424fc 83 /**
jksoft 0:fccb789424fc 84 * Add data_source to run_loop
jksoft 0:fccb789424fc 85 */
jksoft 0:fccb789424fc 86 void embedded_add_data_source(data_source_t *ds){
jksoft 0:fccb789424fc 87 linked_list_add(&data_sources, (linked_item_t *) ds);
jksoft 0:fccb789424fc 88 }
jksoft 0:fccb789424fc 89
jksoft 0:fccb789424fc 90 /**
jksoft 0:fccb789424fc 91 * Remove data_source from run loop
jksoft 0:fccb789424fc 92 */
jksoft 0:fccb789424fc 93 int embedded_remove_data_source(data_source_t *ds){
jksoft 0:fccb789424fc 94 return linked_list_remove(&data_sources, (linked_item_t *) ds);
jksoft 0:fccb789424fc 95 }
jksoft 0:fccb789424fc 96
jksoft 0:fccb789424fc 97 /**
jksoft 0:fccb789424fc 98 * Add timer to run_loop (keep list sorted)
jksoft 0:fccb789424fc 99 */
jksoft 0:fccb789424fc 100 void embedded_add_timer(timer_source_t *ts){
jksoft 0:fccb789424fc 101 #ifdef HAVE_TICK
jksoft 0:fccb789424fc 102 linked_item_t *it;
jksoft 0:fccb789424fc 103 for (it = (linked_item_t *) &timers; it->next ; it = it->next){
jksoft 0:fccb789424fc 104 if (ts->timeout < ((timer_source_t *) it->next)->timeout) {
jksoft 0:fccb789424fc 105 break;
jksoft 0:fccb789424fc 106 }
jksoft 0:fccb789424fc 107 }
jksoft 0:fccb789424fc 108 ts->item.next = it->next;
jksoft 0:fccb789424fc 109 it->next = (linked_item_t *) ts;
jksoft 0:fccb789424fc 110 // log_info("Added timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
jksoft 0:fccb789424fc 111 // embedded_dump_timer();
jksoft 0:fccb789424fc 112 #endif
jksoft 0:fccb789424fc 113 }
jksoft 0:fccb789424fc 114
jksoft 0:fccb789424fc 115 /**
jksoft 0:fccb789424fc 116 * Remove timer from run loop
jksoft 0:fccb789424fc 117 */
jksoft 0:fccb789424fc 118 int embedded_remove_timer(timer_source_t *ts){
jksoft 0:fccb789424fc 119 #ifdef HAVE_TICK
jksoft 0:fccb789424fc 120 // log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
jksoft 0:fccb789424fc 121 return linked_list_remove(&timers, (linked_item_t *) ts);
jksoft 0:fccb789424fc 122 #else
jksoft 0:fccb789424fc 123 return 0;
jksoft 0:fccb789424fc 124 #endif
jksoft 0:fccb789424fc 125 }
jksoft 0:fccb789424fc 126
jksoft 0:fccb789424fc 127 void embedded_dump_timer(void){
jksoft 0:fccb789424fc 128 #ifdef HAVE_TICK
jksoft 0:fccb789424fc 129 #ifdef ENABLE_LOG_INFO
jksoft 0:fccb789424fc 130 linked_item_t *it;
jksoft 0:fccb789424fc 131 int i = 0;
jksoft 0:fccb789424fc 132 for (it = (linked_item_t *) timers; it ; it = it->next){
jksoft 0:fccb789424fc 133 timer_source_t *ts = (timer_source_t*) it;
jksoft 0:fccb789424fc 134 log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout);
jksoft 0:fccb789424fc 135 }
jksoft 0:fccb789424fc 136 #endif
jksoft 0:fccb789424fc 137 #endif
jksoft 0:fccb789424fc 138 }
jksoft 0:fccb789424fc 139
jksoft 0:fccb789424fc 140 /**
jksoft 0:fccb789424fc 141 * Execute run_loop
jksoft 0:fccb789424fc 142 */
jksoft 0:fccb789424fc 143 void embedded_execute(void) {
jksoft 0:fccb789424fc 144 data_source_t *ds;
jksoft 0:fccb789424fc 145
jksoft 0:fccb789424fc 146 while (1) {
jksoft 0:fccb789424fc 147
jksoft 0:fccb789424fc 148 // process data sources
jksoft 0:fccb789424fc 149 data_source_t *next;
jksoft 0:fccb789424fc 150 for (ds = (data_source_t *) data_sources; ds != NULL ; ds = next){
jksoft 0:fccb789424fc 151 next = (data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
jksoft 0:fccb789424fc 152 ds->process(ds);
jksoft 0:fccb789424fc 153 }
jksoft 0:fccb789424fc 154
jksoft 0:fccb789424fc 155 #ifdef HAVE_TICK
jksoft 0:fccb789424fc 156 // process timers
jksoft 0:fccb789424fc 157 while (timers) {
jksoft 0:fccb789424fc 158 timer_source_t *ts = (timer_source_t *) timers;
jksoft 0:fccb789424fc 159 if (ts->timeout > system_ticks) break;
jksoft 0:fccb789424fc 160 run_loop_remove_timer(ts);
jksoft 0:fccb789424fc 161 ts->process(ts);
jksoft 0:fccb789424fc 162 }
jksoft 0:fccb789424fc 163 #endif
jksoft 0:fccb789424fc 164
jksoft 0:fccb789424fc 165 // disable IRQs and check if run loop iteration has been requested. if not, go to sleep
jksoft 0:fccb789424fc 166 hal_cpu_disable_irqs();
jksoft 0:fccb789424fc 167 if (trigger_event_received){
jksoft 0:fccb789424fc 168 hal_cpu_enable_irqs_and_sleep();
jksoft 0:fccb789424fc 169 continue;
jksoft 0:fccb789424fc 170 }
jksoft 0:fccb789424fc 171 hal_cpu_enable_irqs();
jksoft 0:fccb789424fc 172 }
jksoft 0:fccb789424fc 173 }
jksoft 0:fccb789424fc 174
jksoft 0:fccb789424fc 175 #ifdef HAVE_TICK
jksoft 0:fccb789424fc 176 static void embedded_tick_handler(void){
jksoft 0:fccb789424fc 177 system_ticks++;
jksoft 0:fccb789424fc 178 trigger_event_received = 1;
jksoft 0:fccb789424fc 179 }
jksoft 0:fccb789424fc 180
jksoft 0:fccb789424fc 181 uint32_t embedded_get_ticks(void){
jksoft 0:fccb789424fc 182 return system_ticks;
jksoft 0:fccb789424fc 183 }
jksoft 0:fccb789424fc 184
jksoft 0:fccb789424fc 185 uint32_t embedded_ticks_for_ms(uint32_t time_in_ms){
jksoft 0:fccb789424fc 186 return time_in_ms / hal_tick_get_tick_period_in_ms();
jksoft 0:fccb789424fc 187 }
jksoft 0:fccb789424fc 188
jksoft 0:fccb789424fc 189 // set timer
jksoft 0:fccb789424fc 190 void run_loop_set_timer(timer_source_t *ts, uint32_t timeout_in_ms){
jksoft 0:fccb789424fc 191 uint32_t ticks = embedded_ticks_for_ms(timeout_in_ms);
jksoft 0:fccb789424fc 192 if (ticks == 0) ticks++;
jksoft 0:fccb789424fc 193 ts->timeout = system_ticks + ticks;
jksoft 0:fccb789424fc 194 }
jksoft 0:fccb789424fc 195 #endif
jksoft 0:fccb789424fc 196
jksoft 0:fccb789424fc 197 void embedded_init(void){
jksoft 0:fccb789424fc 198
jksoft 0:fccb789424fc 199 data_sources = NULL;
jksoft 0:fccb789424fc 200
jksoft 0:fccb789424fc 201 #ifdef HAVE_TICK
jksoft 0:fccb789424fc 202 timers = NULL;
jksoft 0:fccb789424fc 203 system_ticks = 0;
jksoft 0:fccb789424fc 204 hal_tick_init();
jksoft 0:fccb789424fc 205 hal_tick_set_handler(&embedded_tick_handler);
jksoft 0:fccb789424fc 206 #endif
jksoft 0:fccb789424fc 207 }
jksoft 0:fccb789424fc 208
jksoft 0:fccb789424fc 209 extern const run_loop_t run_loop_embedded;
jksoft 0:fccb789424fc 210 const run_loop_t run_loop_embedded = {
jksoft 0:fccb789424fc 211 &embedded_init,
jksoft 0:fccb789424fc 212 &embedded_add_data_source,
jksoft 0:fccb789424fc 213 &embedded_remove_data_source,
jksoft 0:fccb789424fc 214 &embedded_add_timer,
jksoft 0:fccb789424fc 215 &embedded_remove_timer,
jksoft 0:fccb789424fc 216 &embedded_execute,
jksoft 0:fccb789424fc 217 &embedded_dump_timer
jksoft 0:fccb789424fc 218 };