#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
//Using an array to define all of the LEDs. Keeps code short. (JI)
int ledArray[8] = {47, 48, 45, 0, 35, 36, 37, 38};
//Function Declarations
void initialize_gpio(); //When called, all LEDs will be set as outputs. (JI)
void printbin8(uint8_t valu); //When called, the value of the condition passed will be stored as an 8-bit unsigned integer. (JI)
void app_main() {
initialize_gpio(); //Initliazing LEDs as outputs. This is only done once. (JI)
//While-loop is required in order to iterate through each binary number on the counter, as well as have its associated LED turn on. (JI)
while (1) {
uint8_t count = 0b00000001; // Start with first LED on
// Move left (shifting left)
for (int i = 0; i < 8; i++) {
printf("\nThe value of %d in binary is: 0b", count);
printbin8(count);
for (int k = 0; k < 8; k++) {
gpio_set_level(ledArray[k], 0);
}
for (int k = 0; k < 8; k++) {
if (count & (1 << k)) {
gpio_set_level(ledArray[k], 1);
}
}
vTaskDelay(pdMS_TO_TICKS(800));
if (i < 7) {
count <<= 1;
}
}
// Now shift right (left to right movement)
for (int i = 0; i < 8; i++) {
printf("\nThe value of %d in binary is: 0b", count);
printbin8(count);
// Turn off all LEDs
for (int k = 0; k < 8; k++) {
gpio_set_level(ledArray[k], 0);
}
for (int k = 0; k < 8; k++) {
if (count & (1 << k)) {
gpio_set_level(ledArray[k], 1);
}
}
vTaskDelay(pdMS_TO_TICKS(800));
if (i < 7) {
count >>= 1; // Shift right
}
}
}
}
//Function Definitions (JI)
//Function defintion to intialize all LEDs as outputs. (JI)
void initialize_gpio() {
for (int i = 7; i >= 0; i--) {
gpio_set_direction(ledArray[i], GPIO_MODE_OUTPUT); //Setting all LEDs as outputs. (JI)
}
}
//Function definition for the binary counter. (JI)
void printbin8(uint8_t valu) { //This is a function which will print the value of an 8-bit unsigned integer when called. (JI)
for (int i = 7; i >= 0; i--) { //For-loop structure ensures we are printing from MSB to LSB and not exceeding 8 bits. (JI)
printf("%u", (valu >> i) & 1); //The value of 'valu' is right-shifted by 'i' from the for-loop. (JI)
}
}