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

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

PokittoLib

Library for programming Pokitto hardware

How to Use

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Wed Oct 11 20:35:27 2017 +0000
Revision:
5:ea7377f3d1af
Fixed PokittoLib. Includes a working custom mbed-src

Who changed what in which revision?

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