#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define SEG_NUM 7
#define DISP_NUM 4
#define A_BIT 1
#define B_BIT 2
#define C_BIT 4
#define D_BIT 8
#define E_BIT 16
#define F_BIT 32
#define G_BIT 64
#define DELAY_TIME 1000
#define COUNTER_TIME 4000
int digitPosition[] = {A_BIT, B_BIT, C_BIT, D_BIT, E_BIT, F_BIT, G_BIT};
int n;
gpio_num_t gpio_for_segment[Seg_Num] = { // These are the pins for each of the Segments.
GPIO_NUM_14, // A
GPIO_NUM_32, // B
GPIO_NUM_15, // C
GPIO_NUM_13, // D
GPIO_NUM_12, // E
GPIO_NUM_26, // F
GPIO_NUM_4 // G
};
int segnebt_level_digit[] = {
0b00111111, /* 0 */
0b00000110, /* 1 */
0b01011011, /* 2 */
0b01001111, /* 3 */
0b01100110, /* 4 */
0b01101101, /* 5 */
0b01111101, /* 6 */
0b00000111, /* 7 */
0b01111111, /* 8 */
0b01100111 /* 9 */
}
gpio_num_t gpio_for_control[] = { //Here are the pins for each of the Digits in the Display
GPIO_NUM_27, // NUMBER 1
GPIO_NUM_25, // NUMBER 2
GPIO_NUM_33, // NUMBER 3
GPIO_NUM_2 // NUMBER 4
};
void turnOffDisplay() { // Here we are controlling the segments turning them on and off, using the array of the segments.
for (int i=0; i<Disp_Num; i++) {
gpio_reset_pin(gpio_for_control[i]);
gpio_set_direction(gpio_for_control[i], GPIO_MODE_OUTPUT);
gpio_set_level(gpio_for_control[i], OFF);
}
}
void resetSegments() {
for (int i=0; i<Seg_Num; i++) {
gpio_reset_pin(gpio_for_segment[i]);
gpio_set_direction(gpio_for_segment[i], GPIO_MODE_OUTPUT);
gpio_set_level(gpio_for_segment[i], ON);
}
}
void initDisplay() {
resetSegments();
turnOffDisplay();
gpio_reset_pin(GPIO_NUM_19);
gpio_set_direction(GPIO_NUM_19, GPIO_MODE_INPUT);
}
void display(void *arg) {
int number, dis_number;
while(1) { //Here we control the display and the digits showing in the display
number = n;
printf("in_display: %d\n", n);
for (int i = 0; i < Disp_Num; i++) {
printf("%d\n", number);
turnOffDisplay();
dis_number = number%10;
number /= 10;
for(int j = 0; j < Seg_Num; j++){
gpio_set_level(gpio_for_segment[j], segment_level[dis_number][j]);
}
gpio_set_level(gpio_for_control[i], ON);
vTaskDelay(DELAY_TIME / portTICK_PERIOD_MS);
}
}
}
void counter(void *arg) {
for(;;) {
n = 4321;
printf("in_counter: %d\n", n);
if(gpio_get_level(GPIO_NUM_19)==ON) n=0;
vTaskDelay(COUNTER_TIME / portTICK_PERIOD_MS);
}
}
extern "C" void app_main() {
initDisplay();
xTaskCreate(&counter, "counter", 2048,NULL,5,NULL);
xTaskCreate(&display, "displayNumber", 2048,NULL,5,NULL);
}