Clean code

This commit is contained in:
2020-11-18 08:48:20 +01:00
parent 26eac4ac69
commit b9c0be68d8
9 changed files with 49 additions and 54 deletions

View File

@@ -15,27 +15,27 @@ void SuperDebounce(uint8_t * key_state, volatile uint8_t *KPIN,
if( key_press && !*key_state ) { if( key_press && !*key_state ) {
*key_state = debounce; *key_state = debounce;
timers[TIM_DEBOUNCE] = 15; tim_debounce = 15;
} else } else
if( *key_state ) { if( *key_state ) {
if( key_press && debounce==*key_state && !timers[TIM_DEBOUNCE] ) { if( key_press && debounce==*key_state && !tim_debounce ) {
*key_state = 2; *key_state = 2;
timers[TIM_DEBOUNCE]=5; tim_debounce=5;
} else } else
if( !key_press && *key_state>1 && *key_state<4 ) { if( !key_press && *key_state>1 && *key_state<4 ) {
if(push_proc) push_proc(); /* KEY_UP */ if(push_proc) push_proc(); /* KEY_UP */
*key_state=idle; *key_state=idle;
} else } else
if( key_press && go_rep==*key_state && !timers[TIM_DEBOUNCE] ) { if( key_press && go_rep==*key_state && !tim_debounce ) {
*key_state = wait_rep; *key_state = wait_rep;
timers[TIM_DEBOUNCE]=rep_wait; tim_debounce=rep_wait;
} else } else
if( key_press && wait_rep==*key_state && !timers[TIM_DEBOUNCE] ) { if( key_press && wait_rep==*key_state && !tim_debounce ) {
*key_state = rep; *key_state = rep;
} else } else
if( key_press && rep==*key_state && !timers[TIM_DEBOUNCE] ) { if( key_press && rep==*key_state && !tim_debounce ) {
timers[TIM_DEBOUNCE] = rep_time; tim_debounce = rep_time;
if(rep_proc) rep_proc(); /* KEY_REP */ if(rep_proc) rep_proc(); /* KEY_REP */
} }
} }

View File

@@ -41,7 +41,9 @@ void i2c_readbuf(uint8_t sla, uint8_t adr, uint8_t len, uint8_t* buf)
i2c_write(adr); i2c_write(adr);
i2c_start(); i2c_start();
i2c_write(sla + I2C_READ); i2c_write(sla + I2C_READ);
while(len--) *buf++ = i2c_read(len ? I2C_ACK : I2C_NACK); while(len--) *buf++ = i2c_read(len ? I2C_ACK : I2C_NACK);
i2c_stop(); 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(sla);
i2c_write(adr); i2c_write(adr);
while (len--) while (len--) i2c_write(*buf++);
{
i2c_write(*buf++);
}
i2c_stop(); i2c_stop();
} }

View File

@@ -40,8 +40,8 @@ void keyboard_init(void)
void keyboard_handle_input(void) void keyboard_handle_input(void)
{ {
SuperDebounce(&k_inc_hour, &KEYBOARD_PIN, KEY_INC_HOUR, 20, 500, &inc_hour, &inc_hour); SuperDebounce(&k_inc_hour, &KEYBOARD_PIN, KEY_INC_HOUR, 20, 0, &inc_hour, 0);
SuperDebounce(&k_inc_minute, &KEYBOARD_PIN, KEY_INC_MINUTE, 20, 500, &inc_minute, &inc_minute); SuperDebounce(&k_inc_minute, &KEYBOARD_PIN, KEY_INC_MINUTE, 20, 0, &inc_minute, 0);
SuperDebounce(&k_inc_second, &KEYBOARD_PIN, KEY_INC_SECOND, 20, 500, &inc_second, &inc_second); SuperDebounce(&k_inc_second, &KEYBOARD_PIN, KEY_INC_SECOND, 20, 0, &inc_second, 0);
SuperDebounce(&k_inc_brightness, &KEYBOARD_PIN, KEY_INC_BRIGHTNESS, 20, 500, &inc_brightness, &inc_brightness); SuperDebounce(&k_inc_brightness, &KEYBOARD_PIN, KEY_INC_BRIGHTNESS, 20, 0, &inc_brightness, 0);
} }

View File

@@ -21,7 +21,7 @@ void led_init(void)
TIMSK |= (1<<TOIE0); TIMSK |= (1<<TOIE0);
} }
// ISR(TIMER0_COMP_vect) // 8 MHz / 256 = 31.25 kHz
ISR(TIMER0_OVF_vect) ISR(TIMER0_OVF_vect)
{ {
static uint8_t pwm_counter = 0; static uint8_t pwm_counter = 0;
@@ -44,6 +44,7 @@ ISR(TIMER0_OVF_vect)
break; break;
} }
// 31.25 kHz / 250 = 125 Hz
if(!counter) if(!counter)
{ {
curr_anode <<= 1; curr_anode <<= 1;

View File

@@ -32,7 +32,7 @@ int main()
ISR(INT0_vect) ISR(INT0_vect)
{ {
struct time curr_time = rtc_read_time(); struct TIME curr_time = rtc_read_time();
led_hour = curr_time.hour; led_hour = curr_time.hour;
led_minute = curr_time.minute; led_minute = curr_time.minute;
led_second = curr_time.second; led_second = curr_time.second;

View File

@@ -2,22 +2,20 @@
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "ptimer.h" #include "ptimer.h"
volatile uint16_t timers[TIMERS_COUNT]; volatile uint16_t tim_debounce;
void ptimer_init(void) 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<<WGM21);
TCCR2 |= (1<<CS22) | (1<<CS21) | (1<<CS20); // Prescaler 1024 TCCR2 |= (1<<CS22) | (1<<CS21) | (1<<CS20);
OCR2 = 78; // ~100Hz OCR2 = 8000000UL/1024UL/100UL; // 8 MHz / prescaler / f (==100 Hz)
TIMSK |= (1<<OCIE2); // Enable CTC interruptions TIMSK |= (1<<OCIE2);
} }
// ~100 Hz
ISR(TIMER2_COMP_vect) ISR(TIMER2_COMP_vect)
{ {
for(uint8_t i=0; i<TIMERS_COUNT; ++i) if(tim_debounce) --tim_debounce;
{
if(timers[i]) --timers[i];
}
} }

View File

@@ -1,10 +1,7 @@
#ifndef __PTIMER_H__ #ifndef __PTIMER_H__
#define __PTIMER_H__ #define __PTIMER_H__
#define TIMERS_COUNT 1 extern volatile uint16_t tim_debounce;
#define TIM_DEBOUNCE 0
extern volatile uint16_t timers[TIMERS_COUNT];
void ptimer_init(void); void ptimer_init(void);

View File

@@ -22,12 +22,12 @@ void rtc_set_time(uint8_t part, uint8_t value)
i2c_writebuf(RTC_I2C_ADDR, part, 1, &bcd); i2c_writebuf(RTC_I2C_ADDR, part, 1, &bcd);
} }
struct time rtc_read_time(void) struct TIME rtc_read_time(void)
{ {
uint8_t buffer[3]; uint8_t buffer[3];
i2c_readbuf(RTC_I2C_ADDR, 0x02, 3, buffer); 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[2]),
BCD_2_DEC(buffer[1]), BCD_2_DEC(buffer[1]),
BCD_2_DEC(buffer[0]) BCD_2_DEC(buffer[0])

View File

@@ -18,7 +18,7 @@
#define DEC_2_BCD(dec) ((((dec) / 10) << 4) | ((dec) % 10)) #define DEC_2_BCD(dec) ((((dec) / 10) << 4) | ((dec) % 10))
#define BCD_2_DEC(bcd) (((((bcd) >> 4) & 0x0F) * 10) + ((bcd) & 0x0F)) #define BCD_2_DEC(bcd) (((((bcd) >> 4) & 0x0F) * 10) + ((bcd) & 0x0F))
struct time struct TIME
{ {
uint8_t hour; uint8_t hour;
uint8_t minute; uint8_t minute;
@@ -28,6 +28,6 @@ struct time
void rtc_int0_init(void); void rtc_int0_init(void);
void rtc_int1_init(void); void rtc_int1_init(void);
void rtc_set_time(uint8_t part, uint8_t value); void rtc_set_time(uint8_t part, uint8_t value);
struct time rtc_read_time(void); struct TIME rtc_read_time(void);
#endif #endif