You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.6 KiB
83 lines
1.6 KiB
#pragma once
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <functional>
|
|
|
|
//
|
|
namespace iflytop {
|
|
using namespace std;
|
|
class TMCReg {
|
|
private:
|
|
/* data */
|
|
public:
|
|
uint8_t flag = 0;
|
|
uint8_t reg = 0;
|
|
uint8_t val = 0;
|
|
|
|
void setActiveFlag() { flag = flag | 0x01; }
|
|
bool isActive() { return flag & 0x01; }
|
|
|
|
void setUpdateFlag() { flag = flag | 0x02; }
|
|
bool isUpdate() { return flag & 0x02; }
|
|
};
|
|
|
|
class TMCRegCache {
|
|
public:
|
|
TMCReg cache[50];
|
|
|
|
public:
|
|
void foreach (function<void(TMCReg* item)> fn) {
|
|
for (size_t i = 0; i < sizeof(cache) / sizeof(cache[0]); i++) {
|
|
if (cache[i].isActive() && cache[i].isUpdate()) {
|
|
fn(&cache[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool activeReg(uint8_t reg) {
|
|
TMCReg* item = findReg(reg);
|
|
if (item) return true;
|
|
|
|
item = findPassive();
|
|
if (item) {
|
|
item->reg = reg;
|
|
item->setActiveFlag();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void setReg(uint8_t reg, uint8_t val) {
|
|
TMCReg* item = findReg(reg);
|
|
if (item) {
|
|
item->val = val;
|
|
item->setUpdateFlag();
|
|
}
|
|
}
|
|
|
|
private:
|
|
TMCReg* findPassive() {
|
|
for (size_t i = 0; i < sizeof(cache) / sizeof(cache[0]); i++) {
|
|
if (!cache[i].isActive()) {
|
|
return &cache[i];
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
TMCReg* findReg(uint8_t reg) {
|
|
for (size_t i = 0; i < sizeof(cache) / sizeof(cache[0]); i++) {
|
|
if (cache[i].isActive() && cache[i].reg == reg) {
|
|
return &cache[i];
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
};
|
|
|
|
} // namespace iflytop
|