lol

Dependencies:   MMA8451Q

Fork of Application by Mateusz Kowalik

Committer:
danix
Date:
Sun Jan 21 22:28:30 2018 +0000
Revision:
12:3a30cdffa27c
Parent:
10:41552d038a69
Working acelerometer and mouse

Who changed what in which revision?

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