Skip to content

Commit

Permalink
Huge refactoring of buttons and displaying
Browse files Browse the repository at this point in the history
  • Loading branch information
R. Kaleta committed Feb 23, 2020
1 parent f91bb88 commit d19c513
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 156 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ The infinity symbol in the displayer informs that no votes will be counted at th

### Atmega / Arduino pinouts
+ PB0 / D8 - 'yes' vote button
+ PB1 / D9 - 'no' vote button
+ PB2 / D10 - 'don't care' vote button
+ PB1 / D9 - 'don't care' vote button
+ PB2 / D10 - 'no' vote button
+ PB4 / D12 - reset button
+ PC4 / A4 - Displayer data (SDA)
+ PC5 / A5 - Displayer clock (SCL)
Expand Down
4 changes: 2 additions & 2 deletions include/buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
typedef enum
{
VotedPlus = 0,
VotedMinus = 1,
VotedEgal = 2,
VotedEgal = 1,
VotedMinus = 2,
No = 5,
Resetting = 6,
Multiple = 7
Expand Down
4 changes: 2 additions & 2 deletions include/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
typedef struct
{
number_t plus;
number_t minus;
number_t egal;
number_t minus;
} memory;

const memory * mem_get();
void mem_init();
void mem_inc_plus();
void mem_inc_minus();
void mem_inc_egal();
void mem_inc_minus();

#endif
23 changes: 23 additions & 0 deletions include/show_displayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef SHOW_DISPLAYER_H_
#define SHOW_DISPLAYER_H_

#include <stdint.h>
#include "displayer.h"
#include "showing.h"
#include "types.h"

typedef struct
{
uint8_t column;
character_t sign;
} vote_place;

extern const vote_place PlacePlus;
extern const vote_place PlaceEgal;
extern const vote_place PlaceMinus;

void show_disp_vote(vote_place place, number_t value);
void show_disp_all();
void show_disp_error();

#endif
12 changes: 12 additions & 0 deletions include/show_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef SHOW_UART_H_
#define SHOW_UART_H_

#include "showing.h"
#include "types.h"
#include "uart.h"

void show_uart_vote(character_t sign, number_t value);
void show_uart_all();
void show_uart_error();

#endif
13 changes: 13 additions & 0 deletions include/showing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef SHOWING_H_
#define SHOWING_H_

#include "types.h"

#define SIGN_PLUS '+'
#define SIGN_EGAL '?'
#define SIGN_MINUS '-'

extern const character_t SignWaiting;
extern const character_t SignError[3];

#endif
91 changes: 44 additions & 47 deletions src/buttons.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,83 @@
#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
#include "memory.h"

#define BTN_DDR DDRB
#define BTN_PORT PORTB
#define BTN_PIN PINB

#define PLUS_PIN PB0
#define MINUS_PIN PB1
#define EGAL_PIN PB2
#define RESET_PIN PB4
#define RESET_DDR DDRD
#define RESET_PORT PORTD
#define RESET_PIN PIND

const uint8_t Mask = (1 << PLUS_PIN) | (1 << MINUS_PIN) | (1 << EGAL_PIN) | (1 << RESET_PIN);
#define PLUS_PINOUT PB0
#define EGAL_PINOUT PB1
#define MINUS_PINOUT PB2
#define RESET_PINOUT PD7

pressed btn_release_(uint8_t button_pin)
const uint8_t VotesMask = (1 << PLUS_PINOUT) | (1 << EGAL_PINOUT) | (1 << MINUS_PINOUT);
const uint8_t ResetMask = (1 << RESET_PINOUT);

pressed btn_handle_(uint8_t button_pin)
{
uint8_t was_multiple = 0;

while(~BTN_PIN & VotesMask)
{
if((~BTN_PIN & VotesMask) != (1 << button_pin))
return Multiple;

_delay_us(200);
}

switch(button_pin)
{
case PLUS_PIN:
mem_inc_plus();
case PLUS_PINOUT:
return VotedPlus;

case MINUS_PIN:
mem_inc_minus();
return VotedMinus;

case EGAL_PIN:
mem_inc_egal();
case EGAL_PINOUT:
return VotedEgal;

case RESET_PIN:
mem_init();
return Resetting;
case MINUS_PINOUT:
return VotedMinus;

default:
return Multiple;
}
}

pressed btn_handle_(uint8_t button_pin)
void btn_init()
{
uint8_t was_multiple = 0;
/*
* set DDR on button pins to input
* set PORT on buttons pins to pullup
*/

while(~BTN_PIN & Mask)
{
if((~BTN_PIN & Mask) != (1 << button_pin))
{
was_multiple = 1;
break;
}

_delay_us(200);
}
BTN_DDR &= ~VotesMask;
BTN_PORT |= VotesMask;

return was_multiple ? Multiple : btn_release_(button_pin);
}

void btn_init()
{
BTN_DDR &= ~Mask; // set DDR on button pins to input
BTN_PORT |= Mask; // set PORT on buttons pins to pullup
RESET_DDR &= ~ResetMask;
RESET_PORT |= ResetMask;
}

pressed btn_click()
{
switch(~BTN_PIN & Mask)
if(RESET_PIN & ResetMask)
return Resetting;

switch(~BTN_PIN & VotesMask)
{
case 0:
return No;

case 1 << PLUS_PIN:
return btn_handle_(PLUS_PIN);

case 1 << MINUS_PIN:
return btn_handle_(MINUS_PIN);
case 1 << PLUS_PINOUT:
return btn_handle_(PLUS_PINOUT);

case 1 << EGAL_PIN:
return btn_handle_(EGAL_PIN);
case 1 << EGAL_PINOUT:
return btn_handle_(EGAL_PINOUT);

case 1 << RESET_PIN:
return btn_handle_(RESET_PIN);
case 1 << MINUS_PINOUT:
return btn_handle_(MINUS_PINOUT);

default:
return Multiple;
Expand Down
128 changes: 31 additions & 97 deletions src/lecture_voting.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,90 +3,10 @@
#include "buttons.h"
#include "displayer.h"
#include "memory.h"
#include "show_displayer.h"
#include "types.h"
#include "uart.h"

const character_t ColPlus = 0;
const character_t ColMinus = 5;
const character_t ColEgal = 10;
const character_t SignPlus = '+';
const character_t SignMinus = '-';
const character_t SignEgal = '?';

void show_vote_disp(uint8_t col, character_t sign, number_t value)
{
disp_move(0, col);
disp_write_char(sign);
disp_write_dec(value);

if(value == 0)
{
disp_write_char(' ');
disp_write_char(' ');
}
}

void show_vote_uart(character_t sign, number_t value)
{
uart_write(sign);
uart_write_dec(value);

if(value < 100)
uart_write(' ');

if(value < 10)
uart_write(' ');
}

void show_all_votes_uart()
{
const memory * m = mem_get();

show_vote_uart(SignPlus, m->plus);
uart_write(' ');
show_vote_uart(SignMinus, m->minus);
uart_write(' ');
show_vote_uart(SignEgal, m->egal);
uart_write('\r');
}

void error_multiple()
{
disp_move(1, 0);
disp_write_char('_');
disp_write_char('|');
disp_write_char('_');
uart_write('\r');
uart_write('\n');
uart_write('_');
uart_write('|');
uart_write('_');
uart_write('\r');
uart_write('\n');
}

void show_all()
{
const memory * m = mem_get();

disp_move(1, 0);
disp_write_char(0xF3);
show_vote_disp(ColPlus, SignPlus, m->plus);
show_vote_disp(ColMinus, SignMinus, m->minus);
show_vote_disp(ColEgal, SignEgal, m->egal);
show_all_votes_uart();
disp_clear_line(1, 1);
}

void add_vote(uint8_t col, character_t sign, number_t value)
{
disp_move(1, 0);
disp_write_char(0xF3);
show_vote_disp(col, sign, value);
show_all_votes_uart();
disp_clear_line(1, 1);
}

int main()
{
uint8_t was_error = 0;
Expand All @@ -95,49 +15,63 @@ int main()
uart_init();
btn_init();
disp_init();
show_all();
show_disp_all();

while(1)
{
pressed p = btn_click();
switch(btn_click())
{
case VotedPlus:
if(!was_error)
{
mem_inc_plus();
show_disp_vote(PlacePlus, mem_get()->plus);
}

if(was_error)
disp_clear_line(1, 3);
break;

was_error = 0;
case VotedEgal:
if(!was_error)
{
mem_inc_egal();
show_disp_vote(PlaceEgal, mem_get()->egal);
}

switch(p)
{
case VotedPlus:
add_vote(ColPlus, SignPlus, mem_get()->plus);
break;

case VotedMinus:
add_vote(ColMinus, SignMinus, mem_get()->minus);
break;
if(!was_error)
{
mem_inc_minus();
show_disp_vote(PlaceMinus, mem_get()->minus);
}

case VotedEgal:
add_vote(ColEgal, SignEgal, mem_get()->egal);
break;

case No:
if(was_error)
disp_clear_line(1, 3);

was_error = 0;
break;

case Resetting:
was_error = 0;
mem_init();
disp_clear_all();
show_all();
show_disp_all();
break;

case Multiple:
was_error = 1;
error_multiple();
show_disp_error();
break;

default:
break;
}

_delay_us(500);
_delay_ms(50);
}

disp_end();
Expand Down
Loading

0 comments on commit d19c513

Please sign in to comment.