SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
CanBuffer.h
1#ifndef __ECU_CANBUFFER_H__
2#define __ECU_CANBUFFER_H__
3
4#include <stdint.h>
5#include <stdlib.h>
6
7#include "TeensyThreads.h"
8
9namespace CAN {
10
17typedef void (*canCallback)(uint32_t, volatile uint8_t *);
18
23struct Buffer { // IMPROVE: more rigorous testing on the get funcs
27 const uint32_t address;
28
34 volatile uint8_t *buffer;
35
40
44 const bool outgoing;
45
50 bool modified = false;
51
57 Buffer(volatile uint8_t *buffer, bool outgoing = false) : address(0), buffer(buffer), outgoing(outgoing){};
64 Buffer(const uint32_t address, volatile uint8_t *buffer, bool outgoing = false) : address(address), buffer(buffer), outgoing(outgoing){};
68 void init();
76 void dump(uint8_t *dest);
84 void set(const uint8_t *src);
90 void clear(void);
91
92 void Buffer::setDouble(double val);
93 void Buffer::setULong(uint64_t val);
94 void Buffer::setLong(int64_t val);
95 void Buffer::setFloat(float val, size_t pos);
96 void Buffer::setUInt(uint32_t val, size_t pos);
97 void Buffer::setInt(int32_t val, size_t pos);
98 void Buffer::setUShort(uint16_t val, size_t pos);
99 void Buffer::setShort(int16_t val, size_t pos);
100 void Buffer::setUByte(uint8_t val, size_t pos);
101 void Buffer::setByte(int8_t val, size_t pos);
102 void Buffer::setBit(bool val, size_t pos);
103
104 double Buffer::getDouble();
105
106 float Buffer::getFloat(size_t pos);
107
114 uint64_t getULong();
121 int64_t getLong();
128 uint32_t getUInt(size_t pos);
135 int32_t getInt(size_t pos);
142 uint16_t getUShort(size_t pos);
149 int16_t getShort(size_t pos);
156 uint8_t getUByte(size_t pos);
163 int8_t getByte(size_t pos);
170 bool getBit(size_t pos);
171
178 bool lock();
179
184 void lock_wait();
185
189 void unlock();
190
191private:
192 Thread::Mutex mux; // TODO: make shared_mutex to allow multiple readers
193};
194} // namespace CAN
195#endif // __ECU_CANBUFFER_H__
Canbus functionality. Refer to Canbus.h for more info.
Definition CanBuffer.cpp:4
void(* canCallback)(uint32_t, volatile uint8_t *)
The function type to pass to addCallback.
Definition CanBuffer.h:17
An incoming canbus message, allows the message data to be interpreted through a simple wrapper class.
Definition CanBuffer.h:23
void set(const uint8_t *src)
Replace the current buffer.
Definition CanBuffer.cpp:98
const uint32_t address
Address this buffer represents.
Definition CanBuffer.h:27
void init()
Initialize a buffer if not done so already by constructor.
volatile canCallback callback
Optional callback function associated with this buffer.
Definition CanBuffer.h:39
bool modified
Whether this buffer has been set in anyway.
Definition CanBuffer.h:50
int8_t getByte(size_t pos)
Interpret the buffer as a Byte at byte position pos
Definition CanBuffer.cpp:81
uint64_t getULong()
Interpret the buffer as an unsigned long.
Definition CanBuffer.cpp:57
uint32_t getUInt(size_t pos)
Interpret the buffer as an unsigned Integer at byte position pos
Definition CanBuffer.cpp:66
int32_t getInt(size_t pos)
Interpret the buffer as an Integer at byte position pos
Definition CanBuffer.cpp:69
int16_t getShort(size_t pos)
Interpret the buffer as an Short at byte position pos
Definition CanBuffer.cpp:75
Buffer(const uint32_t address, volatile uint8_t *buffer, bool outgoing=false)
Construct a new internal Buffer.
Definition CanBuffer.h:64
int64_t getLong()
Interpret the buffer as a long.
Definition CanBuffer.cpp:60
bool getBit(size_t pos)
Get the bit at position pos of this buffer.
Definition CanBuffer.cpp:84
void clear(void)
Clear the buffer.
void dump(uint8_t *dest)
Dump the current buffer onto an external one.
Definition CanBuffer.cpp:87
const bool outgoing
Whether this buffer is meant to be an outgoing message.
Definition CanBuffer.h:44
Buffer(volatile uint8_t *buffer, bool outgoing=false)
Construct a new Buffer as a wrapper.
Definition CanBuffer.h:57
uint8_t getUByte(size_t pos)
Interpret the buffer as an unsigned Byte at byte position pos
Definition CanBuffer.cpp:78
void lock_wait()
locks this buffer to be used, waits indefinitely for it to unlock if it is locked
volatile uint8_t * buffer
The buffer.
Definition CanBuffer.h:34
bool lock()
Locks this buffer to be used, returns false if it was unable todo so.
uint16_t getUShort(size_t pos)
Interpret the buffer as an unsigned Short at byte position pos
Definition CanBuffer.cpp:72
void unlock()
Unlocks this buffer if it is locked.