PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
6:ea7377f3d1af
direct lcd stuff used by sensitive

Who changed what in which revision?

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