3 changed files with 126 additions and 9 deletions
@ -0,0 +1,83 @@ |
|||||
|
#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
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue