Clean code
This commit is contained in:
@@ -15,27 +15,27 @@ void SuperDebounce(uint8_t * key_state, volatile uint8_t *KPIN,
|
||||
|
||||
if( key_press && !*key_state ) {
|
||||
*key_state = debounce;
|
||||
timers[TIM_DEBOUNCE] = 15;
|
||||
tim_debounce = 15;
|
||||
} else
|
||||
if( *key_state ) {
|
||||
|
||||
if( key_press && debounce==*key_state && !timers[TIM_DEBOUNCE] ) {
|
||||
if( key_press && debounce==*key_state && !tim_debounce ) {
|
||||
*key_state = 2;
|
||||
timers[TIM_DEBOUNCE]=5;
|
||||
tim_debounce=5;
|
||||
} else
|
||||
if( !key_press && *key_state>1 && *key_state<4 ) {
|
||||
if(push_proc) push_proc(); /* KEY_UP */
|
||||
*key_state=idle;
|
||||
} else
|
||||
if( key_press && go_rep==*key_state && !timers[TIM_DEBOUNCE] ) {
|
||||
if( key_press && go_rep==*key_state && !tim_debounce ) {
|
||||
*key_state = wait_rep;
|
||||
timers[TIM_DEBOUNCE]=rep_wait;
|
||||
tim_debounce=rep_wait;
|
||||
} else
|
||||
if( key_press && wait_rep==*key_state && !timers[TIM_DEBOUNCE] ) {
|
||||
if( key_press && wait_rep==*key_state && !tim_debounce ) {
|
||||
*key_state = rep;
|
||||
} else
|
||||
if( key_press && rep==*key_state && !timers[TIM_DEBOUNCE] ) {
|
||||
timers[TIM_DEBOUNCE] = rep_time;
|
||||
if( key_press && rep==*key_state && !tim_debounce ) {
|
||||
tim_debounce = rep_time;
|
||||
if(rep_proc) rep_proc(); /* KEY_REP */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
void i2c_init(uint32_t bitrate)
|
||||
{
|
||||
uint8_t prescaler_options[] = {1, 4, 16, 64};
|
||||
uint8_t prescaler_value = prescaler_options[TWSR & 0x03]; // 0x03 (hex) = 0b00000011 (bin)
|
||||
TWBR = (uint8_t)((F_CPU / bitrate - 16) / (2 * prescaler_value));
|
||||
uint8_t prescaler_options[] = {1, 4, 16, 64};
|
||||
uint8_t prescaler_value = prescaler_options[TWSR & 0x03]; // 0x03 (hex) = 0b00000011 (bin)
|
||||
TWBR = (uint8_t)((F_CPU / bitrate - 16) / (2 * prescaler_value));
|
||||
}
|
||||
|
||||
void i2c_start(void)
|
||||
@@ -41,7 +41,9 @@ void i2c_readbuf(uint8_t sla, uint8_t adr, uint8_t len, uint8_t* buf)
|
||||
i2c_write(adr);
|
||||
i2c_start();
|
||||
i2c_write(sla + I2C_READ);
|
||||
|
||||
while(len--) *buf++ = i2c_read(len ? I2C_ACK : I2C_NACK);
|
||||
|
||||
i2c_stop();
|
||||
}
|
||||
|
||||
@@ -51,10 +53,7 @@ void i2c_writebuf(uint8_t sla, uint8_t adr, uint8_t len, uint8_t* buf)
|
||||
i2c_write(sla);
|
||||
i2c_write(adr);
|
||||
|
||||
while (len--)
|
||||
{
|
||||
i2c_write(*buf++);
|
||||
}
|
||||
while (len--) i2c_write(*buf++);
|
||||
|
||||
i2c_stop();
|
||||
}
|
||||
@@ -40,8 +40,8 @@ void keyboard_init(void)
|
||||
|
||||
void keyboard_handle_input(void)
|
||||
{
|
||||
SuperDebounce(&k_inc_hour, &KEYBOARD_PIN, KEY_INC_HOUR, 20, 500, &inc_hour, &inc_hour);
|
||||
SuperDebounce(&k_inc_minute, &KEYBOARD_PIN, KEY_INC_MINUTE, 20, 500, &inc_minute, &inc_minute);
|
||||
SuperDebounce(&k_inc_second, &KEYBOARD_PIN, KEY_INC_SECOND, 20, 500, &inc_second, &inc_second);
|
||||
SuperDebounce(&k_inc_brightness, &KEYBOARD_PIN, KEY_INC_BRIGHTNESS, 20, 500, &inc_brightness, &inc_brightness);
|
||||
SuperDebounce(&k_inc_hour, &KEYBOARD_PIN, KEY_INC_HOUR, 20, 0, &inc_hour, 0);
|
||||
SuperDebounce(&k_inc_minute, &KEYBOARD_PIN, KEY_INC_MINUTE, 20, 0, &inc_minute, 0);
|
||||
SuperDebounce(&k_inc_second, &KEYBOARD_PIN, KEY_INC_SECOND, 20, 0, &inc_second, 0);
|
||||
SuperDebounce(&k_inc_brightness, &KEYBOARD_PIN, KEY_INC_BRIGHTNESS, 20, 0, &inc_brightness, 0);
|
||||
}
|
||||
@@ -16,12 +16,12 @@ void led_init(void)
|
||||
// Clear LEDs
|
||||
ANODES_PORT = HOUR_ANODE | MINUTE_ANODE | SECOND_ANODE;
|
||||
LED_PORT |= 0x3F; // 0b00111111
|
||||
|
||||
|
||||
TCCR0 |= (1<<CS00);
|
||||
TIMSK |= (1<<TOIE0);
|
||||
}
|
||||
|
||||
// ISR(TIMER0_COMP_vect)
|
||||
// 8 MHz / 256 = 31.25 kHz
|
||||
ISR(TIMER0_OVF_vect)
|
||||
{
|
||||
static uint8_t pwm_counter = 0;
|
||||
@@ -29,21 +29,22 @@ ISR(TIMER0_OVF_vect)
|
||||
static uint8_t curr_anode = 1;
|
||||
|
||||
switch(curr_anode)
|
||||
{
|
||||
case 1:
|
||||
LED_PORT = ~led_hour;
|
||||
ANODES_PORT = ram_cfg.led_brightness >= pwm_counter ? ~HOUR_ANODE : 0xFF;
|
||||
break;
|
||||
case 2:
|
||||
LED_PORT = ~led_minute;
|
||||
ANODES_PORT = ram_cfg.led_brightness >= pwm_counter ? ~MINUTE_ANODE : 0xFF;
|
||||
break;
|
||||
case 4:
|
||||
LED_PORT = ~led_second;
|
||||
ANODES_PORT = ram_cfg.led_brightness >= pwm_counter ? ~SECOND_ANODE : 0xFF;
|
||||
break;
|
||||
}
|
||||
{
|
||||
case 1:
|
||||
LED_PORT = ~led_hour;
|
||||
ANODES_PORT = ram_cfg.led_brightness >= pwm_counter ? ~HOUR_ANODE : 0xFF;
|
||||
break;
|
||||
case 2:
|
||||
LED_PORT = ~led_minute;
|
||||
ANODES_PORT = ram_cfg.led_brightness >= pwm_counter ? ~MINUTE_ANODE : 0xFF;
|
||||
break;
|
||||
case 4:
|
||||
LED_PORT = ~led_second;
|
||||
ANODES_PORT = ram_cfg.led_brightness >= pwm_counter ? ~SECOND_ANODE : 0xFF;
|
||||
break;
|
||||
}
|
||||
|
||||
// 31.25 kHz / 250 = 125 Hz
|
||||
if(!counter)
|
||||
{
|
||||
curr_anode <<= 1;
|
||||
|
||||
@@ -32,7 +32,7 @@ int main()
|
||||
|
||||
ISR(INT0_vect)
|
||||
{
|
||||
struct time curr_time = rtc_read_time();
|
||||
struct TIME curr_time = rtc_read_time();
|
||||
led_hour = curr_time.hour;
|
||||
led_minute = curr_time.minute;
|
||||
led_second = curr_time.second;
|
||||
|
||||
@@ -2,22 +2,20 @@
|
||||
#include <avr/interrupt.h>
|
||||
#include "ptimer.h"
|
||||
|
||||
volatile uint16_t timers[TIMERS_COUNT];
|
||||
volatile uint16_t tim_debounce;
|
||||
|
||||
void ptimer_init(void)
|
||||
{
|
||||
for(uint8_t i=0; i<TIMERS_COUNT; ++i) timers[i] = 0;
|
||||
tim_debounce = 0;
|
||||
|
||||
TCCR2 |= (1<<WGM21); // CTC Mode
|
||||
TCCR2 |= (1<<CS22) | (1<<CS21) | (1<<CS20); // Prescaler 1024
|
||||
OCR2 = 78; // ~100Hz
|
||||
TIMSK |= (1<<OCIE2); // Enable CTC interruptions
|
||||
TCCR2 |= (1<<WGM21);
|
||||
TCCR2 |= (1<<CS22) | (1<<CS21) | (1<<CS20);
|
||||
OCR2 = 8000000UL/1024UL/100UL; // 8 MHz / prescaler / f (==100 Hz)
|
||||
TIMSK |= (1<<OCIE2);
|
||||
}
|
||||
|
||||
// ~100 Hz
|
||||
ISR(TIMER2_COMP_vect)
|
||||
{
|
||||
for(uint8_t i=0; i<TIMERS_COUNT; ++i)
|
||||
{
|
||||
if(timers[i]) --timers[i];
|
||||
}
|
||||
if(tim_debounce) --tim_debounce;
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
#ifndef __PTIMER_H__
|
||||
#define __PTIMER_H__
|
||||
|
||||
#define TIMERS_COUNT 1
|
||||
#define TIM_DEBOUNCE 0
|
||||
|
||||
extern volatile uint16_t timers[TIMERS_COUNT];
|
||||
extern volatile uint16_t tim_debounce;
|
||||
|
||||
void ptimer_init(void);
|
||||
|
||||
|
||||
@@ -22,12 +22,12 @@ void rtc_set_time(uint8_t part, uint8_t value)
|
||||
i2c_writebuf(RTC_I2C_ADDR, part, 1, &bcd);
|
||||
}
|
||||
|
||||
struct time rtc_read_time(void)
|
||||
struct TIME rtc_read_time(void)
|
||||
{
|
||||
uint8_t buffer[3];
|
||||
i2c_readbuf(RTC_I2C_ADDR, 0x02, 3, buffer);
|
||||
|
||||
struct time curr_time = {
|
||||
struct TIME curr_time = {
|
||||
BCD_2_DEC(buffer[2]),
|
||||
BCD_2_DEC(buffer[1]),
|
||||
BCD_2_DEC(buffer[0])
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#define DEC_2_BCD(dec) ((((dec) / 10) << 4) | ((dec) % 10))
|
||||
#define BCD_2_DEC(bcd) (((((bcd) >> 4) & 0x0F) * 10) + ((bcd) & 0x0F))
|
||||
|
||||
struct time
|
||||
struct TIME
|
||||
{
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
@@ -28,6 +28,6 @@ struct time
|
||||
void rtc_int0_init(void);
|
||||
void rtc_int1_init(void);
|
||||
void rtc_set_time(uint8_t part, uint8_t value);
|
||||
struct time rtc_read_time(void);
|
||||
struct TIME rtc_read_time(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user