1
0
Fork 0
stm32/Src/main.c

506 lines
14 KiB
C

/**
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
*
* COPYRIGHT(c) 2017 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "main.h"
#include "stm32f4xx_hal.h"
/* USER CODE BEGIN Includes */
#include "ssd1306.h"
#include "fonts.h"
//#include "dht22.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
char UART1_Data;
char UART_Buffer[32];
char text[32];
int i;
typedef struct {
float x;
float y;
float z;
} triPoint;
triPoint vertices[8];
triPoint lineVertices[8];
uint32_t angleX = 0;
uint32_t angleY = 0;
uint32_t angleZ = 0;
struct _tStar
{
unsigned char nX;
unsigned char nY;
unsigned char nStarType;
};
#define STARS_NUM 80
struct _tStar aStars[STARS_NUM];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
triPoint rotateX(triPoint startingPoint, uint16_t angle) {
triPoint computedPoint;
float rad = angle * 3.141592 / 180;
float cosa = cos(rad);
float sina = sin(rad);
computedPoint.x = startingPoint.x;
computedPoint.y = (startingPoint.y * cosa) - (startingPoint.z * sina);
computedPoint.z = (startingPoint.y * sina) + (startingPoint.z * cosa);
return computedPoint;
}
triPoint rotateY(triPoint startingPoint, uint16_t angle) {
triPoint computedPoint;
float rad = angle * 3.141592 / 180;
float cosa = cos(rad);
float sina = sin(rad);
computedPoint.y = startingPoint.y;
computedPoint.z = (startingPoint.z * cosa) - (startingPoint.x * sina);
computedPoint.x = (startingPoint.z * sina) + (startingPoint.x * cosa);
return computedPoint;
}
triPoint rotateZ(triPoint startingPoint, uint16_t angle) {
triPoint computedPoint;
float rad = angle * 3.141592 / 180;
float cosa = cos(rad);
float sina = sin(rad);
computedPoint.z = startingPoint.z;
computedPoint.x = (startingPoint.x * cosa) - (startingPoint.y * sina);
computedPoint.y = (startingPoint.x * sina) + (startingPoint.y * cosa);
return computedPoint;
}
triPoint projectPoint(triPoint startingPoint, uint8_t win_width, uint8_t win_height, uint8_t fov, uint8_t viewer_distance) {
triPoint returnPoint;
float factor = fov/(viewer_distance + startingPoint.z);
returnPoint.x = startingPoint.x * factor + win_width/2;
returnPoint.y = -startingPoint.y * factor + win_height/2;
returnPoint.z = 1;
return returnPoint;
}
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
vertices[0].x = -1;
vertices[0].y = 1;
vertices[0].z = -1;
vertices[1].x = 1;
vertices[1].y = 1;
vertices[1].z = -1;
vertices[2].x = 1;
vertices[2].y = -1;
vertices[2].z = -1;
vertices[3].x = -1;
vertices[3].y = -1;
vertices[3].z = -1;
vertices[4].x = -1;
vertices[4].y = 1;
vertices[4].z = 1;
vertices[5].x = 1;
vertices[5].y = 1;
vertices[5].z = 1;
vertices[6].x = 1;
vertices[6].y = -1;
vertices[6].z = 1;
vertices[7].x = -1;
vertices[7].y = -1;
vertices[7].z = 1;
triPoint calcPoint, newPoint;
unsigned char nStar = 0;
memset(aStars, 0, sizeof(aStars));
//Init
for(nStar = 0; nStar < STARS_NUM; nStar++)
{
aStars[nStar].nX = rand() % 128;
aStars[nStar].nY = rand() % 64;
aStars[nStar].nStarType = rand() % 3;
}
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
ssd1306_Init();
//DHT22_Init();
HAL_UART_Receive_IT(&huart1, (uint8_t*) &UART1_Data, 1);
/*
for(int y = 0; y < 129; y++) {
ssd1306_DrawPixel(y, 20, White);
}
*/
//drawLine(1, 1, 20, 20);
//ssd1306_UpdateScreen();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
int zbr = 40;
int direction = 1;
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
ssd1306_Fill(Black);
//ssd1306_UpdateScreen();
/*
int x, msg;
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6); //Toggle the state of pin PC9
//DHT22_Read(htim6);
//startscrollright(0x00, 0x0F);
for(int times = 0; times < 12; times++) {
ssd1306_Scroll();
ssd1306_UpdateScreen();
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_7); //Toggle the state of pin PC9
}
x = rand() % 60 + 1;
msg = rand() % 2 + 1;
ssd1306_SetCursor(x, 53);
ssd1306_WriteString((msg == 1) ? "GORDOS" : "LOL", Font_7x10, White);
*/
for(nStar = 0; nStar < STARS_NUM; nStar++) {
//move star
switch(aStars[nStar].nStarType) {
case 0: //slow star
aStars[nStar].nX += 2;
break;
case 1: //medium star
aStars[nStar].nX += 3;
break;
case 2: //fast star
aStars[nStar].nX += 4;
break;
}
if (aStars[nStar].nX >= 160) {
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6); //Toggle the state of pin PC9
aStars[nStar].nX = 0;
aStars[nStar].nY = rand() % 200;
aStars[nStar].nStarType = rand() % 3;
continue;
}
//paint star
ssd1306_DrawPixel(aStars[nStar].nX, aStars[nStar].nY, White);
}
/*
ssd1306_SetCursor(zbr - 39, (sin(zbr) * 10) + 10);
ssd1306_WriteString("LOLGORDOS", Font_7x10, White);
//ssd1306_SetCursor(zbr - 39, 53);
ssd1306_SetCursor(zbr - 39, (sin(zbr) * 10) + 43);
ssd1306_WriteString("DEMOSCENE", Font_7x10, White);
*/
if (direction == 1) {
zbr++;
} else {
zbr--;
}
for (uint8_t i = 0; i<8; i++) {
calcPoint = rotateZ(vertices[i],angleZ++);
calcPoint = rotateY(calcPoint,angleY++);
calcPoint = rotateX(calcPoint,angleX++);
newPoint = projectPoint(calcPoint, 128, 64, zbr, 5);
lineVertices[i] = newPoint;
}
drawLine(lineVertices[0].x,lineVertices[0].y,lineVertices[1].x,lineVertices[1].y);
drawLine(lineVertices[1].x,lineVertices[1].y,lineVertices[2].x,lineVertices[2].y);
drawLine(lineVertices[2].x,lineVertices[2].y,lineVertices[3].x,lineVertices[3].y);
drawLine(lineVertices[3].x,lineVertices[3].y,lineVertices[0].x,lineVertices[0].y);
drawLine(lineVertices[0].x,lineVertices[0].y,lineVertices[4].x,lineVertices[4].y);
drawLine(lineVertices[1].x,lineVertices[1].y,lineVertices[5].x,lineVertices[5].y);
drawLine(lineVertices[2].x,lineVertices[2].y,lineVertices[6].x,lineVertices[6].y);
drawLine(lineVertices[3].x,lineVertices[3].y,lineVertices[7].x,lineVertices[7].y);
drawLine(lineVertices[4].x,lineVertices[4].y,lineVertices[5].x,lineVertices[5].y);
drawLine(lineVertices[5].x,lineVertices[5].y,lineVertices[6].x,lineVertices[6].y);
drawLine(lineVertices[6].x,lineVertices[6].y,lineVertices[7].x,lineVertices[7].y);
drawLine(lineVertices[7].x,lineVertices[7].y,lineVertices[4].x,lineVertices[4].y);
ssd1306_UpdateScreen();
HAL_Delay(25);
if (zbr > 100) {
direction = 0;
}
if (zbr < 40) {
direction = 1;
}
}
/* USER CODE END 3 */
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* I2C1 init function */
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 400000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
/* USART1 init function */
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
/*Configure GPIO pins : PA6 PA7 */
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
char n = 0;
char UART_Aux[32];
if (huart->Instance == USART1) {
if (UART1_Data != '\n') {
UART_Buffer[i] = UART1_Data;
i++;
} else {
n = sprintf(UART_Aux, "%s\r\n", UART_Buffer);
HAL_UART_Transmit(&huart1, (uint8_t*) &UART_Aux, n, 1000);
// limpa o '\r'
UART_Buffer[i-1] = 0;
snprintf(text, 32, "%s", UART_Buffer);
i = 0;
}
HAL_UART_Receive_IT(&huart1, (uint8_t*) &UART1_Data, 1);
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/