C code style

* renamed methods and fields using camelCase notation
* merged nested if-statements
* removed unused globals
This commit is contained in:
Stefan Kalscheuer 2018-09-08 14:00:55 +02:00
parent 18b48bcff3
commit 61c131486b
3 changed files with 284 additions and 238 deletions

View File

@ -1,8 +1,14 @@
// midifs.c /**
// Typ: C-Sourcerfile * MIDI Footswitch
// Project: MIDI Footswitch *
// Date: 17.06.2014 * @file midifs.c
// Author: ska * @author Stefan Kalscheuer
* @date 2014-06-17
* @brief Main program
*
* Platform: ATtiny2313
* Internal RC-oscillator 8 MHz
*/
#define F_CPU 8000000UL #define F_CPU 8000000UL
@ -12,185 +18,213 @@
#include "midifs.h" #include "midifs.h"
#include "usart.h" #include "usart.h"
volatile unsigned char status_channel, status, channel, data1, data2; // Counter for debouncing inputs.
volatile unsigned int iBtn1_counter = 0, iBtn2_counter = 0, iBtn3_counter = 0, iBtn4_counter = 0, iSw1_counter = 0, iSw2_counter = 0; volatile uint16_t counterBtn1 = 0;
volatile unsigned char cBtn1 = 0, cBtn2 = 0, cBtn3 = 0, cBtn4 = 0, cSw1 = 0, cSw2 = 0; volatile uint16_t counterBtn2 = 0;
volatile unsigned char cChannel = 0, cBank = 0; cTuner = 0; volatile uint16_t counterBtn3 = 0;
unsigned int cnt = 0; volatile uint16_t counterBtn4 = 0;
volatile uint16_t counterSw1 = 0;
volatile uint16_t counterSw2 = 0;
int main (void) // Input states.
{ volatile uint8_t stateBtn1 = 0;
DDRB = 0x00; // PB1..7 as inputs volatile uint8_t stateBtn2 = 0;
PORTB = 0xFF; // interlan pull-ups for PB0..7y volatile uint8_t stateBtn3 = 0;
DDRD = 0x30; // PD4..5 as outputs volatile uint8_t stateBtn4 = 0;
PORTD = 0x0C; // internal pull-ups for PD2..3 volatile uint8_t stateSw1 = 0;
volatile uint8_t stateSw2 = 0;
OUT_LED = 0x30; // LEDs off // Channel, bank and tuner states.
volatile uint8_t currentChannel = 0;
volatile uint8_t currentBank = 0;
volatile uint8_t currentTuner = 0;
// Timer1 init // Global interrupt counter.
TCCR1A |= (1 << WGM01); // set timer 0 to CTC-Mode uint16_t cnt = 0;
TCCR1B &= ~(1 << CS10); // prescaler 8 (1MHz)
TCCR1B |= (1 << CS11);
TCCR1B &= ~(1 << CS12);
OCR1A = 499; // period of 1 ms
cli(); // disable interrupts /**
TIMSK |= (1 << TOIE1); // activate timer 1 * Switch to given bank.
sei(); // enable interrupts *
* @param bank Bank number (supported 1 and 2).
//USART_Init(38400); // initialize USART with 38400 baud (debug) * @return void
USART_Init(31250); // initialize USART with 31250 baud */
void setBank(uint8_t bank) {
while (1) { if (bank == 0) {
if (iBtn1_counter > BTN_THRESHOLD && cBtn1 == 0) // Button 1 USART_Transmit(0xB0); // Bank 1
{ USART_Transmit(0x00);
Bank(cBank); USART_Transmit(0x00);
USART_Transmit(0xC0); // Channel 1 USART_Transmit(0xC0);
USART_Transmit(0x00); USART_Transmit(0x00);
USART_Transmit(currentChannel);
cChannel = 0; OUT_LED &= ~(1 << LED_RT); // LED green
cTuner = 0; OUT_LED |= (1 << LED_GN);
} else {
cBtn1 = 1; USART_Transmit(0xB0); // Bank 2
} USART_Transmit(0x00);
else if (iBtn1_counter < BTN_THRESHOLD && cBtn1 == 1) USART_Transmit(0x01);
cBtn1 = 0; USART_Transmit(0xC0);
USART_Transmit(0x00);
if(iBtn2_counter > BTN_THRESHOLD && cBtn2 == 0) // Button 2 USART_Transmit(currentChannel);
{ OUT_LED &= ~(1 << LED_GN); // LED green
Bank(cBank); OUT_LED |= (1 << LED_RT);
USART_Transmit(0xC0); // Channel 2 }
USART_Transmit(0x01);
cChannel = 1;
cTuner = 0;
cBtn2 = 1;
}
else if (iBtn2_counter < BTN_THRESHOLD && cBtn2 == 1)
cBtn2 = 0;
if(iBtn3_counter > BTN_THRESHOLD && cBtn3 == 0) // Button 3
{
Bank(cBank);
USART_Transmit(0xC0); // Channel 3
USART_Transmit(0x02);
cChannel = 2;
cTuner = 0;
cBtn3 = 1;
}
else if (iBtn3_counter < BTN_THRESHOLD && cBtn3 == 1)
cBtn3 = 0;
if(iBtn4_counter > BTN_THRESHOLD && cBtn4 == 0) // Button 4
{
USART_Transmit(0xB0); // Tuner on
USART_Transmit(0x0F);
USART_Transmit(0x7F);
//OUT_LED |= (1 << LED_RT); // LEDs off
//OUT_LED |= (1 << LED_GN);
cTuner = 1;
cBtn4 = 1;
}
else if (iBtn4_counter < BTN_THRESHOLD && cBtn4 == 1)
cBtn4 = 0;
if (iSw1_counter > BTN_THRESHOLD && cSw1 == 0) // Switch Pos. 1
{
Bank(0);
cBank = 0;
cTuner = 0;
cSw1 = 1;
}
else if (iSw1_counter < BTN_THRESHOLD && cSw1 == 1)
{
cSw1 = 0;
}
if(iSw2_counter > BTN_THRESHOLD && cSw2 == 0) // Switch Pos. 2
{
Bank(1);
cBank = 1;
cTuner = 0;
cSw2 = 1;
}
else if (iSw2_counter < BTN_THRESHOLD && cSw2 == 1)
{
cSw2 = 0;
}
}
} }
void Bank(unsigned char num) { /**
if(num == 0) { * Main routine.
USART_Transmit(0xB0); // Bank 1 *
USART_Transmit(0x00); * @return This method should never terminate.
USART_Transmit(0x00); */
USART_Transmit(0xC0); int main(void) {
USART_Transmit(0x00); DDRB = 0x00; // PB1..7 as inputs
USART_Transmit(cChannel); PORTB = 0xFF; // interlan pull-ups for PB0..7y
OUT_LED &= ~(1 << LED_RT); // LED green DDRD = 0x30; // PD4..5 as outputs
OUT_LED |= (1 << LED_GN); PORTD = 0x0C; // internal pull-ups for PD2..3
}
else { OUT_LED = 0x30; // LEDs off
USART_Transmit(0xB0); // Bank 2
USART_Transmit(0x00); // Timer1 init
USART_Transmit(0x01); TCCR1A |= (1 << WGM01); // Set timer 0 to CTC-Mode
USART_Transmit(0xC0); TCCR1B &= ~(1 << CS10); // Prescaler 8 (1MHz)
USART_Transmit(0x00); TCCR1B |= (1 << CS11);
USART_Transmit(cChannel); TCCR1B &= ~(1 << CS12);
OUT_LED &= ~(1 << LED_GN); // LED green OCR1A = 499; // Pperiod of 1 ms
OUT_LED |= (1 << LED_RT);
} cli(); // Disable interrupts
TIMSK |= (1 << TOIE1); // Activate timer 1
sei(); // Enable interrupts
//USART_Init(38400); // Initialize USART with 38400 baud (debug)
USART_Init(31250); // Initialize USART with 31250 baud
while (1) {
if (counterBtn1 > BTN_THRESHOLD && stateBtn1 == 0) { // Button 1
setBank(currentBank);
USART_Transmit(0xC0); // Channel 1
USART_Transmit(0x00);
currentChannel = 0;
currentTuner = 0;
stateBtn1 = 1;
} else if (counterBtn1 < BTN_THRESHOLD && stateBtn1 == 1) {
stateBtn1 = 0;
}
if (counterBtn2 > BTN_THRESHOLD && stateBtn2 == 0) { // Button 2
setBank(currentBank);
USART_Transmit(0xC0); // Channel 2
USART_Transmit(0x01);
currentChannel = 1;
currentTuner = 0;
stateBtn2 = 1;
} else if (counterBtn2 < BTN_THRESHOLD && stateBtn2 == 1) {
stateBtn2 = 0;
}
if (counterBtn3 > BTN_THRESHOLD && stateBtn3 == 0) { // Button 3
setBank(currentBank);
USART_Transmit(0xC0); // Channel 3
USART_Transmit(0x02);
currentChannel = 2;
currentTuner = 0;
stateBtn3 = 1;
} else if (counterBtn3 < BTN_THRESHOLD && stateBtn3 == 1) {
stateBtn3 = 0;
}
if (counterBtn4 > BTN_THRESHOLD && stateBtn4 == 0) { // Button 4
USART_Transmit(0xB0); // Tuner on
USART_Transmit(0x0F);
USART_Transmit(0x7F);
//OUT_LED |= (1 << LED_RT); // LEDs off
//OUT_LED |= (1 << LED_GN);
currentTuner = 1;
stateBtn4 = 1;
} else if (counterBtn4 < BTN_THRESHOLD && stateBtn4 == 1) {
stateBtn4 = 0;
}
if (counterSw1 > BTN_THRESHOLD && stateSw1 == 0) { // Switch Pos. 1
setBank(0);
currentBank = 0;
currentTuner = 0;
stateSw1 = 1;
} else if (counterSw1 < BTN_THRESHOLD && stateSw1 == 1) {
stateSw1 = 0;
}
if (counterSw2 > BTN_THRESHOLD && stateSw2 == 0) { // Switch Pos. 2
setBank(1);
currentBank = 1;
currentTuner = 0;
stateSw2 = 1;
} else if (counterSw2 < BTN_THRESHOLD && stateSw2 == 1) {
stateSw2 = 0;
}
}
} }
/* interrupt function: TIMER1_OVF1_vect /**
* interrupt function: TIMER1_OVF1_vect
* *
* Timer interrupt. Increments counters for inputs * Timer interrupt. Increments counters for inputs
*/ */
ISR(TIMER1_OVF_vect) ISR(TIMER1_OVF_vect) {
{ if (++cnt == 250) {
if(++cnt == 250) cnt = 0; cnt = 0;
if(cTuner == 1 && cnt == 1) { }
if(cBank == 1) OUT_LED ^= (1<<LED_GN);
else OUT_LED ^= (1<<LED_RT);
}
if (bit_is_clear(IN_BTN, BTN1)) { // Button 1 if (currentTuner == 1 && cnt == 1) {
if (iBtn1_counter < BTN_MAX) iBtn1_counter++; if (currentBank == 1) {
} else { OUT_LED ^= (1 << LED_GN);
if (iBtn1_counter > 0) iBtn1_counter--; } else {
} OUT_LED ^= (1 << LED_RT);
if (bit_is_clear(IN_BTN, BTN2)) { // Button 2 }
if (iBtn2_counter < BTN_MAX) iBtn2_counter++; }
} else {
if (iBtn2_counter > 0) iBtn2_counter--; if (bit_is_clear(IN_BTN, BTN1) && counterBtn1 < BTN_MAX) { // Button 1
} counterBtn1++;
if (bit_is_clear(IN_BTN, BTN3)) { // Button 3 } else if (counterBtn1 > 0) {
if (iBtn3_counter < BTN_MAX) iBtn3_counter++; counterBtn1--;
} else { }
if (iBtn3_counter > 0) iBtn3_counter--;
} if (bit_is_clear(IN_BTN, BTN2) && counterBtn2 < BTN_MAX) { // Button 2
if (bit_is_clear(IN_BTN, BTN4)) { // Button 4 counterBtn2++;
if (iBtn4_counter < BTN_MAX) iBtn4_counter++; } else if (counterBtn2 > 0) {
} else { counterBtn2--;
if (iBtn4_counter > 0) iBtn4_counter--; }
}
if (bit_is_clear(IN_SW, SW1)) { // Switch 1
if (iSw1_counter < BTN_MAX) iSw1_counter++; if (bit_is_clear(IN_BTN, BTN3) && counterBtn3 < BTN_MAX) { // Button 3
} else { counterBtn3++;
if (iSw1_counter > 0) iSw1_counter--; } else if (counterBtn3 > 0) {
} counterBtn3--;
if (bit_is_clear(IN_SW, SW2)) { // Switch 2 }
if (iSw2_counter < BTN_MAX) iSw2_counter++;
} else { if (bit_is_clear(IN_BTN, BTN4) && counterBtn4 < BTN_MAX) { // Button 4
if (iSw2_counter > 0) iSw2_counter--; counterBtn4++;
} } else if (counterBtn4 > 0) {
counterBtn4--;
}
if (bit_is_clear(IN_SW, SW1) && counterSw1 < BTN_MAX) { // Switch 1
counterSw1++;
} else if (counterSw1 > 0) {
counterSw1--;
}
if (bit_is_clear(IN_SW, SW2) && counterSw2 < BTN_MAX) { // Switch 2
counterSw2++;
} else if (counterSw2 > 0) {
counterSw2--;
}
} }

View File

@ -1,39 +1,24 @@
// midi-to-parport.h /**
// Typ: C-Headerfile * MIDI Footswitch
// Project: MIDI-to-Parallelport interface *
// Date: 27.08.2012 * @file main.h
// Author: ska * @author Stefan Kalscheuer
* @date 2014-06-17
*/
#define IN_SW PIND #define IN_SW PIND
#define SW1 PD2 #define SW1 PD2
#define SW2 PD3 #define SW2 PD3
#define IN_BTN PINB #define IN_BTN PINB
#define BTN1 PB0 #define BTN1 PB0
#define BTN2 PB1 #define BTN2 PB1
#define BTN3 PB2 #define BTN3 PB2
#define BTN4 PB3 #define BTN4 PB3
#define OUT_LED PORTD #define OUT_LED PORTD
#define LED_RT PD5 #define LED_RT PD5
#define LED_GN PD4 #define LED_GN PD4
#define BTN_THRESHOLD 50 #define BTN_THRESHOLD 50
#define BTN_MAX 100 #define BTN_MAX 100
void delayms(unsigned int millis) {
// uint16_t loop;
while ( millis ) {
_delay_ms(1);
millis--;
}
}
void delayus(unsigned long millis) {
// uint16_t loop;
while ( millis ) {
_delay_us(1);
millis--;
}
}

View File

@ -1,36 +1,63 @@
void USART_Init( unsigned long baud ) /**
{ * MIDI Footswitch
/* Calculate UBRR */ *
unsigned int ubrr_val = (unsigned int)((F_CPU/(baud*16L))-1); * @file main.h
/* Set baud rate */ * @author Stefan Kalscheuer
UBRRH = (unsigned char)(ubrr_val>>8); * @date 2014-06-17
UBRRL = (unsigned char)ubrr_val; * @brief USART helper funcitons
/* Enable transmitter */ */
UCSRB |= (1<<TXEN);
/* Set frame format: 8data, 1stop bit */ /**
UCSRC = (3<<UCSZ0); * Initialize USART interface with given BAUD rate.
*
* @param baud Baud rate.
* @return void
*/
void USART_Init(uint32_t baud) {
/* Calculate UBRR */
uint16_t ubrr_val = (uint16_t) ((F_CPU / (baud * 16L)) - 1);
/* Set baud rate */
UBRRH = (uint8_t) (ubrr_val >> 8);
UBRRL = (uint8_t) ubrr_val;
/* Enable transmitter */
UCSRB |= (1 << TXEN);
/* Set frame format: 8data, 1stop bit */
UCSRC = (3 << UCSZ0);
} }
unsigned char USART_Receive( void ) /**
{ * Receive USART data from buffer.
/* Wait for data to be received */ *
while ( !(UCSRA & (1<<RXC)) ) * @return Data byte from buffer.
; */
/* Get and return received data from buffer */ unsigned char USART_Receive(void) {
return UDR; /* Wait for data to be received */
while (!(UCSRA & (1 << RXC)));
/* Get and return received data from buffer */
return UDR;
} }
void USART_Transmit( unsigned char data ) /**
{ * Transmit USART data byte.
/* Wait for empty transmit buffer */ *
while ( !( UCSRA & (1<<UDRE)) ) * @param data Data byte to transmit.
; * @return void
/* Put data into buffer, sends the data */ */
UDR = data; void USART_Transmit(uint8_t data) {
/* Wait for empty transmit buffer */
while (!(UCSRA & (1 << UDRE)));
/* Put data into buffer, sends the data */
UDR = data;
} }
void USART_Flush( void ) /**
{ * Flush USART buffer.
unsigned char dummy; *
while ( UCSRA & (1<<RXC) ) dummy = UDR; * @return void
*/
void USART_Flush(void) {
uint8_t dummy;
while (UCSRA & (1 << RXC)) {
dummy = UDR;
}
} }