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) 2006-2013 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 #ifndef MBED_CAN_H
Pokitto 5:7e5c566b1760 17 #define MBED_CAN_H
Pokitto 5:7e5c566b1760 18
Pokitto 5:7e5c566b1760 19 #include "platform.h"
Pokitto 5:7e5c566b1760 20
Pokitto 5:7e5c566b1760 21 #if DEVICE_CAN
Pokitto 5:7e5c566b1760 22
Pokitto 5:7e5c566b1760 23 #include "can_api.h"
Pokitto 5:7e5c566b1760 24 #include "can_helper.h"
Pokitto 5:7e5c566b1760 25 #include "FunctionPointer.h"
Pokitto 5:7e5c566b1760 26
Pokitto 5:7e5c566b1760 27 namespace mbed {
Pokitto 5:7e5c566b1760 28
Pokitto 5:7e5c566b1760 29 /** CANMessage class
Pokitto 5:7e5c566b1760 30 */
Pokitto 5:7e5c566b1760 31 class CANMessage : public CAN_Message {
Pokitto 5:7e5c566b1760 32
Pokitto 5:7e5c566b1760 33 public:
Pokitto 5:7e5c566b1760 34 /** Creates empty CAN message.
Pokitto 5:7e5c566b1760 35 */
Pokitto 5:7e5c566b1760 36 CANMessage() : CAN_Message() {
Pokitto 5:7e5c566b1760 37 len = 8;
Pokitto 5:7e5c566b1760 38 type = CANData;
Pokitto 5:7e5c566b1760 39 format = CANStandard;
Pokitto 5:7e5c566b1760 40 id = 0;
Pokitto 5:7e5c566b1760 41 memset(data, 0, 8);
Pokitto 5:7e5c566b1760 42 }
Pokitto 5:7e5c566b1760 43
Pokitto 5:7e5c566b1760 44 /** Creates CAN message with specific content.
Pokitto 5:7e5c566b1760 45 */
Pokitto 5:7e5c566b1760 46 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
Pokitto 5:7e5c566b1760 47 len = _len & 0xF;
Pokitto 5:7e5c566b1760 48 type = _type;
Pokitto 5:7e5c566b1760 49 format = _format;
Pokitto 5:7e5c566b1760 50 id = _id;
Pokitto 5:7e5c566b1760 51 memcpy(data, _data, _len);
Pokitto 5:7e5c566b1760 52 }
Pokitto 5:7e5c566b1760 53
Pokitto 5:7e5c566b1760 54 /** Creates CAN remote message.
Pokitto 5:7e5c566b1760 55 */
Pokitto 5:7e5c566b1760 56 CANMessage(int _id, CANFormat _format = CANStandard) {
Pokitto 5:7e5c566b1760 57 len = 0;
Pokitto 5:7e5c566b1760 58 type = CANRemote;
Pokitto 5:7e5c566b1760 59 format = _format;
Pokitto 5:7e5c566b1760 60 id = _id;
Pokitto 5:7e5c566b1760 61 memset(data, 0, 8);
Pokitto 5:7e5c566b1760 62 }
Pokitto 5:7e5c566b1760 63 };
Pokitto 5:7e5c566b1760 64
Pokitto 5:7e5c566b1760 65 /** A can bus client, used for communicating with can devices
Pokitto 5:7e5c566b1760 66 */
Pokitto 5:7e5c566b1760 67 class CAN {
Pokitto 5:7e5c566b1760 68
Pokitto 5:7e5c566b1760 69 public:
Pokitto 5:7e5c566b1760 70 /** Creates an CAN interface connected to specific pins.
Pokitto 5:7e5c566b1760 71 *
Pokitto 5:7e5c566b1760 72 * @param rd read from transmitter
Pokitto 5:7e5c566b1760 73 * @param td transmit to transmitter
Pokitto 5:7e5c566b1760 74 *
Pokitto 5:7e5c566b1760 75 * Example:
Pokitto 5:7e5c566b1760 76 * @code
Pokitto 5:7e5c566b1760 77 * #include "mbed.h"
Pokitto 5:7e5c566b1760 78 *
Pokitto 5:7e5c566b1760 79 * Ticker ticker;
Pokitto 5:7e5c566b1760 80 * DigitalOut led1(LED1);
Pokitto 5:7e5c566b1760 81 * DigitalOut led2(LED2);
Pokitto 5:7e5c566b1760 82 * CAN can1(p9, p10);
Pokitto 5:7e5c566b1760 83 * CAN can2(p30, p29);
Pokitto 5:7e5c566b1760 84 *
Pokitto 5:7e5c566b1760 85 * char counter = 0;
Pokitto 5:7e5c566b1760 86 *
Pokitto 5:7e5c566b1760 87 * void send() {
Pokitto 5:7e5c566b1760 88 * if(can1.write(CANMessage(1337, &counter, 1))) {
Pokitto 5:7e5c566b1760 89 * printf("Message sent: %d\n", counter);
Pokitto 5:7e5c566b1760 90 * counter++;
Pokitto 5:7e5c566b1760 91 * }
Pokitto 5:7e5c566b1760 92 * led1 = !led1;
Pokitto 5:7e5c566b1760 93 * }
Pokitto 5:7e5c566b1760 94 *
Pokitto 5:7e5c566b1760 95 * int main() {
Pokitto 5:7e5c566b1760 96 * ticker.attach(&send, 1);
Pokitto 5:7e5c566b1760 97 * CANMessage msg;
Pokitto 5:7e5c566b1760 98 * while(1) {
Pokitto 5:7e5c566b1760 99 * if(can2.read(msg)) {
Pokitto 5:7e5c566b1760 100 * printf("Message received: %d\n\n", msg.data[0]);
Pokitto 5:7e5c566b1760 101 * led2 = !led2;
Pokitto 5:7e5c566b1760 102 * }
Pokitto 5:7e5c566b1760 103 * wait(0.2);
Pokitto 5:7e5c566b1760 104 * }
Pokitto 5:7e5c566b1760 105 * }
Pokitto 5:7e5c566b1760 106 * @endcode
Pokitto 5:7e5c566b1760 107 */
Pokitto 5:7e5c566b1760 108 CAN(PinName rd, PinName td);
Pokitto 5:7e5c566b1760 109 virtual ~CAN();
Pokitto 5:7e5c566b1760 110
Pokitto 5:7e5c566b1760 111 /** Set the frequency of the CAN interface
Pokitto 5:7e5c566b1760 112 *
Pokitto 5:7e5c566b1760 113 * @param hz The bus frequency in hertz
Pokitto 5:7e5c566b1760 114 *
Pokitto 5:7e5c566b1760 115 * @returns
Pokitto 5:7e5c566b1760 116 * 1 if successful,
Pokitto 5:7e5c566b1760 117 * 0 otherwise
Pokitto 5:7e5c566b1760 118 */
Pokitto 5:7e5c566b1760 119 int frequency(int hz);
Pokitto 5:7e5c566b1760 120
Pokitto 5:7e5c566b1760 121 /** Write a CANMessage to the bus.
Pokitto 5:7e5c566b1760 122 *
Pokitto 5:7e5c566b1760 123 * @param msg The CANMessage to write.
Pokitto 5:7e5c566b1760 124 *
Pokitto 5:7e5c566b1760 125 * @returns
Pokitto 5:7e5c566b1760 126 * 0 if write failed,
Pokitto 5:7e5c566b1760 127 * 1 if write was successful
Pokitto 5:7e5c566b1760 128 */
Pokitto 5:7e5c566b1760 129 int write(CANMessage msg);
Pokitto 5:7e5c566b1760 130
Pokitto 5:7e5c566b1760 131 /** Read a CANMessage from the bus.
Pokitto 5:7e5c566b1760 132 *
Pokitto 5:7e5c566b1760 133 * @param msg A CANMessage to read to.
Pokitto 5:7e5c566b1760 134 * @param handle message filter handle (0 for any message)
Pokitto 5:7e5c566b1760 135 *
Pokitto 5:7e5c566b1760 136 * @returns
Pokitto 5:7e5c566b1760 137 * 0 if no message arrived,
Pokitto 5:7e5c566b1760 138 * 1 if message arrived
Pokitto 5:7e5c566b1760 139 */
Pokitto 5:7e5c566b1760 140 int read(CANMessage &msg, int handle = 0);
Pokitto 5:7e5c566b1760 141
Pokitto 5:7e5c566b1760 142 /** Reset CAN interface.
Pokitto 5:7e5c566b1760 143 *
Pokitto 5:7e5c566b1760 144 * To use after error overflow.
Pokitto 5:7e5c566b1760 145 */
Pokitto 5:7e5c566b1760 146 void reset();
Pokitto 5:7e5c566b1760 147
Pokitto 5:7e5c566b1760 148 /** Puts or removes the CAN interface into silent monitoring mode
Pokitto 5:7e5c566b1760 149 *
Pokitto 5:7e5c566b1760 150 * @param silent boolean indicating whether to go into silent mode or not
Pokitto 5:7e5c566b1760 151 */
Pokitto 5:7e5c566b1760 152 void monitor(bool silent);
Pokitto 5:7e5c566b1760 153
Pokitto 5:7e5c566b1760 154 enum Mode {
Pokitto 5:7e5c566b1760 155 Reset = 0,
Pokitto 5:7e5c566b1760 156 Normal,
Pokitto 5:7e5c566b1760 157 Silent,
Pokitto 5:7e5c566b1760 158 LocalTest,
Pokitto 5:7e5c566b1760 159 GlobalTest,
Pokitto 5:7e5c566b1760 160 SilentTest
Pokitto 5:7e5c566b1760 161 };
Pokitto 5:7e5c566b1760 162
Pokitto 5:7e5c566b1760 163 /** Change CAN operation to the specified mode
Pokitto 5:7e5c566b1760 164 *
Pokitto 5:7e5c566b1760 165 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
Pokitto 5:7e5c566b1760 166 *
Pokitto 5:7e5c566b1760 167 * @returns
Pokitto 5:7e5c566b1760 168 * 0 if mode change failed or unsupported,
Pokitto 5:7e5c566b1760 169 * 1 if mode change was successful
Pokitto 5:7e5c566b1760 170 */
Pokitto 5:7e5c566b1760 171 int mode(Mode mode);
Pokitto 5:7e5c566b1760 172
Pokitto 5:7e5c566b1760 173 /** Filter out incomming messages
Pokitto 5:7e5c566b1760 174 *
Pokitto 5:7e5c566b1760 175 * @param id the id to filter on
Pokitto 5:7e5c566b1760 176 * @param mask the mask applied to the id
Pokitto 5:7e5c566b1760 177 * @param format format to filter on (Default CANAny)
Pokitto 5:7e5c566b1760 178 * @param handle message filter handle (Optional)
Pokitto 5:7e5c566b1760 179 *
Pokitto 5:7e5c566b1760 180 * @returns
Pokitto 5:7e5c566b1760 181 * 0 if filter change failed or unsupported,
Pokitto 5:7e5c566b1760 182 * new filter handle if successful
Pokitto 5:7e5c566b1760 183 */
Pokitto 5:7e5c566b1760 184 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
Pokitto 5:7e5c566b1760 185
Pokitto 5:7e5c566b1760 186 /** Returns number of read errors to detect read overflow errors.
Pokitto 5:7e5c566b1760 187 */
Pokitto 5:7e5c566b1760 188 unsigned char rderror();
Pokitto 5:7e5c566b1760 189
Pokitto 5:7e5c566b1760 190 /** Returns number of write errors to detect write overflow errors.
Pokitto 5:7e5c566b1760 191 */
Pokitto 5:7e5c566b1760 192 unsigned char tderror();
Pokitto 5:7e5c566b1760 193
Pokitto 5:7e5c566b1760 194 enum IrqType {
Pokitto 5:7e5c566b1760 195 RxIrq = 0,
Pokitto 5:7e5c566b1760 196 TxIrq,
Pokitto 5:7e5c566b1760 197 EwIrq,
Pokitto 5:7e5c566b1760 198 DoIrq,
Pokitto 5:7e5c566b1760 199 WuIrq,
Pokitto 5:7e5c566b1760 200 EpIrq,
Pokitto 5:7e5c566b1760 201 AlIrq,
Pokitto 5:7e5c566b1760 202 BeIrq,
Pokitto 5:7e5c566b1760 203 IdIrq
Pokitto 5:7e5c566b1760 204 };
Pokitto 5:7e5c566b1760 205
Pokitto 5:7e5c566b1760 206 /** Attach a function to call whenever a CAN frame received interrupt is
Pokitto 5:7e5c566b1760 207 * generated.
Pokitto 5:7e5c566b1760 208 *
Pokitto 5:7e5c566b1760 209 * @param fptr A pointer to a void function, or 0 to set as none
Pokitto 5:7e5c566b1760 210 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
Pokitto 5:7e5c566b1760 211 */
Pokitto 5:7e5c566b1760 212 void attach(void (*fptr)(void), IrqType type=RxIrq);
Pokitto 5:7e5c566b1760 213
Pokitto 5:7e5c566b1760 214 /** Attach a member function to call whenever a CAN frame received interrupt
Pokitto 5:7e5c566b1760 215 * is generated.
Pokitto 5:7e5c566b1760 216 *
Pokitto 5:7e5c566b1760 217 * @param tptr pointer to the object to call the member function on
Pokitto 5:7e5c566b1760 218 * @param mptr pointer to the member function to be called
Pokitto 5:7e5c566b1760 219 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
Pokitto 5:7e5c566b1760 220 */
Pokitto 5:7e5c566b1760 221 template<typename T>
Pokitto 5:7e5c566b1760 222 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
Pokitto 5:7e5c566b1760 223 if((mptr != NULL) && (tptr != NULL)) {
Pokitto 5:7e5c566b1760 224 _irq[type].attach(tptr, mptr);
Pokitto 5:7e5c566b1760 225 can_irq_set(&_can, (CanIrqType)type, 1);
Pokitto 5:7e5c566b1760 226 }
Pokitto 5:7e5c566b1760 227 else {
Pokitto 5:7e5c566b1760 228 can_irq_set(&_can, (CanIrqType)type, 0);
Pokitto 5:7e5c566b1760 229 }
Pokitto 5:7e5c566b1760 230 }
Pokitto 5:7e5c566b1760 231
Pokitto 5:7e5c566b1760 232 static void _irq_handler(uint32_t id, CanIrqType type);
Pokitto 5:7e5c566b1760 233
Pokitto 5:7e5c566b1760 234 protected:
Pokitto 5:7e5c566b1760 235 can_t _can;
Pokitto 5:7e5c566b1760 236 FunctionPointer _irq[9];
Pokitto 5:7e5c566b1760 237 };
Pokitto 5:7e5c566b1760 238
Pokitto 5:7e5c566b1760 239 } // namespace mbed
Pokitto 5:7e5c566b1760 240
Pokitto 5:7e5c566b1760 241 #endif
Pokitto 5:7e5c566b1760 242
Pokitto 5:7e5c566b1760 243 #endif // MBED_CAN_H