Create scs dict

This commit is contained in:
Stefano Calabretti
2022-02-03 18:55:52 +01:00
parent d22f9539b3
commit f7b5c0315e
2 changed files with 57 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
#ifndef INC_MMR_CAN_SCS_H_
#define INC_MMR_CAN_SCS_H_
#ifndef MMR_CAN_SCS_DICTIONARY_SIZE
#define MMR_CAN_SCS_DICTIONARY_SIZE 5
#endif
#include <stdint.h>
#include "mmr_can.h"
typedef struct {
uint32_t key;
uint32_t delayStart;
} MmrCanScsDictEntry;
typedef MmrCanScsDictEntry MmrCanScsDictionary[MMR_SCS_DICTIONARY_SIZE];
typedef void(*MmrCanScsDictAction)(MmrCanScsDictEntry *entry);
uint32_t MMR_CAN_ScsDictPut(MmrCanScsDictEntry entry);
void MMR_CAN_ScsDictRemove(uint32_t key);
void MMR_CAN_ScsDictForeach(MmrCanScsDictAction action);
uint32_t MMR_CAN_ScsGetKey(MmrCanMessage *message);
#endif // !INC_MMR_CAN_SCS_H_
+30
View File
@@ -0,0 +1,30 @@
#include "mmr_can_scs.h"
static MmrCanScsDictionary _dictionary;
uint32_t MMR_CAN_ScsDictPut(MmrCanScsDictEntry entry) {
_dictionary[entry.key % MMR_CAN_SCS_DICTIONARY_SIZE] = entry.delayStart;
}
void MMR_CAN_ScsDictRemove(uint32_t key) {
_dictionary[key] = {};
}
void MMR_CAN_ScsDictForeach(MmrCanScsDictAction action) {
int i = 0;
for (; i < MMR_CAN_SCS_DICTIONARY_SIZE; i++) {
MmrCanScsDictEntry entry = _dictionary[i];
if (entry.key != 0) {
action(&entry);
}
}
}
uint32_t MMR_CAN_ScsMessageAsKey(MmrCanMessage *message) {
uint16_t upperHalf = message->header.messageId;
uint16_t lowerHalf = message->header.messageType;
return (upperHalf << 16) | lowerHalf;
}