SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
CanBuffer.cpp
1#include "CanBuffer.h"
2#include "Canbus.h"
3
4namespace CAN {
5
6/* Setters */
7void Buffer::setDouble(double val) {
8 *(double *)buffer = val;
9 modified = true;
10}
11void Buffer::setULong(uint64_t val) {
12 *(uint64_t *)buffer = val;
13 modified = true;
14}
15void Buffer::setLong(int64_t val) {
16 *(int64_t *)buffer = val;
17 modified = true;
18}
19void Buffer::setFloat(float val, size_t pos) {
20 *(float *)(buffer + pos) = val;
21 modified = true;
22}
23void Buffer::setUInt(uint32_t val, size_t pos) {
24 *(uint32_t *)(buffer + pos) = val;
25 modified = true;
26}
27void Buffer::setInt(int32_t val, size_t pos) {
28 *(int32_t *)(buffer + pos) = val;
29 modified = true;
30}
31void Buffer::setUShort(uint16_t val, size_t pos) {
32 *(uint16_t *)(buffer + pos) = val;
33 modified = true;
34}
35void Buffer::setShort(int16_t val, size_t pos) {
36 *(int16_t *)(buffer + pos) = val;
37 modified = true;
38}
39void Buffer::setUByte(uint8_t val, size_t pos) {
40 buffer[pos] = val;
41 modified = true;
42}
43void Buffer::setByte(int8_t val, size_t pos) {
44 *(int8_t *)(buffer + pos) = val;
45 modified = true;
46}
47void Buffer::setBit(bool val, size_t pos) { // pos is in terms of bit position
48 buffer[pos / 8] &= val << (pos % 8);
49 buffer[pos / 8] |= val << (pos % 8);
50 modified = true;
51}
52
53/* Getters */
54double Buffer::getDouble() {
55 return *(double *)buffer;
56}
57uint64_t Buffer::getULong() {
58 return *(uint64_t *)buffer;
59}
60int64_t Buffer::getLong() {
61 return *(int64_t *)buffer;
62}
63float Buffer::getFloat(size_t pos) {
64 return *(float *)(buffer + pos);
65}
66uint32_t Buffer::getUInt(size_t pos) {
67 return *(uint32_t *)(buffer + pos);
68}
69int32_t Buffer::getInt(size_t pos) {
70 return *(int32_t *)(buffer + pos);
71}
72uint16_t Buffer::getUShort(size_t pos) {
73 return *(uint16_t *)(buffer + pos);
74}
75int16_t Buffer::getShort(size_t pos) {
76 return *(int16_t *)(buffer + pos);
77}
78uint8_t Buffer::getUByte(size_t pos) {
79 return buffer[pos];
80}
81int8_t Buffer::getByte(size_t pos) {
82 return *(int8_t *)(buffer + pos);
83}
84bool Buffer::getBit(size_t pos) { // pos is in terms of bit position
85 return (buffer[pos / 8] >> (pos % 8)) & 1;
86}
87void Buffer::dump(uint8_t *dest) {
88 dest[0] = buffer[0];
89 dest[1] = buffer[1];
90 dest[2] = buffer[2];
91 dest[3] = buffer[3];
92 dest[4] = buffer[4];
93 dest[5] = buffer[5];
94 dest[6] = buffer[6];
95 dest[7] = buffer[7];
96}
97
98void Buffer::set(const uint8_t *src) {
99 buffer[0] = src[0];
100 buffer[1] = src[1];
101 buffer[2] = src[2];
102 buffer[3] = src[3];
103 buffer[4] = src[4];
104 buffer[5] = src[5];
105 buffer[6] = src[6];
106 buffer[7] = src[7];
107}
108
110 buffer[0] = 0;
111 buffer[1] = 0;
112 buffer[2] = 0;
113 buffer[3] = 0;
114 buffer[4] = 0;
115 buffer[5] = 0;
116 buffer[6] = 0;
117 buffer[7] = 0;
118}
119
121 return mux.try_lock();
122}
123
125 mux.lock();
126}
127
129 mux.unlock();
130}
131
132} // namespace CAN
FlexCAN_T4 wrapper.
Canbus functionality. Refer to Canbus.h for more info.
Definition CanBuffer.cpp:4
void set(const uint8_t *src)
Replace the current buffer.
Definition CanBuffer.cpp:98
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
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
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.