#ifndef _CMDPANEL_H_ #define _CMDPANEL_H_ #include #include #include "common/utilities/loop.h" #include "message/arm_common.h" enum class KeyPress{ RELEASE, PRESS, REPEAT }; enum class ActionType{ EMPTY, STATE, VALUE }; struct KeyCmd{ std::string c; KeyPress keyPress; }; class KeyAction{ public: KeyAction(ActionType type); virtual ~KeyAction(){}; ActionType getType(){return _type;} protected: ActionType _type; }; class StateAction : public KeyAction{ public: StateAction(std::string c, int state, KeyPress press = KeyPress::PRESS); virtual ~StateAction(){}; int getState(){return _state;}; bool handleCmd(KeyCmd keyCmd, int &state); protected: int _state; KeyCmd _keyCmdSet; }; class EmptyAction : public StateAction{ public: EmptyAction(int state); ~EmptyAction(){}; private: }; class ValueAction : public KeyAction{ public: ValueAction(std::string cUp, std::string cDown, double deltaValue, double initValue = 0.0); ValueAction(std::string cUp, std::string cDown, std::string cGoZero, double deltaValue, double initValue = 0.0); ValueAction(std::string cUp, std::string cDown, double deltaValue, double limit1, double limit2, double initValue = 0.0); ValueAction(std::string cUp, std::string cDown, std::string cGoZero, double deltaValue, double limit1, double limit2, double initValue = 0.0); ~ValueAction(){}; bool handleCmd(KeyCmd keyCmd); void setDt(double dt); double getValue(); double getDValue(); double getDirection(){return _changeDirection;}; void setValue(double value){_value = value;} private: double _value; double _changeDirection=0.0; double _dV = 0.0; //delta value per delta time double _dt = 0.0; double _dVdt = 0.0; // delta value per second double _dVdtf = 0.0; // delta value per second after filter double _lim1, _lim2; bool _hasLim = false; bool _hasGoZero = false; KeyCmd _upCmdSet; KeyCmd _downCmdSet; KeyCmd _goZeroCmdSet; }; class CmdPanel{ public: CmdPanel(std::vector events, EmptyAction emptyAction, size_t channelNum = 1, double dt = 0.002); virtual ~CmdPanel(); virtual int getState(size_t channelID = 0); std::vector getValues() {return _values;}; std::vector getDValues() {return _dValues;}; std::vector getDirections() {return _changeDirections;}; void setValue(std::vector values); void setValue(double value, size_t id); virtual std::string getString(std::string slogan); virtual std::vector stringToArray(std::string slogan); virtual std::vector > stringToMatrix(std::string slogan); virtual SendCmd getSendCmd(); virtual void setRecvState(RecvState& recvState){}; protected: virtual void _read() = 0; void _run(); void _updateState(); void _pressKeyboard(); void _releaseKeyboard(); LoopFunc *_runThread; LoopFunc *_readThread; std::vector _stateEvents; std::vector _valueEvents; EmptyAction _emptyAction; size_t _actionNum = 0; size_t _stateNum = 0; size_t _valueNum = 0; size_t _channelNum; std::vector _values; std::vector _dValues; std::vector _changeDirections; int _state; std::vector> _stateQueue; std::vector _outputState; std::vector _getState; double _dt; KeyCmd _keyCmd; std::string _cPast = ""; bool _running = true; }; #endif