Open FFBoard
Open source force feedback firmware
EncoderLocal.cpp
Go to the documentation of this file.
1/*
2 * EncoderLocal.cpp
3 *
4 * Created on: 02.02.2020
5 * Author: Yannick
6 */
7
8#include "EncoderLocal.h"
9#include "flash_helpers.h"
10#ifdef LOCALENCODER
11bool EncoderLocal::inUse = false;
13 .name = "Local ABN" ,
14 .id=CLSID_ENCODER_LOCAL,
15 };
17 return info;
18}
19
20EncoderLocal::EncoderLocal() : CommandHandler("localenc",CLSID_ENCODER_LOCAL) {
22 this->restoreFlash();
23 this->htim = &TIM_ENC;
24 HAL_TIM_Base_Start_IT(htim); // May immediately call overflow. Initialize count again
25 this->htim->Instance->CNT = 0x7fff;
26 pos = 0;
27 this->htim->Instance->CR1 = 1;
28
29 if(!useIndex)
30 setPos(0);
31
33}
34
37 registerCommand("cpr", EncoderLocal_commands::cpr, "CPR of encoder",CMDFLAG_GET|CMDFLAG_SET);
38 registerCommand("index", EncoderLocal_commands::useindex, "Enable index pin",CMDFLAG_GET|CMDFLAG_SET);
39}
40
42 EncoderLocal::inUse = false;
43 this->htim->Instance->CR1 = 0;
44}
45
46
48 Flash_Write(ADR_ENCLOCAL_CPR, this->getCpr());
49 Flash_Write(ADR_ENCLOCAL_OFS, useIndex ? (int16_t)this->offset : 0); // 0 offset indicates index off
50
51}
52
54 uint16_t cpr = 0;
55 if (Flash_Read(ADR_ENCLOCAL_CPR, &cpr)){
56 this->setCpr(cpr);
57 }
58 uint16_t ofs = 0;
59 if(Flash_Read(ADR_ENCLOCAL_OFS, &ofs)){
60 this->offset = (int16_t)ofs;
61 useIndex = ofs != 0;
62 }
63
64}
65
67 return (int32_t)htim->Instance->CNT - (int32_t)0x7fff;
68}
69
72}
73
74
76 return getTimerCount() + pos;
77}
78
79void EncoderLocal::setPos(int32_t pos){
80 int32_t cnt = getTimerCount();
81
82 this->pos = pos - cnt;
83 if(useIndex && indexHit){
84 int32_t idxpos = ((int32_t)indexpos - (int32_t)0x7fff);
85 offset = ( ( idxpos-cnt-pos ) % this->cpr); // offset is distance to index position (0 = 0x7fff index)
86 }
87 //this->pos = pos - cnt;
88 //htim->Instance->CNT = 0x7fff; // Reset //pos+0x7fff;
89
90
91}
92
93
94void EncoderLocal::setPeriod(uint32_t period){
95 this->htim->Instance->ARR = period-1;
96}
97
98void EncoderLocal::exti(uint16_t GPIO_Pin){
99 if(GPIO_Pin == ENCODER_Z_Pin){
100 if(HAL_GPIO_ReadPin(ENCODER_Z_GPIO_Port, ENCODER_Z_Pin) == GPIO_PIN_RESET){
101 // Encoder Z pin activated
102
103 if(useIndex && !indexHit){
104 pulseSysLed();
105 htim->Instance->CNT = 0x7fff; // Center encoder
106 this->pos = offset;
107 }
108 indexpos = htim->Instance->CNT; // Store current encoder count
109 indexHit = true;
110 }
111
112 }
113}
114
115void EncoderLocal::timerElapsed(TIM_HandleTypeDef* htim){
116 if(htim == this->htim){
118 }
119}
120
122 if(htim->Instance->CNT > this->htim->Instance->ARR/2){
123 pos -= htim->Instance->ARR+1;
124 }else{
125 pos += htim->Instance->ARR+1;
126 }
127}
128
129void EncoderLocal::setCpr(uint32_t cpr){
130 this->cpr = cpr;
131}
132
133CommandStatus EncoderLocal::command(const ParsedCommand& cmd,std::vector<CommandReply>& replies){
134 switch(static_cast<EncoderLocal_commands>(cmd.cmdId)){
137 break;
139 return handleGetSet(cmd, replies, useIndex);
140 break;
141 default:
143 }
144
145 return CommandStatus::OK;
146}
147#endif
CommandStatus
EncoderType
Definition: Encoder.h:27
TIM_HandleTypeDef TIM_ENC
static CommandStatus handleGetFuncSetFunc(const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal(cls1::*getfunc)(), void(cls2::*setfunc)(TVal), cls *obj)
void registerCommand(const char *cmd, const ID cmdid, const char *help=nullptr, uint32_t flags=0)
static CommandStatus handleGetSet(const ParsedCommand &cmd, std::vector< CommandReply > &replies, TVal &value)
virtual uint32_t getCpr()
Definition: Encoder.cpp:36
uint32_t cpr
Definition: Encoder.h:53
int32_t getTimerCount()
int32_t getPos()
void setPos(int32_t pos)
virtual ~EncoderLocal()
void exti(uint16_t GPIO_Pin) override
void timerElapsed(TIM_HandleTypeDef *htim)
void setPeriod(uint32_t period)
static bool inUse
Definition: EncoderLocal.h:29
const ClassIdentifier getInfo()
Command handlers always have class infos. Works well with ChoosableClass.
void registerCommands()
static ClassIdentifier info
Definition: EncoderLocal.h:30
void saveFlash()
void restoreFlash()
int16_t offset
Definition: EncoderLocal.h:61
TIM_HandleTypeDef * htim
Definition: EncoderLocal.h:60
void overflowCallback()
EncoderType getEncoderType()
CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
uint32_t indexpos
Definition: EncoderLocal.h:63
void setCpr(uint32_t cpr)
int32_t pos
Definition: EncoderLocal.h:62
bool Flash_Write(uint16_t adr, uint16_t dat)
bool Flash_Read(uint16_t adr, uint16_t *buf, bool checkempty=true)
void pulseSysLed()
Definition: ledEffects.cpp:39
const char * name
uint32_t cmdId