diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..beb2561 --- /dev/null +++ b/.clang-format @@ -0,0 +1,57 @@ +BasedOnStyle: Chromium +AccessModifierOffset: 0 +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true + AlignCompound: true + AlignFunctionPointers: true + PadOperators: false +AlignConsecutiveBitFields: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true + AlignCompound: true + AlignFunctionPointers: true + PadOperators: true +AlignConsecutiveDeclarations: + Enabled: true + AcrossComments: true + AlignCompound: true + AlignFunctionPointers: true + PadOperators: true +AlignConsecutiveMacros: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true + AlignCompound: true + AlignFunctionPointers: true + PadOperators: true +AlignConsecutiveShortCaseStatements: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true + AlignCaseArrows: true + AlignCaseColons: true +ColumnLimit: 100 +NamespaceIndentation: All +CommentPragmas: "" +IncludeCategories: + - Regex: ^<.*\.h> + Priority: 1 + SortPriority: 0 + CaseSensitive: false + - Regex: ^<.* + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: .* + Priority: 3 + SortPriority: 0 + CaseSensitive: false +IndentPPDirectives: AfterHash +IndentWidth: 4 +TabWidth: 4 +UseTab: Always +Language: Cpp +IndentAccessModifiers: true diff --git a/ex01/main.c b/ex01/main.c index c1bbb10..10b077c 100644 --- a/ex01/main.c +++ b/ex01/main.c @@ -3,11 +3,14 @@ #define LED_PORT PORTB0 int main(void) { - // set pin to output - DDRB |= (_BV(LED_PORT)); + // set pin to output + // DDRB is the register that set the direction of the pins (IN/OUT) + // we want to say that the LED_PORT pin is an OUT pin, which isnt the default + DDRB |= (_BV(LED_PORT)); - // say to pull high the pin - PORTB |= (_BV(LED_PORT)); - while (1) - ; + // say to pull high the pins + // since the pin is set to output, this works ! + PORTB |= (_BV(LED_PORT)); + while (1) + ; } diff --git a/ex02/main.c b/ex02/main.c index 91e8914..82945a2 100644 --- a/ex02/main.c +++ b/ex02/main.c @@ -7,15 +7,23 @@ #define BTN_PORT PIND2 int main(void) { - // set pin to output - DDRB |= (_BV(LED_PORT)); + // set pin to output + // DDRB is the register that set the direction of the pins (IN/OUT) + // we want to say that the LED_PORT pin is an OUT pin, which isnt the default + DDRB |= (_BV(LED_PORT)); - while (true) { - bool cur = (PIND & _BV(BTN_PORT)); - if (!cur) - PORTB |= (_BV(LED_PORT)); - else - PORTB &= ~(_BV(LED_PORT)); - _delay_ms(20.f); - } + while (true) { + // to read the pin value (IN pin), we read from the PINX register, and + // select the correct bit. Here the correct bit is the 1 << BTN_PORT1 + bool cur = (PIND & _BV(BTN_PORT)); + + // depending on if the pin is set, we turn on or off the led + if (!cur) + PORTB |= (_BV(LED_PORT)); // on + else + PORTB &= ~(_BV(LED_PORT)); // off + + // lets wait a bit before going again + _delay_ms(20.f); + } } diff --git a/ex03/main.c b/ex03/main.c index fab1d0b..9bcf010 100644 --- a/ex03/main.c +++ b/ex03/main.c @@ -7,21 +7,31 @@ #define BTN_PORT PIND2 int main(void) { - // set pin to output - DDRB |= (_BV(LED_PORT)); + // set pin to output + // DDRB is the register that set the direction of the pins (IN/OUT) + // we want to say that the LED_PORT pin is an OUT pin, which isnt the default + DDRB |= (_BV(LED_PORT)); - bool led_state = false; - bool prev = (PIND & _BV(BTN_PORT)); - while (true) { - bool cur = (PIND & _BV(BTN_PORT)); - if (prev && !cur) { - led_state = !led_state; - if (led_state) - PORTB |= (_BV(LED_PORT)); - else - PORTB &= ~(_BV(LED_PORT)); - } - prev = cur; - _delay_ms(20.f); - } + // lets keep track of the led state here + bool led_state = false; + // lets also keep track of the button state for the last iteration + bool prev = (PIND & _BV(BTN_PORT)); + while (true) { + // we read it + bool cur = (PIND & _BV(BTN_PORT)); + + // if it wasnt pressed before, but pressed now -> it just turned on + if (prev && !cur) { + // toggle the state and update the led with the new state + led_state = !led_state; + if (led_state) + PORTB |= (_BV(LED_PORT)); + else + PORTB &= ~(_BV(LED_PORT)); + } + // keep track of the old state for next iteration + prev = cur; + // sleep + _delay_ms(20.f); + } } diff --git a/ex04/main.c b/ex04/main.c index 2ab4dd0..b41fac6 100644 --- a/ex04/main.c +++ b/ex04/main.c @@ -4,52 +4,59 @@ #include -#define D1 PB0 -#define D2 PB1 -#define D3 PB2 -#define D4 PB4 +#define D1 PB0 +#define D2 PB1 +#define D3 PB2 +#define D4 PB4 #define SW1 PD2 #define SW2 PD4 int main(void) { - DDRB |= _BV(D4); - DDRB |= _BV(D3); - DDRB |= _BV(D2); - DDRB |= _BV(D1); + // ouput pins for leds + DDRB |= _BV(D4); + DDRB |= _BV(D3); + DDRB |= _BV(D2); + DDRB |= _BV(D1); - DDRD &= ~(_BV(SW1)); - DDRD &= ~(_BV(SW2)); + // set input (not neccessary since input by default) + DDRD &= ~(_BV(SW1)); + DDRD &= ~(_BV(SW2)); - PORTB |= _BV(D4); + PORTB |= _BV(D4); - uint8_t prev = 0; - uint8_t value = 0; - while (true) { - uint8_t cur = (PIND & (_BV(SW1) | _BV(SW2))); - if (prev & (_BV(SW1)) && !(cur & (_BV(SW1)))) { - value++; - } - if (prev & (_BV(SW2)) && !(cur & (_BV(SW2)))) { - value--; - } - prev = cur; - if (value & 0b0001) - PORTB |= (_BV(D1)); - else - PORTB &= ~(_BV(D1)); - if (value & 0b0010) - PORTB |= (_BV(D2)); - else - PORTB &= ~(_BV(D2)); - if (value & 0b0100) - PORTB |= (_BV(D3)); - else - PORTB &= ~(_BV(D3)); - if (value & 0b1000) - PORTB |= (_BV(D4)); - else - PORTB &= ~(_BV(D4)); - _delay_ms(20); - } + uint8_t prev = 0; + uint8_t value = 0; + while (true) { + // lets read both button in a single byte, and keep track of that too + uint8_t cur = (PIND & (_BV(SW1) | _BV(SW2))); + // check for button SW1 + if (prev & (_BV(SW1)) && !(cur & (_BV(SW1)))) { + value++; + } + // check for button SW2 + if (prev & (_BV(SW2)) && !(cur & (_BV(SW2)))) { + value--; + } + prev = cur; + + // for each bit of the counter -> lets turn on or of the led + if (value & 0b0001) + PORTB |= (_BV(D1)); + else + PORTB &= ~(_BV(D1)); + if (value & 0b0010) + PORTB |= (_BV(D2)); + else + PORTB &= ~(_BV(D2)); + if (value & 0b0100) + PORTB |= (_BV(D3)); + else + PORTB &= ~(_BV(D3)); + if (value & 0b1000) + PORTB |= (_BV(D4)); + else + PORTB &= ~(_BV(D4)); + _delay_ms(20); + } }