From b631a44abb2fb5b52cd0e2601bf777527b08446e Mon Sep 17 00:00:00 2001 From: uvos Date: Mon, 10 Jun 2024 20:06:08 +0200 Subject: [PATCH] update button handling --- buttons.h | 7 +++- fusebits | 1 + main.cpp | 118 ++++++++++++++++++++++++++++++++++++---------------- stepper.cpp | 40 +++++++++++++++--- stepper.h | 18 ++++---- writepin.h | 1 - 6 files changed, 133 insertions(+), 52 deletions(-) create mode 100644 fusebits diff --git a/buttons.h b/buttons.h index ff5e766..303c14b 100644 --- a/buttons.h +++ b/buttons.h @@ -8,8 +8,10 @@ class Buttons public: static constexpr uint8_t PRESSED = 0; - static constexpr uint8_t LONG_PRESSED = 1; static constexpr uint8_t RELEASED = 2; + static constexpr uint8_t LONG_PRESSED = 1; + static constexpr uint8_t LONG_RELEASED= 3; + private: volatile uint8_t * const pinReg = &PIND; @@ -31,7 +33,8 @@ void Buttons::tick() { if(readPin(pinReg, button[i]) == true) { - if(buttonCount[i] > 2) _eventHandler(i, RELEASED, _userData); + if(buttonCount[i] >= 100) _eventHandler(i, LONG_RELEASED, _userData); + else if(buttonCount[i] >= 3)_eventHandler(i, RELEASED, _userData); buttonCount[i] = 0; } else diff --git a/fusebits b/fusebits new file mode 100644 index 0000000..7965ede --- /dev/null +++ b/fusebits @@ -0,0 +1 @@ +avrdude -B10 -v -p attiny2313 -c usbasp -U lfuse:w:0xcc:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m diff --git a/main.cpp b/main.cpp index 90a040f..c794b4b 100644 --- a/main.cpp +++ b/main.cpp @@ -9,24 +9,42 @@ #include "buttons.h" #include "eeprom.h" -#define LED_1_PIN PB3 -#define LED_2_PIN PB4 +#define LED_2_PIN PB3 +#define LED_1_PIN PB4 #define BUTTON_1_PIN PD5 #define BUTTON_2_PIN PD6 bool setup = false; +bool direction = false; static bool buttonLongpressed[2]; -uint8_t lockedSpeed = 180; +uint16_t lockedSpeed = 0xFF00; +Stepper stepper(&PORTD, PD0, PD1, PD2, PD3); -void debugBlink(bool fast = true) +ISR(TIMER0_COMPA_vect) +{ + stepper.tick(); +} + +void blink(bool fast = true, bool red = false) { _delay_ms(100); if(!fast) _delay_ms(200); - PORTB |= 1 << LED_1_PIN; + PORTB |= 1 << red ? LED_2_PIN : LED_1_PIN; _delay_ms(100); if(!fast) _delay_ms(200); - PORTB &= ~(1 << LED_1_PIN); + PORTB &= ~(1 << red ? LED_2_PIN : LED_1_PIN); +} + +void lowBattery(Stepper* stepper) +{ + stepper->setEndlesMove(false); + stepper->shutdown(); + writePin(&PORTB, LED_1_PIN, false); + while(true) + { + blink(true, true); + } } void buttonHandler(uint8_t index, uint8_t type, void* data) @@ -36,21 +54,23 @@ void buttonHandler(uint8_t index, uint8_t type, void* data) { if(type == Buttons::PRESSED) { + stepper->setHalfCurrent(false); if(index == 0) { - stepper->setSpeed(230); + stepper->setSpeed(0xFFEA); stepper->setEndlesMove(true, true); } else { - stepper->setSpeed(230); + stepper->setSpeed(0xFFEA); stepper->setEndlesMove(true, false); } } - else if(type == Buttons::RELEASED) + else if(type == Buttons::RELEASED || type == Buttons::LONG_RELEASED) { stepper->setSpeed(lockedSpeed); - stepper->setEndlesMove(true, true); + stepper->setEndlesMove(true, direction); + stepper->setHalfCurrent(true); buttonLongpressed[0] = false; buttonLongpressed[1] = false; } @@ -59,16 +79,17 @@ void buttonHandler(uint8_t index, uint8_t type, void* data) buttonLongpressed[index] = true; if(buttonLongpressed[0] && buttonLongpressed[1]) { + stepper->setHalfCurrent(false); writePin(&PORTB, LED_2_PIN, true); writePin(&PORTB, LED_1_PIN, false); setup = true; buttonLongpressed[0] = false; buttonLongpressed[1] = false; stepper->setSpeed(lockedSpeed); - stepper->setEndlesMove(true, true); - debugBlink(); - debugBlink(); - debugBlink(); + stepper->setEndlesMove(true, direction); + blink(); + blink(); + blink(); } } } @@ -78,21 +99,17 @@ void buttonHandler(uint8_t index, uint8_t type, void* data) if(type == Buttons::RELEASED) { writePin(&PORTB, LED_1_PIN, false); - if(index == 0) - { - lockedSpeed+=5; - } - else - { - lockedSpeed-=5; - } - EEPROM_write_char(0, lockedSpeed); + if(index == 0 && lockedSpeed < 0xFF00) lockedSpeed+=80; + else if(lockedSpeed > 0xEF00) lockedSpeed-=80; + cli(); + EEPROM_write_char(6, lockedSpeed>>8); + EEPROM_write_char(5, lockedSpeed & 0x00FF); + sei(); stepper->setSpeed(lockedSpeed); } else if(type == Buttons::LONG_PRESSED) { - buttonLongpressed[index] = true; - if(buttonLongpressed[0] && buttonLongpressed[1]) + if(index == 0) { writePin(&PORTB, LED_2_PIN, false); writePin(&PORTB, LED_1_PIN, true); @@ -100,7 +117,17 @@ void buttonHandler(uint8_t index, uint8_t type, void* data) buttonLongpressed[0] = false; buttonLongpressed[1] = false; stepper->setSpeed(lockedSpeed); - stepper->setEndlesMove(true, true); + stepper->setEndlesMove(true, direction); + } + else + { + direction = !direction; + cli(); + EEPROM_write_char(2, direction); + sei(); + blink(); + blink(); + stepper->setEndlesMove(true, direction); } } } @@ -112,26 +139,43 @@ int main() DDRD = 1 << PD0 | 1 << PD1 | 1 << PD2 | 1 << PD3; PORTD = 1 << BUTTON_1_PIN | 1 << BUTTON_2_PIN; - if(EEPROM_read_char(0) != 0) lockedSpeed = EEPROM_read_char(0); + if(EEPROM_read_char(6) != 0) + { + lockedSpeed = (uint16_t)EEPROM_read_char(5) | ((uint16_t)EEPROM_read_char(6) << 8); + direction = EEPROM_read_char(2); + } - Stepper stepper(&PORTD, PD0, PD1, PD2, PD3); + if(lockedSpeed > 0xFF00 || lockedSpeed < 0xEF00) + { + lockedSpeed = 0xFF00; + blink(true, true); + } + - debugBlink(false); - debugBlink(false); + TCCR0A = (1<(&stepper)); - uint8_t timer = 0; - while(true) { - if(++timer == 0)buttons.tick(); - stepper.tick(); - _delay_us(50); + if(comperator.compare()) lowBattery(&stepper); + buttons.tick(); + _delay_ms(2); } } diff --git a/stepper.cpp b/stepper.cpp index 34a0327..e775486 100644 --- a/stepper.cpp +++ b/stepper.cpp @@ -4,7 +4,7 @@ void Stepper::tick() { - if(++_tickCounter > UINT8_MAX - (_speed>>8) ) + if(++_tickCounter > UINT16_MAX - _speed ) { if(_targetStep != _currentStep) { @@ -13,8 +13,13 @@ void Stepper::tick() else if(endlessMove) step(endlessDriection); _tickCounter = 0; } - if(_speed>>8 < _targetSpeed) _speed = _speed + acceleration; - else if(_speed>>8 > _targetSpeed)_speed = _speed - acceleration;; + /*else if(halfCurrent) + { + if(_tickCounter % 2 == 0) disableOutputs(); + else setOutputs(); + }*/ + if(_speed < _targetSpeed) _speed = _speed + acceleration; + else if(_speed > _targetSpeed)_speed = _speed - acceleration; } void Stepper::moveTo(const int32_t step) @@ -27,7 +32,7 @@ void Stepper::moveRelative(const int16_t dist) moveTo(_targetStep + dist); } -void Stepper::setSpeed(const uint8_t speed) +void Stepper::setSpeed(const uint16_t speed) { _targetSpeed = speed; if(_targetSpeed < speedFloor) _targetSpeed = speedFloor; @@ -53,7 +58,22 @@ bool Stepper::isStoped() return _currentStep == _targetStep; } -void Stepper::step(bool direction) +void Stepper::shutdown() +{ + _currentStep = _targetStep; + _speed = 0; + disableOutputs(); +} + +void Stepper::disableOutputs() +{ + writePin(_port, _pinA, false); + writePin(_port, _pinB, false); + writePin(_port, _pinC, false); + writePin(_port, _pinD, false); +} + +void Stepper::setOutputs() { switch(_currentStep & 0x03) { @@ -87,6 +107,16 @@ void Stepper::step(bool direction) writePin(_port, _pinC, false); writePin(_port, _pinD, false); } +} + +void Stepper::setHalfCurrent( bool current) +{ + halfCurrent = current; +} + +void Stepper::step(bool direction) +{ _currentStep = _currentStep + (direction ? 1 : -1); if(endlessMove) _targetStep = _currentStep; + setOutputs(); } diff --git a/stepper.h b/stepper.h index 238fd08..c5e34c4 100644 --- a/stepper.h +++ b/stepper.h @@ -11,14 +11,15 @@ private: const uint8_t _pinC; const uint8_t _pinD; - static constexpr uint8_t acceleration = 5; - static constexpr uint8_t speedFloor = 50; + static constexpr uint8_t acceleration = 1; + static constexpr uint16_t speedFloor = 0xEF00; bool endlessMove = false; - bool endlessDriection = true; - + bool endlessDriection = true; + bool halfCurrent = false; + uint16_t _speed = speedFloor; - uint8_t _targetSpeed = 220; + uint16_t _targetSpeed = 220; uint8_t _stepInterval = 0; uint16_t _tickCounter = 0; @@ -27,16 +28,19 @@ private: int32_t _targetStep = 0; void step(bool direction); + void setOutputs(); + void disableOutputs(); - public: Stepper( volatile uint8_t * const port, const uint8_t pinA, const uint8_t pinB, const uint8_t pinC, const uint8_t pinD): _port(port), _pinA(pinA), _pinB(pinB), _pinC(pinC), _pinD(pinD){} void tick(); void moveTo(const int32_t step); void moveRelative(const int16_t dist); - void setSpeed(const uint8_t speed); + void setSpeed(const uint16_t speed); uint32_t isAt(); + void setHalfCurrent(bool current); bool isStoped(); void setEndlesMove(bool endless, bool direction = true); + void shutdown(); }; diff --git a/writepin.h b/writepin.h index 4e680bb..0cf8943 100644 --- a/writepin.h +++ b/writepin.h @@ -2,7 +2,6 @@ #define WRITEPIN_H #include - void writePin(volatile unsigned char * const port, const unsigned char pin, const bool state); void setBit( volatile unsigned char * const reg, const unsigned char bit, const bool value );