Repositorio de los archivos fuentes del software para las pantallas graficas de semavenca llamadas PAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

968 lines
31 KiB

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/timers.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "HUB75.h"
#include "Numeros.h"
#include "FlechasV.h"
#include "FlechasA.h"
#include "FlechasR.h"
#include "Peaton.h"
#include "Circulos.h"
#include "Pines.h"
#include "ds18x20.h"
const char *TAG = "PAS";
#define EDO_AMA 1
#define EDO_VER 2
#define EDO_ROJ 3
// #define SIMULACION
#ifdef SIMULACION
#define CONT_ROJ 12
#define CONT_VER 6
#define CONT_AMA 3
#else
static void Luces_task(void *arg);
#endif
static void HUB75_task(void *arg);
static void DSX18_Task(void *);
#define TAM 1536
DMA_ATTR uint32_t graphicsBuffer[TAM];
// GPS
#define PIN_RTS (UART_PIN_NO_CHANGE)
#define PIN_CTS (UART_PIN_NO_CHANGE)
#define UART_PORT_NUM (2)
#define TASK_STACK_SIZE (4096)
QueueHandle_t cola;
#define BUF_SIZE (2048)
uint8_t *data;
uint8_t calc_NMEA_Checksum(uint8_t *, int);
// Respuesta:
const char DEL_TXT[] = "$PQTXT,W,0,1*23\r\n"; // $PQTXT,W,OK*0A␍␊
const char DEL_CAD[] = "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n"; // $PMTK001,314,3*36␍␊
const char ADD_RMS[] = "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n"; // $PMTK001,314,3*36␍␊ $GNRMC,000030.100,V,,,,,0.00,0.00,060180,,,N,V*24␍␊
static void GPS_task(void *arg);
uint8_t h = 0, m = 0, s = 0, t = 20;
uint8_t gps_hora_ok = 0;
int16_t ds = 0;
uint8_t estado_actual, estado_inicial;
uint8_t estado_solapado, estado_solapado_ant = 0;
uint8_t tiempos_OK = 0;
uint8_t estado = EDO_VER;
uint8_t contador;
float temperatura;
uint8_t TEMP_OK = false;
uint8_t temp_int = 0, seg = 0;
uint8_t at = 40;
// #define AAA 1
// #define DDD 2
// #define III 3
//
// #define ADD 4
// #define AAD 5
// #define IAA 6
// #define IIA 7
// #define IDD 8
// #define IID 9
//
// #define IAD 10
//
// #define SOL 20
//
// #define NADD 21
// #define AAND 22 //
// #define NIAA 23
// #define IINA 24 //
// #define NIDD 25
// #define IIND 26 //
//
// #define IAND 27 //
// #define NIAD 28
uint8_t MODO = IAND;
uint8_t borrar_ptos = 0, nr_b;
void app_main(void)
{
uint16_t i = 0, kk = 0, cu, cd, act_gps = 0, hd, hu, md, mu, td, tu, tg, vv = 0, v5 = 0, nr;
#ifdef SIMULACION
uint8_t cont_edo;
#else
TaskHandle_t Luces_Handle = NULL;
#endif
TaskHandle_t HUB75_Handle = NULL;
TaskHandle_t DSX18_Handle = NULL;
TaskHandle_t GPS_Handle = NULL;
#ifndef SIMULACION
xTaskCreatePinnedToCore(Luces_task, "Luces_task", 2048, NULL, 10, &Luces_Handle, 0);
#endif
xTaskCreatePinnedToCore(GPS_task, "GPS_task", 4096, NULL, 10, &GPS_Handle, 0);
xTaskCreatePinnedToCore(DSX18_Task, "DSX18_task", 4096, NULL, 10, &DSX18_Handle, 0);
uart_write_bytes(UART_PORT_NUM, ADD_RMS, strlen(ADD_RMS));
vTaskDelay(pdMS_TO_TICKS(10000));
xTaskCreatePinnedToCore(HUB75_task, "HUB75_task", 4096, NULL, 10, &HUB75_Handle, 1);
i = 50;
#ifdef SIMULACION
cont_edo = 3;
estado_actual = EDO_VER;
tiempos_OK = 3;
#endif
while (true)
{
i++;
act_gps++;
if (act_gps > 1200)
{
uart_write_bytes(UART_PORT_NUM, ADD_RMS, strlen(ADD_RMS));
act_gps = 0;
}
if (i == 25)
{
if (borrar_ptos == 1)
{
borrar_ptos = 0;
graphicsBuffer[11 * 24 + nr_b + 3] &= 0XFFFFFFF0;
graphicsBuffer[12 * 24 + nr_b + 3] &= 0XFFFFFF00;
graphicsBuffer[13 * 24 + nr_b + 3] &= 0XFFFFFFF0;
graphicsBuffer[11 * 24 + nr_b + 4] &= 0X0FFFFFFF;
graphicsBuffer[12 * 24 + nr_b + 4] &= 0X00FFFFFF;
graphicsBuffer[13 * 24 + nr_b + 4] &= 0X0FFFFFFF;
graphicsBuffer[18 * 24 + nr_b + 3] &= 0XFFFFFFF0;
graphicsBuffer[19 * 24 + nr_b + 3] &= 0XFFFFFF00;
graphicsBuffer[20 * 24 + nr_b + 3] &= 0XFFFFFFF0;
graphicsBuffer[18 * 24 + nr_b + 4] &= 0X0FFFFFFF;
graphicsBuffer[19 * 24 + nr_b + 4] &= 0X00FFFFFF;
graphicsBuffer[20 * 24 + nr_b + 4] &= 0X0FFFFFFF;
borrar_ptos = 0;
}
}
if (i >= 50) // cada segundo
{
i = 0;
#ifdef SIMULACION
cont_edo--;
if (cont_edo == 0)
{
switch (estado_actual)
{
case EDO_VER:
estado_actual = EDO_AMA;
cont_edo = CONT_AMA;
break;
case EDO_AMA:
estado_actual = EDO_ROJ;
cont_edo = CONT_ROJ;
break;
case EDO_ROJ:
estado_actual = EDO_VER;
cont_edo = CONT_VER;
break;
}
}
ds = 10 * cont_edo;
#endif
if (ds > 0)
{
cd = 64 * 4 * ((ds / 10) / 10);
cu = 64 * 4 * ((ds / 10) % 10);
}
switch (estado_actual)
{
case EDO_AMA:
estado = EDO_ROJ;
break;
case EDO_ROJ:
estado = EDO_VER;
break;
case EDO_VER:
estado = EDO_AMA;
break;
}
estado_solapado = gpio_get_level(ENT_SOL);
if (estado_solapado_ant != estado_solapado)
{
estado_solapado_ant = estado_solapado;
}
estado = estado_actual;
switch (estado)
{
case EDO_ROJ:
if ((tiempos_OK == 3) && (ds > 10))
{
for (uint16_t i = 0; i < 64; i++)
{
graphicsBuffer[i * 24 + 0] = NumGRR[cd + 0];
graphicsBuffer[i * 24 + 1] = NumGRR[cd + 1];
graphicsBuffer[i * 24 + 2] = NumGRR[cd + 2];
graphicsBuffer[i * 24 + 3] = NumGRR[cd + 3];
graphicsBuffer[i * 24 + 4] = NumGRR[cu + 0];
graphicsBuffer[i * 24 + 5] = NumGRR[cu + 1];
graphicsBuffer[i * 24 + 6] = NumGRR[cu + 2];
graphicsBuffer[i * 24 + 7] = NumGRR[cu + 3];
cu += 4;
cd += 4;
}
}
else
{
for (uint16_t i = 0; i < 64; i++)
{
for (uint16_t j = 0; j < 8; j++)
{
graphicsBuffer[i * 24 + j] = CR[i * 8 + j];
}
}
}
if ((MODO > SOL) && (estado_solapado == 0) && ((MODO == NADD) || (MODO == NIAA) || (MODO == NIDD) || (MODO == NIAD)))
nr = 16;
else
nr = 8;
if (gps_hora_ok)
{
hd = 32 * 2 * (h / 10);
hu = 32 * 2 * (h % 10);
md = 32 * 2 * (m / 10);
mu = 32 * 2 * (m % 10);
if ((h > 5) && (h < 19))
at = 40;
else
at = 30;
for (uint16_t i = 0; i < 32; i++)
{
graphicsBuffer[i * 24 + nr + 0] = NBI[hd + 0];
graphicsBuffer[i * 24 + nr + 1] = NBI[hd + 1];
graphicsBuffer[i * 24 + nr + 2] = NBI[hu + 0];
graphicsBuffer[i * 24 + nr + 3] = NBI[hu + 1];
graphicsBuffer[i * 24 + nr + 4] = NBD[md + 0];
graphicsBuffer[i * 24 + nr + 5] = NBD[md + 1];
graphicsBuffer[i * 24 + nr + 6] = NBD[mu + 0];
graphicsBuffer[i * 24 + nr + 7] = NBD[mu + 1];
hd += 2;
hu += 2;
md += 2;
mu += 2;
}
graphicsBuffer[11 * 24 + nr + 3] |= 0X00000007;
graphicsBuffer[12 * 24 + nr + 3] |= 0X00000077;
graphicsBuffer[13 * 24 + nr + 3] |= 0X00000007;
graphicsBuffer[11 * 24 + nr + 4] |= 0X70000000;
graphicsBuffer[12 * 24 + nr + 4] |= 0X77000000;
graphicsBuffer[13 * 24 + nr + 4] |= 0X70000000;
graphicsBuffer[18 * 24 + nr + 3] |= 0X00000007;
graphicsBuffer[19 * 24 + nr + 3] |= 0X00000077;
graphicsBuffer[20 * 24 + nr + 3] |= 0X00000007;
graphicsBuffer[18 * 24 + nr + 4] |= 0X70000000;
graphicsBuffer[19 * 24 + nr + 4] |= 0X77000000;
graphicsBuffer[20 * 24 + nr + 4] |= 0X70000000;
borrar_ptos = 1;
nr_b = nr;
}
else
{
for (uint16_t i = 0; i < 32; i++)
{
graphicsBuffer[i * 24 + nr + 0] = 0;
graphicsBuffer[i * 24 + nr + 1] = 0;
graphicsBuffer[i * 24 + nr + 2] = 0;
graphicsBuffer[i * 24 + nr + 3] = 0;
graphicsBuffer[i * 24 + nr + 4] = 0;
graphicsBuffer[i * 24 + nr + 5] = 0;
graphicsBuffer[i * 24 + nr + 6] = 0;
graphicsBuffer[i * 24 + nr + 7] = 0;
}
}
if (TEMP_OK)
{
temp_int = (uint8_t)temperatura;
td = 32 * 2 * (temp_int / 10);
tu = 32 * 2 * (temp_int % 10);
tg = 0;
for (uint16_t i = 32; i < 64; i++)
{
graphicsBuffer[i * 24 + nr + 0] = 0;
graphicsBuffer[i * 24 + nr + 1] = 0;
graphicsBuffer[i * 24 + nr + 2] = NBD[td + 0];
graphicsBuffer[i * 24 + nr + 3] = NBD[td + 1];
graphicsBuffer[i * 24 + nr + 4] = NBD[tu + 0];
graphicsBuffer[i * 24 + nr + 5] = NBD[tu + 1];
graphicsBuffer[i * 24 + nr + 6] = SG[tg + 0];
graphicsBuffer[i * 24 + nr + 7] = SG[tg + 1];
td += 2;
tu += 2;
tg += 2;
}
}
else
{
for (uint16_t i = 32; i < 64; i++)
{
graphicsBuffer[i * 24 + nr + 0] = 0;
graphicsBuffer[i * 24 + nr + 1] = 0;
graphicsBuffer[i * 24 + nr + 2] = 0;
graphicsBuffer[i * 24 + nr + 3] = 0;
graphicsBuffer[i * 24 + nr + 4] = 0;
graphicsBuffer[i * 24 + nr + 5] = 0;
graphicsBuffer[i * 24 + nr + 6] = 0;
graphicsBuffer[i * 24 + nr + 7] = 0;
}
}
break;
case EDO_AMA:
if ((tiempos_OK == 3) && (ds > 10))
{
for (uint16_t i = 0; i < 64; i++)
{
graphicsBuffer[i * 24 + 0] = NumGRA[cd + 0];
graphicsBuffer[i * 24 + 1] = NumGRA[cd + 1];
graphicsBuffer[i * 24 + 2] = NumGRA[cd + 2];
graphicsBuffer[i * 24 + 3] = NumGRA[cd + 3];
graphicsBuffer[i * 24 + 4] = NumGRA[cu + 0];
graphicsBuffer[i * 24 + 5] = NumGRA[cu + 1];
graphicsBuffer[i * 24 + 6] = NumGRA[cu + 2];
graphicsBuffer[i * 24 + 7] = NumGRA[cu + 3];
cu += 4;
cd += 4;
}
}
else
{
for (uint16_t i = 0; i < 64; i++)
{
for (uint16_t j = 0; j < 8; j++)
{
graphicsBuffer[i * 24 + j] = CA[i * 8 + j];
}
}
}
break;
case EDO_VER:
if ((tiempos_OK == 3) && (ds > 10))
{
for (uint16_t i = 0; i < 64; i++)
{
graphicsBuffer[i * 24 + 0] = NumGRV[cd + 0];
graphicsBuffer[i * 24 + 1] = NumGRV[cd + 1];
graphicsBuffer[i * 24 + 2] = NumGRV[cd + 2];
graphicsBuffer[i * 24 + 3] = NumGRV[cd + 3];
graphicsBuffer[i * 24 + 4] = NumGRV[cu + 0];
graphicsBuffer[i * 24 + 5] = NumGRV[cu + 1];
graphicsBuffer[i * 24 + 6] = NumGRV[cu + 2];
graphicsBuffer[i * 24 + 7] = NumGRV[cu + 3];
cu += 4;
cd += 4;
}
}
else
{
for (uint16_t i = 0; i < 64; i++)
{
for (uint16_t j = 0; j < 8; j++)
{
graphicsBuffer[i * 24 + j] = CV[i * 8 + j];
}
}
}
break;
}
}
if ((estado == EDO_ROJ) && ((i % 3) == 0) && ((MODO < SOL) || ((MODO > SOL) && (estado_solapado == 1)))) // No hay Solpados Configurados | No esta activo el Sol
{
kk++;
if (kk >= 30)
kk = 0;
tg = 64 * 8 * kk;
for (uint16_t i = 0; i < 64; i++)
{
graphicsBuffer[i * 24 + 16 + 0] = Peaton[tg + 0];
graphicsBuffer[i * 24 + 16 + 1] = Peaton[tg + 1];
graphicsBuffer[i * 24 + 16 + 2] = Peaton[tg + 2];
graphicsBuffer[i * 24 + 16 + 3] = Peaton[tg + 3];
graphicsBuffer[i * 24 + 16 + 4] = Peaton[tg + 4];
graphicsBuffer[i * 24 + 16 + 5] = Peaton[tg + 5];
graphicsBuffer[i * 24 + 16 + 6] = Peaton[tg + 6];
graphicsBuffer[i * 24 + 16 + 7] = Peaton[tg + 7];
tg += 8;
}
}
if ((estado == EDO_ROJ) && ((i % 10) == 0) && (MODO > SOL) && (estado_solapado == 0)) // Hay un Solapado configurado y esta Activo
{
if (v5 == 0)
{
vv++;
if (vv >= 5)
{
v5 = 1;
vv = 5;
}
}
else if (v5 < 4)
v5++;
else
{
v5 = 0;
vv = 0;
}
uint8_t Despl, ef;
if ((MODO == NIAA) || (MODO == NADD) || (MODO == NIDD) || (MODO == NIAD))
Despl = 8;
else
Despl = 16;
for (uint16_t i = 0; i < 64; i++)
{
for (uint16_t j = 0; j < 8; j++)
{
ef = 1;
if (Despl == 8)
{
if (j > 5)
{
graphicsBuffer[i * 24 + Despl + j] = 0;
ef = 0;
}
}
else
{
if (j < 2)
{
graphicsBuffer[i * 24 + Despl + j] = 0;
ef = 0;
}
}
if (ef == 1)
{
switch (MODO)
{
case NADD:
graphicsBuffer[i * 24 + Despl + j] = Rojo_Sol_AI[vv * 512 + i * 8 + j];
break;
case IINA:
graphicsBuffer[i * 24 + Despl + j] = Rojo_Sol_AD[vv * 512 + i * 8 + j];
break;
case AAND:
case IIND:
case IAND:
graphicsBuffer[i * 24 + Despl + j] = Rojo_Sol_D[vv * 512 + i * 8 + j];
break;
case NIAA:
case NIDD:
case NIAD:
graphicsBuffer[i * 24 + Despl + j] = Rojo_Sol_I[vv * 512 + i * 8 + j];
break;
}
}
}
}
}
if ((estado == EDO_AMA) && (i % 10) == 0)
{
if (v5 == 0)
{
vv++;
if (vv >= 5)
{
v5 = 1;
vv = 5;
}
}
else if (v5 < 4)
v5++;
else
{
v5 = 0;
vv = 0;
}
if ((MODO < SOL) || ((MODO > SOL) && (estado_solapado == 1))) // No hay solape
{
for (uint16_t i = 0; i < 64; i++)
{
for (uint16_t j = 0; j < 16; j++)
{
graphicsBuffer[i * 24 + 8 + j] = Flecha_Amarillo[i * 16 + j];
}
}
}
else
{
for (uint16_t i = 0; i < 64; i++)
{
for (uint16_t j = 0; j < 16; j++)
{
switch (MODO)
{
case NADD:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Ama_Sol_AI[vv * 384 + i * 6 + j];
break;
case IINA:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Ama_Sol_AD[vv * 384 + i * 6 + j];
break;
case AAND:
case IIND:
case IAND:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Ama_Sol_D[vv * 384 + i * 6 + j];
break;
case NIAA:
case NIDD:
case NIAD:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Ama_Sol_I[vv * 1024 + i * 16 + j];
break;
}
}
}
}
}
if ((estado == EDO_VER) && (i % 10) == 0)
{
if (v5 == 0)
{
vv++;
if (vv >= 5)
{
v5 = 1;
vv = 5;
}
}
else if (v5 < 4)
v5++;
else
{
v5 = 0;
vv = 0;
}
if ((MODO < SOL) || ((MODO > SOL) && (estado_solapado == 0))) // No hay solape
{
for (uint16_t i = 0; i < 64; i++)
{
for (uint16_t j = 0; j < 16; j++)
{
switch (MODO)
{
case AAA:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_AAA[vv * 1024 + i * 16 + j];
break;
case DDD:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_DDD[vv * 1024 + i * 16 + j];
break;
case III:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_III[vv * 1024 + i * 16 + j];
break;
case ADD:
case NADD:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_ADD[vv * 1024 + i * 16 + j];
break;
case AAD:
case AAND:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_AAD[vv * 1024 + i * 16 + j];
break;
case IAA:
case NIAA:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_IAA[vv * 1024 + i * 16 + j];
break;
case IIA:
case IINA:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_IIA[vv * 1024 + i * 16 + j];
break;
case IDD:
case NIDD:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_IDD[vv * 1024 + i * 16 + j];
break;
case IID:
case IIND:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_IID[vv * 1024 + i * 16 + j];
break;
case IAD:
case IAND:
case NIAD:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_IAD[vv * 1024 + i * 16 + j];
break;
}
}
}
}
else
{
for (uint16_t i = 0; i < 64; i++)
{
for (uint16_t j = 0; j < 16; j++)
{
switch (MODO)
{
case NADD:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_NADD[vv * 1024 + i * 16 + j];
break;
case AAND:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_AAND[vv * 1024 + i * 16 + j];
break;
case NIAA:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_NIAA[vv * 1024 + i * 16 + j];
break;
case IINA:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_IINA[vv * 1024 + i * 16 + j];
break;
case NIDD:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_NIDD[vv * 1024 + i * 16 + j];
break;
case IIND:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_IIND[vv * 1024 + i * 16 + j];
break;
case IAND:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_IAND[vv * 1024 + i * 16 + j];
break;
case NIAD:
graphicsBuffer[i * 24 + 8 + j] = Flecha_Verde_NIAD[vv * 1024 + i * 16 + j];
break;
}
}
}
}
}
vTaskDelay(pdMS_TO_TICKS(20));
}
}
static void HUB75_task(void *arg)
{
HUB75_init();
while (1)
{
HUB75_displayBuffer_Color64(graphicsBuffer, at);
vTaskDelay(pdMS_TO_TICKS(1));
}
}
#ifndef SIMULACION
#define INTERVALO 20
static void Luces_task(void *arg)
{
uint16_t tiempo[4] = {0, 0, 0, 0};
gpio_reset_pin(ENT_VER);
gpio_set_direction(ENT_VER, GPIO_MODE_INPUT);
gpio_reset_pin(ENT_AMA);
gpio_set_direction(ENT_AMA, GPIO_MODE_INPUT);
gpio_reset_pin(ENT_SOL);
gpio_set_direction(ENT_SOL, GPIO_MODE_INPUT);
estado_inicial = gpio_get_level(ENT_AMA) * 2 + gpio_get_level(ENT_VER);
do
{
vTaskDelay(pdMS_TO_TICKS(100));
estado_actual = gpio_get_level(ENT_AMA) * 2 + gpio_get_level(ENT_VER);
} while (estado_actual == estado_inicial);
while (1)
{
if (tiempos_OK < 3)
{
ds = 0;
estado_inicial = estado_actual;
do
{
vTaskDelay(pdMS_TO_TICKS(100));
ds++;
estado_actual = gpio_get_level(ENT_AMA) * 2 + gpio_get_level(ENT_VER);
} while (estado_actual == estado_inicial);
tiempo[estado_inicial] = ds;
tiempos_OK++;
}
else
{
ds = tiempo[estado_actual] + 10;
estado_inicial = estado_actual;
do
{
vTaskDelay(pdMS_TO_TICKS(100));
if (ds < 0)
tiempos_OK = 0;
else
ds--;
estado_actual = gpio_get_level(ENT_AMA) * 2 + gpio_get_level(ENT_VER);
} while (estado_actual == estado_inicial);
if (ds > INTERVALO)
tiempos_OK = 0;
}
}
}
#endif
static void GPS_task(void *arg)
{
uart_event_t evento;
data = (uint8_t *)malloc(BUF_SIZE);
uart_config_t uart_config =
{
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
};
ESP_ERROR_CHECK(uart_driver_install(UART_PORT_NUM, BUF_SIZE * 2, 0, 10, &cola, 0));
ESP_ERROR_CHECK(uart_param_config(UART_PORT_NUM, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_PORT_NUM, PIN_TXD, PIN_RXD, PIN_RTS, PIN_CTS));
uart_write_bytes(UART_PORT_NUM, ADD_RMS, strlen(ADD_RMS));
vTaskDelay(pdMS_TO_TICKS(100));
uart_write_bytes(UART_PORT_NUM, DEL_TXT, strlen(DEL_TXT));
vTaskDelay(pdMS_TO_TICKS(100));
uart_write_bytes(UART_PORT_NUM, DEL_CAD, strlen(DEL_CAD));
while (true)
{
if (xQueueReceive(cola, (void *)&evento, portMAX_DELAY))
{
if (evento.type == UART_DATA)
{
uint16_t i = 0;
uint8_t len = uart_read_bytes(UART_PORT_NUM, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
data[len] = '\0';
uint8_t Chk_Cal = calc_NMEA_Checksum(data, len);
uint8_t Chk_Cal1 = Chk_Cal / 16;
uint8_t Chk_Cal2 = Chk_Cal % 16;
if (Chk_Cal1 < 10)
Chk_Cal1 += '0';
else
Chk_Cal1 = Chk_Cal1 - 10 + 'A';
if (Chk_Cal2 < 10)
Chk_Cal2 += '0';
else
Chk_Cal2 = Chk_Cal2 - 10 + 'A';
if ((Chk_Cal1 == data[len - 4]) && (Chk_Cal2 == data[len - 3]))
{
i = 0;
while (true)
{
if ((data[i] == 'R') && (data[i + 1] == 'M') && (data[i + 2] == 'C'))
break;
i++;
if (i == len - 2)
break;
}
if (i < len - 2)
{
uint8_t k = 0; //, a;
uint16_t j = 0;
uart_write_bytes(UART_PORT_NUM, DEL_CAD, strlen(DEL_CAD));
//$GNRMC,134508.087,V,,,,,0.00,0.00,251123,,,N,V*2B
while (j < BUF_SIZE)
{
j++;
if (data[j] == ',')
{
k++;
if (k == 12 /*9*/)
break;
}
}
if (k == 12)
{
// 01234567890123456789012345678901234567890123456789
// 1 2 34567 8 9 012
//$GNRMC,134508.087,V,,,,,0.00,0.00,251123,,,N,V*2B
//--a = (data[j + 5] - '0') * 10 + data[j + 6] - '0';
// a = data[j + 1];
// printf("a = %c\n", a);
// Mode indication (A=autonomous positioning, D=differential, E=estimation, N=invalid data)
// if ((a == 'A')||(a == 'D')||(a == 'E'))
{
h = (data[i + 4] - '0') * 10 + data[i + 5] - '0';
m = (data[i + 6] - '0') * 10 + data[i + 7] - '0';
s = (data[i + 8] - '0') * 10 + data[i + 9] - '0';
if ((h < 24) && (m < 60) && (s < 60))
{
if (h < 4)
h += 20;
else
h -= 4;
gps_hora_ok = 1;
}
else
gps_hora_ok = 0;
}
}
}
}
}
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
uint8_t calc_NMEA_Checksum(uint8_t *buf, int n)
{
char Checksum = 0;
for (unsigned char i = 0; i < n; ++i)
{
switch (buf[i])
{
case '$':
break;
case '*':
i = n;
continue;
default:
if (Checksum == 0)
Checksum = buf[i];
else
Checksum = Checksum ^ buf[i];
break;
}
}
return Checksum;
}
void DSX18_Task(void *p)
{
float temperature, temperature_ac, i_ac;
esp_err_t res;
ds18x20_addr_t addr;
uint8_t MAX_SENSORS = 3;
ds18x20_addr_t addrs[MAX_SENSORS];
size_t sensor_count = 0;
gpio_set_pull_mode(DS_PIN, GPIO_PULLUP_ONLY);
while (true)
{
res = ds18x20_scan_devices(DS_PIN, addrs, MAX_SENSORS, &sensor_count);
if (res != ESP_OK)
{
vTaskDelay(pdMS_TO_TICKS(20000));
continue;
}
if (!sensor_count)
{
vTaskDelay(pdMS_TO_TICKS(20000));
continue;
}
else
{
for (uint8_t i = 0; i < sensor_count; i++)
addr = addrs[i];
while (true)
{
temperature_ac = 0;
i_ac = 0;
for (uint8_t i = 0; i < sensor_count; i++)
{
addr = addrs[i];
res = ds18x20_measure_and_read(DS_PIN, addr, &temperature);
if (res == ESP_OK)
{
temperature_ac += temperature;
i_ac++;
}
}
if (i_ac > 0)
{
temperatura = temperature_ac / i_ac;
TEMP_OK = true;
}
vTaskDelay(pdMS_TO_TICKS(20000));
}
}
}
}