This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
Sem-dma/lines_functions.c
Julien Chevalley 902141e8b6 Initial commit
2023-12-11 14:43:05 +01:00

96 lines
2.9 KiB
C

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "stm32f7xx_hal.h"
#include "nokia.h"
#define NUMBER_OF_LINES 20
typedef struct // LINE STRUCTURE
{
int8_t x1; ///< first point X pos
int8_t y1; ///< first point y pos
int8_t x2; ///< second point X pos
int8_t y2; ///< second point Y pos
} LINE;
LINE lines[NUMBER_OF_LINES]; // the lines
uint32_t toClear,toDraw;
LINE directions =
{ 1, 2, 3, 4 }; // the point of lines directions
///////////////////////////////////////////////////////////////////////////////
/// \brief Calculate the new line X position
/// \param pos the pointer to new position
/// \param dir the pointer to the new direction
///////////////////////////////////////////////////////////////////////////////
static void CalcNewLineX(int8_t * pos, int8_t * dir)
{
int8_t temp;
temp = *pos + *dir; // new position calcul
if ((temp > 83) || (temp < 0)) // check limit bounds
{
if (*dir > 0) // was going left to right
{
*dir = 0 - ((rand() * 3 / RAND_MAX) + 1); // change direction
}
else // was going right to left
{
*dir = (rand() * 3 / RAND_MAX) + 1; // change direction
}
temp = *pos + *dir; // recalculate new position
}
*pos = temp; // change position
}
///////////////////////////////////////////////////////////////////////////////
/// \brief Calculate the new line Y position
/// \param pos the pointer to new position
/// \param dir the pointer to the new direction
///////////////////////////////////////////////////////////////////////////////
static void CalcNewLineY(int8_t * pos, int8_t * dir)
{
int8_t temp;
temp = *pos + *dir; // new position calcul
if ((temp > 47) || (temp < 0)) // check limit bounds
{
if (*dir > 0) // was going top to bottom
{
*dir = 0 - ((rand() * 1 / RAND_MAX) + 1); // change direction
}
else // was going bottom to top
{
*dir = (rand() * 1 / RAND_MAX) + 1; // change direction
}
temp = *pos + *dir; // recalculate new position
}
*pos = temp; // change position
}
void ScreenSaver(void)
{
toClear = (toDraw + 1)%NUMBER_OF_LINES; // next line to clear
//--------------------------------------------------------------------
// clear oldest line
NokiaDrawLine(lines[toClear].x1,lines[toClear].y1,
lines[toClear].x2,lines[toClear].y2,0);
//--------------------------------------------------------------------
// draw newest line
NokiaDrawLine(lines[toDraw].x1,lines[toDraw].y1,
lines[toDraw].x2,lines[toDraw].y2,1);
//--------------------------------------------------------------------
lines[toClear] = lines[toDraw]; // for calculation of new line
CalcNewLineX(&lines[toClear].x1,&directions.x1);
CalcNewLineX(&lines[toClear].x2,&directions.x2);
CalcNewLineY(&lines[toClear].y1,&directions.y1);
CalcNewLineY(&lines[toClear].y2,&directions.y2);
toDraw = toClear; // next line to draw
}