PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
spinal
Date:
Sun Oct 15 18:03:02 2017 +0000
Revision:
11:02ad9c807a21
Parent:
5:7e5c566b1760
fixed 4color refreshRegion code

Who changed what in which revision?

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