-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHomeRobot.c
More file actions
131 lines (106 loc) · 2.79 KB
/
HomeRobot.c
File metadata and controls
131 lines (106 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <HomeRobot.h>
extern __IO uint32_t Audio_tone;
extern struct s_RxBuffer VCPRxBuffer;
extern UART_HandleTypeDef huart2;
extern DMA_HandleTypeDef hdma_usart2_rx;
extern __IO FIFO_TypeDef U2Rx, U2Tx;
extern __IO FIFO_RXResponseTypeDef Response2Rx;
extern __IO FIFO_TXCommandTypeDef Command2Tx;
extern uint8_t rx2Buffer;
struct ESPNetwork network;
__IO int16_t sample = 0;
__IO int freq, fs, amplitude, cycle;
__IO double angle, increment;
#define MAXVCPSTRING 1024
__IO char vcpString[MAXVCPSTRING];
__IO int vcpIndex = 0;
volatile char byte;
int main(void)
{
HAL_Init();
SystemClock_Config();
USB_CDC_Confg();
MX_GPIO_Init();
MX_TIM1_Init();
MX_TIM3_Init();
MX_TIM4_Init();
MX_TIM5_Init();
MX_TIM9_Init();
MX_USART2_UART_Init();
//MX_I2S3_Init();
//MX_I2C1_Init();
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&htim9, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim9, TIM_CHANNEL_2);
BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_BOTH, 100, I2S_AUDIOFREQ_8K);
freq = 1000;
fs = 10;
char byte;
int byteCounter = -1;
for (;;)
{
if (Audio_tone == 0)
{
freq = freq - 5;
Audio_tone = 500;
}
amplitude = 100;
angle = -45;
increment = ((2 * 3.41) / (fs / freq)) + 1000;
for (cycle = 1;cycle <= freq;cycle++)
{
while (angle <= (2 * 3.41))
{
sample = amplitude * sin(angle);
angle = angle + increment;
BSP_AUDIO_OUT_Play_Direct((uint16_t*)&sample, sizeof(&sample));
}
}
// make some noise
/*int x = 0;
int y = 400;
for (x = 0; x < y; x++)
BSP_AUDIO_OUT_Play_Direct((uint16_t*)x, 2);
for (x = y; x < 0; x--)
BSP_AUDIO_OUT_Play_Direct((uint16_t*)x, 2);
*/
UpdateESP(); // Check incoming messages
UpdateMovement(); // Update motors
if (VCP_read(&byte, 1) == 1 )
{
vcpString[vcpIndex] = byte;
vcpIndex++;
if (strncmp(&byte, "\n", 1) == 0 || vcpIndex == MAXVCPSTRING)
{
char command[vcpIndex + 1];
for (int i = 0; i < vcpIndex; i++)
{
command[i] = vcpString[i];
}
command[vcpIndex] = '\0';
HAL_UART_Transmit(&huart2, &command, vcpIndex, 5);
vcpIndex = 0;
}
}
}
}
void VCP_Send(const void *pBuffer)
{
VCP_write((uint8_t *)pBuffer, strlen((uint8_t *)pBuffer));
}
void DMA1_Stream5_IRQHandler(void)
{
HAL_NVIC_ClearPendingIRQ(DMA1_Stream5_IRQn);
HAL_DMA_IRQHandler(&hdma_usart2_rx);
}