update button handling
This commit is contained in:
@ -8,8 +8,10 @@ class Buttons
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
static constexpr uint8_t PRESSED = 0;
|
static constexpr uint8_t PRESSED = 0;
|
||||||
static constexpr uint8_t LONG_PRESSED = 1;
|
|
||||||
static constexpr uint8_t RELEASED = 2;
|
static constexpr uint8_t RELEASED = 2;
|
||||||
|
static constexpr uint8_t LONG_PRESSED = 1;
|
||||||
|
static constexpr uint8_t LONG_RELEASED= 3;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
volatile uint8_t * const pinReg = &PIND;
|
volatile uint8_t * const pinReg = &PIND;
|
||||||
@ -31,7 +33,8 @@ void Buttons::tick()
|
|||||||
{
|
{
|
||||||
if(readPin(pinReg, button[i]) == true)
|
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;
|
buttonCount[i] = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
1
fusebits
Normal file
1
fusebits
Normal file
@ -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
|
118
main.cpp
118
main.cpp
@ -9,24 +9,42 @@
|
|||||||
#include "buttons.h"
|
#include "buttons.h"
|
||||||
#include "eeprom.h"
|
#include "eeprom.h"
|
||||||
|
|
||||||
#define LED_1_PIN PB3
|
#define LED_2_PIN PB3
|
||||||
#define LED_2_PIN PB4
|
#define LED_1_PIN PB4
|
||||||
|
|
||||||
#define BUTTON_1_PIN PD5
|
#define BUTTON_1_PIN PD5
|
||||||
#define BUTTON_2_PIN PD6
|
#define BUTTON_2_PIN PD6
|
||||||
|
|
||||||
bool setup = false;
|
bool setup = false;
|
||||||
|
bool direction = false;
|
||||||
static bool buttonLongpressed[2];
|
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);
|
_delay_ms(100);
|
||||||
if(!fast) _delay_ms(200);
|
if(!fast) _delay_ms(200);
|
||||||
PORTB |= 1 << LED_1_PIN;
|
PORTB |= 1 << red ? LED_2_PIN : LED_1_PIN;
|
||||||
_delay_ms(100);
|
_delay_ms(100);
|
||||||
if(!fast) _delay_ms(200);
|
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)
|
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)
|
if(type == Buttons::PRESSED)
|
||||||
{
|
{
|
||||||
|
stepper->setHalfCurrent(false);
|
||||||
if(index == 0)
|
if(index == 0)
|
||||||
{
|
{
|
||||||
stepper->setSpeed(230);
|
stepper->setSpeed(0xFFEA);
|
||||||
stepper->setEndlesMove(true, true);
|
stepper->setEndlesMove(true, true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
stepper->setSpeed(230);
|
stepper->setSpeed(0xFFEA);
|
||||||
stepper->setEndlesMove(true, false);
|
stepper->setEndlesMove(true, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(type == Buttons::RELEASED)
|
else if(type == Buttons::RELEASED || type == Buttons::LONG_RELEASED)
|
||||||
{
|
{
|
||||||
stepper->setSpeed(lockedSpeed);
|
stepper->setSpeed(lockedSpeed);
|
||||||
stepper->setEndlesMove(true, true);
|
stepper->setEndlesMove(true, direction);
|
||||||
|
stepper->setHalfCurrent(true);
|
||||||
buttonLongpressed[0] = false;
|
buttonLongpressed[0] = false;
|
||||||
buttonLongpressed[1] = false;
|
buttonLongpressed[1] = false;
|
||||||
}
|
}
|
||||||
@ -59,16 +79,17 @@ void buttonHandler(uint8_t index, uint8_t type, void* data)
|
|||||||
buttonLongpressed[index] = true;
|
buttonLongpressed[index] = true;
|
||||||
if(buttonLongpressed[0] && buttonLongpressed[1])
|
if(buttonLongpressed[0] && buttonLongpressed[1])
|
||||||
{
|
{
|
||||||
|
stepper->setHalfCurrent(false);
|
||||||
writePin(&PORTB, LED_2_PIN, true);
|
writePin(&PORTB, LED_2_PIN, true);
|
||||||
writePin(&PORTB, LED_1_PIN, false);
|
writePin(&PORTB, LED_1_PIN, false);
|
||||||
setup = true;
|
setup = true;
|
||||||
buttonLongpressed[0] = false;
|
buttonLongpressed[0] = false;
|
||||||
buttonLongpressed[1] = false;
|
buttonLongpressed[1] = false;
|
||||||
stepper->setSpeed(lockedSpeed);
|
stepper->setSpeed(lockedSpeed);
|
||||||
stepper->setEndlesMove(true, true);
|
stepper->setEndlesMove(true, direction);
|
||||||
debugBlink();
|
blink();
|
||||||
debugBlink();
|
blink();
|
||||||
debugBlink();
|
blink();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,21 +99,17 @@ void buttonHandler(uint8_t index, uint8_t type, void* data)
|
|||||||
if(type == Buttons::RELEASED)
|
if(type == Buttons::RELEASED)
|
||||||
{
|
{
|
||||||
writePin(&PORTB, LED_1_PIN, false);
|
writePin(&PORTB, LED_1_PIN, false);
|
||||||
if(index == 0)
|
if(index == 0 && lockedSpeed < 0xFF00) lockedSpeed+=80;
|
||||||
{
|
else if(lockedSpeed > 0xEF00) lockedSpeed-=80;
|
||||||
lockedSpeed+=5;
|
cli();
|
||||||
}
|
EEPROM_write_char(6, lockedSpeed>>8);
|
||||||
else
|
EEPROM_write_char(5, lockedSpeed & 0x00FF);
|
||||||
{
|
sei();
|
||||||
lockedSpeed-=5;
|
|
||||||
}
|
|
||||||
EEPROM_write_char(0, lockedSpeed);
|
|
||||||
stepper->setSpeed(lockedSpeed);
|
stepper->setSpeed(lockedSpeed);
|
||||||
}
|
}
|
||||||
else if(type == Buttons::LONG_PRESSED)
|
else if(type == Buttons::LONG_PRESSED)
|
||||||
{
|
{
|
||||||
buttonLongpressed[index] = true;
|
if(index == 0)
|
||||||
if(buttonLongpressed[0] && buttonLongpressed[1])
|
|
||||||
{
|
{
|
||||||
writePin(&PORTB, LED_2_PIN, false);
|
writePin(&PORTB, LED_2_PIN, false);
|
||||||
writePin(&PORTB, LED_1_PIN, true);
|
writePin(&PORTB, LED_1_PIN, true);
|
||||||
@ -100,7 +117,17 @@ void buttonHandler(uint8_t index, uint8_t type, void* data)
|
|||||||
buttonLongpressed[0] = false;
|
buttonLongpressed[0] = false;
|
||||||
buttonLongpressed[1] = false;
|
buttonLongpressed[1] = false;
|
||||||
stepper->setSpeed(lockedSpeed);
|
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;
|
DDRD = 1 << PD0 | 1 << PD1 | 1 << PD2 | 1 << PD3;
|
||||||
PORTD = 1 << BUTTON_1_PIN | 1 << BUTTON_2_PIN;
|
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);
|
TCCR0A = (1<<WGM01); //CTC timer mode
|
||||||
debugBlink(false);
|
TIMSK = (1<<OCIE0A); //OCIE0A compeare interrupt enable
|
||||||
|
OCR0A = 255;
|
||||||
|
TCCR0B = (1<<CS00); //start with no prescaleing
|
||||||
|
sei();
|
||||||
|
|
||||||
|
blink(false);
|
||||||
|
blink(false);
|
||||||
|
|
||||||
writePin(&PORTB, LED_1_PIN, true);
|
writePin(&PORTB, LED_1_PIN, true);
|
||||||
stepper.setSpeed(180);
|
stepper.setSpeed(lockedSpeed);
|
||||||
stepper.setEndlesMove(true);
|
stepper.setEndlesMove(true, direction);
|
||||||
|
stepper.setHalfCurrent(true);
|
||||||
|
|
||||||
|
Comperator comperator;
|
||||||
|
comperator.on();
|
||||||
|
|
||||||
Buttons buttons(&buttonHandler, reinterpret_cast<void*>(&stepper));
|
Buttons buttons(&buttonHandler, reinterpret_cast<void*>(&stepper));
|
||||||
|
|
||||||
uint8_t timer = 0;
|
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
if(++timer == 0)buttons.tick();
|
if(comperator.compare()) lowBattery(&stepper);
|
||||||
stepper.tick();
|
buttons.tick();
|
||||||
_delay_us(50);
|
_delay_ms(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
40
stepper.cpp
40
stepper.cpp
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
void Stepper::tick()
|
void Stepper::tick()
|
||||||
{
|
{
|
||||||
if(++_tickCounter > UINT8_MAX - (_speed>>8) )
|
if(++_tickCounter > UINT16_MAX - _speed )
|
||||||
{
|
{
|
||||||
if(_targetStep != _currentStep)
|
if(_targetStep != _currentStep)
|
||||||
{
|
{
|
||||||
@ -13,8 +13,13 @@ void Stepper::tick()
|
|||||||
else if(endlessMove) step(endlessDriection);
|
else if(endlessMove) step(endlessDriection);
|
||||||
_tickCounter = 0;
|
_tickCounter = 0;
|
||||||
}
|
}
|
||||||
if(_speed>>8 < _targetSpeed) _speed = _speed + acceleration;
|
/*else if(halfCurrent)
|
||||||
else if(_speed>>8 > _targetSpeed)_speed = _speed - acceleration;;
|
{
|
||||||
|
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)
|
void Stepper::moveTo(const int32_t step)
|
||||||
@ -27,7 +32,7 @@ void Stepper::moveRelative(const int16_t dist)
|
|||||||
moveTo(_targetStep + dist);
|
moveTo(_targetStep + dist);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Stepper::setSpeed(const uint8_t speed)
|
void Stepper::setSpeed(const uint16_t speed)
|
||||||
{
|
{
|
||||||
_targetSpeed = speed;
|
_targetSpeed = speed;
|
||||||
if(_targetSpeed < speedFloor) _targetSpeed = speedFloor;
|
if(_targetSpeed < speedFloor) _targetSpeed = speedFloor;
|
||||||
@ -53,7 +58,22 @@ bool Stepper::isStoped()
|
|||||||
return _currentStep == _targetStep;
|
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)
|
switch(_currentStep & 0x03)
|
||||||
{
|
{
|
||||||
@ -87,6 +107,16 @@ void Stepper::step(bool direction)
|
|||||||
writePin(_port, _pinC, false);
|
writePin(_port, _pinC, false);
|
||||||
writePin(_port, _pinD, false);
|
writePin(_port, _pinD, false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Stepper::setHalfCurrent( bool current)
|
||||||
|
{
|
||||||
|
halfCurrent = current;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Stepper::step(bool direction)
|
||||||
|
{
|
||||||
_currentStep = _currentStep + (direction ? 1 : -1);
|
_currentStep = _currentStep + (direction ? 1 : -1);
|
||||||
if(endlessMove) _targetStep = _currentStep;
|
if(endlessMove) _targetStep = _currentStep;
|
||||||
|
setOutputs();
|
||||||
}
|
}
|
||||||
|
18
stepper.h
18
stepper.h
@ -11,14 +11,15 @@ private:
|
|||||||
const uint8_t _pinC;
|
const uint8_t _pinC;
|
||||||
const uint8_t _pinD;
|
const uint8_t _pinD;
|
||||||
|
|
||||||
static constexpr uint8_t acceleration = 5;
|
static constexpr uint8_t acceleration = 1;
|
||||||
static constexpr uint8_t speedFloor = 50;
|
static constexpr uint16_t speedFloor = 0xEF00;
|
||||||
|
|
||||||
bool endlessMove = false;
|
bool endlessMove = false;
|
||||||
bool endlessDriection = true;
|
bool endlessDriection = true;
|
||||||
|
bool halfCurrent = false;
|
||||||
|
|
||||||
uint16_t _speed = speedFloor;
|
uint16_t _speed = speedFloor;
|
||||||
uint8_t _targetSpeed = 220;
|
uint16_t _targetSpeed = 220;
|
||||||
uint8_t _stepInterval = 0;
|
uint8_t _stepInterval = 0;
|
||||||
|
|
||||||
uint16_t _tickCounter = 0;
|
uint16_t _tickCounter = 0;
|
||||||
@ -27,16 +28,19 @@ private:
|
|||||||
int32_t _targetStep = 0;
|
int32_t _targetStep = 0;
|
||||||
|
|
||||||
void step(bool direction);
|
void step(bool direction);
|
||||||
|
void setOutputs();
|
||||||
|
void disableOutputs();
|
||||||
|
|
||||||
|
|
||||||
public:
|
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){}
|
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 tick();
|
||||||
void moveTo(const int32_t step);
|
void moveTo(const int32_t step);
|
||||||
void moveRelative(const int16_t dist);
|
void moveRelative(const int16_t dist);
|
||||||
void setSpeed(const uint8_t speed);
|
void setSpeed(const uint16_t speed);
|
||||||
uint32_t isAt();
|
uint32_t isAt();
|
||||||
|
void setHalfCurrent(bool current);
|
||||||
bool isStoped();
|
bool isStoped();
|
||||||
void setEndlesMove(bool endless, bool direction = true);
|
void setEndlesMove(bool endless, bool direction = true);
|
||||||
|
void shutdown();
|
||||||
};
|
};
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
#define WRITEPIN_H
|
#define WRITEPIN_H
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
|
|
||||||
|
|
||||||
void writePin(volatile unsigned char * const port, const unsigned char pin, const bool state);
|
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 );
|
void setBit( volatile unsigned char * const reg, const unsigned char bit, const bool value );
|
||||||
|
Reference in New Issue
Block a user