138 lines
2.8 KiB
C++
138 lines
2.8 KiB
C++
#include <stdint.h>
|
|
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
#include <avr/interrupt.h>
|
|
#include <avr/sleep.h>
|
|
#include "stepper.h"
|
|
#include "writepin.h"
|
|
#include "comperator.h"
|
|
#include "buttons.h"
|
|
#include "eeprom.h"
|
|
|
|
#define LED_1_PIN PB3
|
|
#define LED_2_PIN PB4
|
|
|
|
#define BUTTON_1_PIN PD5
|
|
#define BUTTON_2_PIN PD6
|
|
|
|
bool setup = false;
|
|
static bool buttonLongpressed[2];
|
|
uint8_t lockedSpeed = 180;
|
|
|
|
void debugBlink(bool fast = true)
|
|
{
|
|
_delay_ms(100);
|
|
if(!fast) _delay_ms(200);
|
|
PORTB |= 1 << LED_1_PIN;
|
|
_delay_ms(100);
|
|
if(!fast) _delay_ms(200);
|
|
PORTB &= ~(1 << LED_1_PIN);
|
|
}
|
|
|
|
void buttonHandler(uint8_t index, uint8_t type, void* data)
|
|
{
|
|
Stepper* stepper = reinterpret_cast<Stepper*>(data);
|
|
if(setup == false)
|
|
{
|
|
if(type == Buttons::PRESSED)
|
|
{
|
|
if(index == 0)
|
|
{
|
|
stepper->setSpeed(230);
|
|
stepper->setEndlesMove(true, true);
|
|
}
|
|
else
|
|
{
|
|
stepper->setSpeed(230);
|
|
stepper->setEndlesMove(true, false);
|
|
}
|
|
}
|
|
else if(type == Buttons::RELEASED)
|
|
{
|
|
stepper->setSpeed(lockedSpeed);
|
|
stepper->setEndlesMove(true, true);
|
|
buttonLongpressed[0] = false;
|
|
buttonLongpressed[1] = false;
|
|
}
|
|
else if(type == Buttons::LONG_PRESSED)
|
|
{
|
|
buttonLongpressed[index] = true;
|
|
if(buttonLongpressed[0] && buttonLongpressed[1])
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(type == Buttons::PRESSED) writePin(&PORTB, LED_1_PIN, true);
|
|
if(type == Buttons::RELEASED)
|
|
{
|
|
writePin(&PORTB, LED_1_PIN, false);
|
|
if(index == 0)
|
|
{
|
|
lockedSpeed+=5;
|
|
}
|
|
else
|
|
{
|
|
lockedSpeed-=5;
|
|
}
|
|
EEPROM_write_char(0, lockedSpeed);
|
|
stepper->setSpeed(lockedSpeed);
|
|
}
|
|
else if(type == Buttons::LONG_PRESSED)
|
|
{
|
|
buttonLongpressed[index] = true;
|
|
if(buttonLongpressed[0] && buttonLongpressed[1])
|
|
{
|
|
writePin(&PORTB, LED_2_PIN, false);
|
|
writePin(&PORTB, LED_1_PIN, true);
|
|
setup = false;
|
|
buttonLongpressed[0] = false;
|
|
buttonLongpressed[1] = false;
|
|
stepper->setSpeed(lockedSpeed);
|
|
stepper->setEndlesMove(true, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
DDRB = 1 << LED_1_PIN | 1 << LED_2_PIN;
|
|
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);
|
|
|
|
Stepper stepper(&PORTD, PD0, PD1, PD2, PD3);
|
|
|
|
debugBlink(false);
|
|
debugBlink(false);
|
|
|
|
writePin(&PORTB, LED_1_PIN, true);
|
|
stepper.setSpeed(180);
|
|
stepper.setEndlesMove(true);
|
|
|
|
Buttons buttons(&buttonHandler, reinterpret_cast<void*>(&stepper));
|
|
|
|
uint8_t timer = 0;
|
|
|
|
while(true)
|
|
{
|
|
if(++timer == 0)buttons.tick();
|
|
stepper.tick();
|
|
_delay_us(50);
|
|
}
|
|
}
|
|
|