Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Fri May 26 17:21:04 2017 +0000
Revision:
34:69342782fb68
Parent:
18:6a4db94011d3
Added small reverse turns before the break so that we can stop faster.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /* mbed Microcontroller Library
sahilmgandhi 18:6a4db94011d3 2 * Copyright (c) 2015 ARM Limited
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
sahilmgandhi 18:6a4db94011d3 5 * you may not use this file except in compliance with the License.
sahilmgandhi 18:6a4db94011d3 6 * You may obtain a copy of the License at
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * http://www.apache.org/licenses/LICENSE-2.0
sahilmgandhi 18:6a4db94011d3 9 *
sahilmgandhi 18:6a4db94011d3 10 * Unless required by applicable law or agreed to in writing, software
sahilmgandhi 18:6a4db94011d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
sahilmgandhi 18:6a4db94011d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sahilmgandhi 18:6a4db94011d3 13 * See the License for the specific language governing permissions and
sahilmgandhi 18:6a4db94011d3 14 * limitations under the License.
sahilmgandhi 18:6a4db94011d3 15 */
sahilmgandhi 18:6a4db94011d3 16 #include <stddef.h>
sahilmgandhi 18:6a4db94011d3 17 #include "hal/ticker_api.h"
sahilmgandhi 18:6a4db94011d3 18 #include "platform/mbed_critical.h"
sahilmgandhi 18:6a4db94011d3 19
sahilmgandhi 18:6a4db94011d3 20 void ticker_set_handler(const ticker_data_t *const data, ticker_event_handler handler) {
sahilmgandhi 18:6a4db94011d3 21 data->interface->init();
sahilmgandhi 18:6a4db94011d3 22
sahilmgandhi 18:6a4db94011d3 23 data->queue->event_handler = handler;
sahilmgandhi 18:6a4db94011d3 24 }
sahilmgandhi 18:6a4db94011d3 25
sahilmgandhi 18:6a4db94011d3 26 void ticker_irq_handler(const ticker_data_t *const data) {
sahilmgandhi 18:6a4db94011d3 27 data->interface->clear_interrupt();
sahilmgandhi 18:6a4db94011d3 28
sahilmgandhi 18:6a4db94011d3 29 /* Go through all the pending TimerEvents */
sahilmgandhi 18:6a4db94011d3 30 while (1) {
sahilmgandhi 18:6a4db94011d3 31 if (data->queue->head == NULL) {
sahilmgandhi 18:6a4db94011d3 32 // There are no more TimerEvents left, so disable matches.
sahilmgandhi 18:6a4db94011d3 33 data->interface->disable_interrupt();
sahilmgandhi 18:6a4db94011d3 34 return;
sahilmgandhi 18:6a4db94011d3 35 }
sahilmgandhi 18:6a4db94011d3 36
sahilmgandhi 18:6a4db94011d3 37 if ((int)(data->queue->head->timestamp - data->interface->read()) <= 0) {
sahilmgandhi 18:6a4db94011d3 38 // This event was in the past:
sahilmgandhi 18:6a4db94011d3 39 // point to the following one and execute its handler
sahilmgandhi 18:6a4db94011d3 40 ticker_event_t *p = data->queue->head;
sahilmgandhi 18:6a4db94011d3 41 data->queue->head = data->queue->head->next;
sahilmgandhi 18:6a4db94011d3 42 if (data->queue->event_handler != NULL) {
sahilmgandhi 18:6a4db94011d3 43 (*data->queue->event_handler)(p->id); // NOTE: the handler can set new events
sahilmgandhi 18:6a4db94011d3 44 }
sahilmgandhi 18:6a4db94011d3 45 /* Note: We continue back to examining the head because calling the
sahilmgandhi 18:6a4db94011d3 46 * event handler may have altered the chain of pending events. */
sahilmgandhi 18:6a4db94011d3 47 } else {
sahilmgandhi 18:6a4db94011d3 48 // This event and the following ones in the list are in the future:
sahilmgandhi 18:6a4db94011d3 49 // set it as next interrupt and return
sahilmgandhi 18:6a4db94011d3 50 data->interface->set_interrupt(data->queue->head->timestamp);
sahilmgandhi 18:6a4db94011d3 51 return;
sahilmgandhi 18:6a4db94011d3 52 }
sahilmgandhi 18:6a4db94011d3 53 }
sahilmgandhi 18:6a4db94011d3 54 }
sahilmgandhi 18:6a4db94011d3 55
sahilmgandhi 18:6a4db94011d3 56 void ticker_insert_event(const ticker_data_t *const data, ticker_event_t *obj, timestamp_t timestamp, uint32_t id) {
sahilmgandhi 18:6a4db94011d3 57 /* disable interrupts for the duration of the function */
sahilmgandhi 18:6a4db94011d3 58 core_util_critical_section_enter();
sahilmgandhi 18:6a4db94011d3 59
sahilmgandhi 18:6a4db94011d3 60 // initialise our data
sahilmgandhi 18:6a4db94011d3 61 obj->timestamp = timestamp;
sahilmgandhi 18:6a4db94011d3 62 obj->id = id;
sahilmgandhi 18:6a4db94011d3 63
sahilmgandhi 18:6a4db94011d3 64 /* Go through the list until we either reach the end, or find
sahilmgandhi 18:6a4db94011d3 65 an element this should come before (which is possibly the
sahilmgandhi 18:6a4db94011d3 66 head). */
sahilmgandhi 18:6a4db94011d3 67 ticker_event_t *prev = NULL, *p = data->queue->head;
sahilmgandhi 18:6a4db94011d3 68 while (p != NULL) {
sahilmgandhi 18:6a4db94011d3 69 /* check if we come before p */
sahilmgandhi 18:6a4db94011d3 70 if ((int)(timestamp - p->timestamp) < 0) {
sahilmgandhi 18:6a4db94011d3 71 break;
sahilmgandhi 18:6a4db94011d3 72 }
sahilmgandhi 18:6a4db94011d3 73 /* go to the next element */
sahilmgandhi 18:6a4db94011d3 74 prev = p;
sahilmgandhi 18:6a4db94011d3 75 p = p->next;
sahilmgandhi 18:6a4db94011d3 76 }
sahilmgandhi 18:6a4db94011d3 77
sahilmgandhi 18:6a4db94011d3 78 /* if we're at the end p will be NULL, which is correct */
sahilmgandhi 18:6a4db94011d3 79 obj->next = p;
sahilmgandhi 18:6a4db94011d3 80
sahilmgandhi 18:6a4db94011d3 81 /* if prev is NULL we're at the head */
sahilmgandhi 18:6a4db94011d3 82 if (prev == NULL) {
sahilmgandhi 18:6a4db94011d3 83 data->queue->head = obj;
sahilmgandhi 18:6a4db94011d3 84 data->interface->set_interrupt(timestamp);
sahilmgandhi 18:6a4db94011d3 85 } else {
sahilmgandhi 18:6a4db94011d3 86 prev->next = obj;
sahilmgandhi 18:6a4db94011d3 87 }
sahilmgandhi 18:6a4db94011d3 88
sahilmgandhi 18:6a4db94011d3 89 core_util_critical_section_exit();
sahilmgandhi 18:6a4db94011d3 90 }
sahilmgandhi 18:6a4db94011d3 91
sahilmgandhi 18:6a4db94011d3 92 void ticker_remove_event(const ticker_data_t *const data, ticker_event_t *obj) {
sahilmgandhi 18:6a4db94011d3 93 core_util_critical_section_enter();
sahilmgandhi 18:6a4db94011d3 94
sahilmgandhi 18:6a4db94011d3 95 // remove this object from the list
sahilmgandhi 18:6a4db94011d3 96 if (data->queue->head == obj) {
sahilmgandhi 18:6a4db94011d3 97 // first in the list, so just drop me
sahilmgandhi 18:6a4db94011d3 98 data->queue->head = obj->next;
sahilmgandhi 18:6a4db94011d3 99 if (data->queue->head == NULL) {
sahilmgandhi 18:6a4db94011d3 100 data->interface->disable_interrupt();
sahilmgandhi 18:6a4db94011d3 101 } else {
sahilmgandhi 18:6a4db94011d3 102 data->interface->set_interrupt(data->queue->head->timestamp);
sahilmgandhi 18:6a4db94011d3 103 }
sahilmgandhi 18:6a4db94011d3 104 } else {
sahilmgandhi 18:6a4db94011d3 105 // find the object before me, then drop me
sahilmgandhi 18:6a4db94011d3 106 ticker_event_t* p = data->queue->head;
sahilmgandhi 18:6a4db94011d3 107 while (p != NULL) {
sahilmgandhi 18:6a4db94011d3 108 if (p->next == obj) {
sahilmgandhi 18:6a4db94011d3 109 p->next = obj->next;
sahilmgandhi 18:6a4db94011d3 110 break;
sahilmgandhi 18:6a4db94011d3 111 }
sahilmgandhi 18:6a4db94011d3 112 p = p->next;
sahilmgandhi 18:6a4db94011d3 113 }
sahilmgandhi 18:6a4db94011d3 114 }
sahilmgandhi 18:6a4db94011d3 115
sahilmgandhi 18:6a4db94011d3 116 core_util_critical_section_exit();
sahilmgandhi 18:6a4db94011d3 117 }
sahilmgandhi 18:6a4db94011d3 118
sahilmgandhi 18:6a4db94011d3 119 timestamp_t ticker_read(const ticker_data_t *const data)
sahilmgandhi 18:6a4db94011d3 120 {
sahilmgandhi 18:6a4db94011d3 121 return data->interface->read();
sahilmgandhi 18:6a4db94011d3 122 }
sahilmgandhi 18:6a4db94011d3 123
sahilmgandhi 18:6a4db94011d3 124 int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *timestamp)
sahilmgandhi 18:6a4db94011d3 125 {
sahilmgandhi 18:6a4db94011d3 126 int ret = 0;
sahilmgandhi 18:6a4db94011d3 127
sahilmgandhi 18:6a4db94011d3 128 /* if head is NULL, there are no pending events */
sahilmgandhi 18:6a4db94011d3 129 core_util_critical_section_enter();
sahilmgandhi 18:6a4db94011d3 130 if (data->queue->head != NULL) {
sahilmgandhi 18:6a4db94011d3 131 *timestamp = data->queue->head->timestamp;
sahilmgandhi 18:6a4db94011d3 132 ret = 1;
sahilmgandhi 18:6a4db94011d3 133 }
sahilmgandhi 18:6a4db94011d3 134 core_util_critical_section_exit();
sahilmgandhi 18:6a4db94011d3 135
sahilmgandhi 18:6a4db94011d3 136 return ret;
sahilmgandhi 18:6a4db94011d3 137 }