![]() |
Lab 4
Code for ECE 474 lab4
|
Code for PartIIA. More...
#include <arduinoFFT.h>
#include <Arduino_FreeRTOS.h>
#include "queue.h"
Macros | |
#define | LED_BLINK 49 |
pin for the external LED to blink More... | |
#define | LED_ON 100 |
time in miliseconds for the LED to stay on More... | |
#define | LED_OFF 200 |
time in miliseconds for the LED to stay off More... | |
#define | Note_D4 3400 |
294Hz frequeny to play More... | |
#define | Note_E4 3038 |
329Hz frequency to play More... | |
#define | Note_C4 3830 |
261Hz frequency to play More... | |
#define | Note_C3 7692 |
130Hz frequency to play More... | |
#define | Note_G3 5102 |
196Hz frequencys to play More... | |
#define | Note_R 0 |
slience frequency More... | |
#define | SOUND_DUR 500 |
define the duration for each note/sound the speaker play More... | |
#define | SLEEP_DUR 3000UL |
define sleep time More... | |
#define | REPEAT_TIMES 3 |
Numbers of times to repeat the tune. More... | |
#define | RAND_SIZE 128 |
Size of the random array size. More... | |
#define | TIME_SIZE 5 |
Size of the size of the time array. More... | |
#define | STACK_SMALL 128 |
Size of the stack size 1. More... | |
#define | RAND_MIN -32000 |
Minimum number for creating a random number. More... | |
#define | RAND_MAX 32000 |
Maximum number for creating a random number. More... | |
Functions | |
void | TaskBlink (void *pvParameters) |
blink an external LED More... | |
void | TaskTheme (void *pvParameters) |
Play "Close Encounters" on an external speaker Three times. More... | |
void | RT3p0 (void *pvParameters) |
Function to run task RT3p0. More... | |
void | RT3p1 (void *pvParameters) |
Function to run task RT3p1. More... | |
void | RT4 (void *pvParameters) |
Function to run task RT-4. More... | |
void | setup () |
Funciton to setup RtOS tasks. More... | |
void | loop () |
loop though different funcitons, not in use for this lab More... | |
Variables | |
int | melody [] = { Note_D4, Note_R, Note_E4, Note_R, Note_C4, Note_R, Note_C3, Note_R, Note_G3, Note_R } |
the "Close Encounter" tune going to play More... | |
int | tuneLen = sizeof(melody) / 2 |
define tune array length More... | |
arduinoFFT | FFT = arduinoFFT() |
Create FFT object. More... | |
const double | signalFrequency = 1000 |
frequency (Hz) of the FFT signal More... | |
const double | samplingFrequency = 5000 |
frequency (Hz) of the FFT sample More... | |
const uint16_t | samples = RAND_SIZE |
numbers of samples for the FFT More... | |
double | vReal [samples] |
sample arrays real part More... | |
double | vImag [samples] |
sample arrays imaginary part More... | |
double | rand_array [RAND_SIZE] |
random array RT-3 sent to More... | |
double | store_rand [RAND_SIZE] |
random array RT-4 received from More... | |
unsigned long | time_array [TIME_SIZE] |
time array that each FFT takes to compute that RT-4 will sent back More... | |
unsigned long | store_time [TIME_SIZE] |
time array that each FFT takes to compute that RT-3 will receive once RT-4 sent back More... | |
unsigned long | start_time |
time indicator that the FFTs started to compute More... | |
unsigned long | end_time |
time indicator that the FFTs completed to compute More... | |
QueueHandle_t | xQueue1 |
FreeRTOS Queue for RT3p0. More... | |
QueueHandle_t | xQueue2 |
FreeRTOS Queue shared between RT-3 and 4. More... | |
unsigned long | result |
done result for RT3p1 More... | |
boolean | done |
done signal for RT4 More... | |
Code for PartIIA.
External LED at pin 49 flashes on for 100ms and off for 200ms; speaker at pin 6 plays the theme of “Close Encounters” for three times with 1.5s delay between each play; serial monitor displays time it takes to compute the FFT function for 5 times.
#define LED_BLINK 49 |
pin for the external LED to blink
#define LED_OFF 200 |
time in miliseconds for the LED to stay off
#define LED_ON 100 |
time in miliseconds for the LED to stay on
#define Note_C3 7692 |
130Hz frequency to play
#define Note_C4 3830 |
261Hz frequency to play
#define Note_D4 3400 |
294Hz frequeny to play
#define Note_E4 3038 |
329Hz frequency to play
#define Note_G3 5102 |
196Hz frequencys to play
#define Note_R 0 |
slience frequency
#define RAND_MAX 32000 |
Maximum number for creating a random number.
#define RAND_MIN -32000 |
Minimum number for creating a random number.
#define RAND_SIZE 128 |
Size of the random array size.
#define REPEAT_TIMES 3 |
Numbers of times to repeat the tune.
#define SLEEP_DUR 3000UL |
define sleep time
#define SOUND_DUR 500 |
define the duration for each note/sound the speaker play
#define STACK_SMALL 128 |
Size of the stack size 1.
#define TIME_SIZE 5 |
Size of the size of the time array.
void loop | ( | ) |
loop though different funcitons, not in use for this lab
This program uses RTOS so this function is not in use. Things are done in Tasks.
void RT3p0 | ( | void * | pvParameters | ) |
Function to run task RT3p0.
This task would create a RAND_SIZE
number random array in rand_array
. Then it will use FreeRTOS xQueueCreate
function. to create a queue on xQueue1
. It then uses FreeRTOS's xTaskCreate()
function to create task RT3p1
's function and then stop itself using vTaskSuspend
pvParameters | parameters that FreeRTOS can pass in |
void RT3p1 | ( | void * | pvParameters | ) |
Function to run task RT3p1.
This task would sent back rand_array
pointer to the data into xQueue1
using the FreeRTOS xQueueSendToBack()
function. in a loop that repeats TIME_SIZE
times then it will use xQueueReceive()
to receive store_time
pointer with portMAX_DELAY
delay to back and wait. Then it use serial to print out the the time to compute FFT in another loop that repeats TIME_SIZE
times
pvParameters | parameters that FreeRTOS can pass in |
void RT4 | ( | void * | pvParameters | ) |
Function to run task RT-4.
This task first stores the time it started each loop in start_time
. It will recieve the store_rand
pointer from xQueue1
using the xQueueReceive()
function. It then loop samples
of times to fill the vReal
and vImag
part of the FFT and compute the ramdom buffer. It recorde another time mark end_time
and using the difference between start_time
and end_time
to compute amount of time it took to compute th FFT and store this value into duration
. It put each of such variable into time_array
and report its pointer back to xQueue2
using xQueueSendToBack()
pvParameters | parameters that FreeRTOS can pass in |
void setup | ( | ) |
Funciton to setup RtOS tasks.
Setup serial communication method and creat RTOS tasks. Set the priority of each task as well as its size.
void TaskBlink | ( | void * | pvParameters | ) |
blink an external LED
First it setup the LED in this function. Then in an infinite loop, it is going to turn LED on using digitalWrite()
function and wait for LED_ON
miliseconds using vTaskDelay()
. Similarly then it uses the same functions to turn it off for LED_OFF
miliseconds
pvParameters | parameters that FreeRTOS can pass in |
void TaskTheme | ( | void * | pvParameters | ) |
Play "Close Encounters" on an external speaker Three times.
First it setup the registers and timers for the speaker. Then loop thought the task three times. Within the loop there is another for loop that increment through melody
array to use the correct note freqencies to play sounds. Use vTaskDelay
to wait in between notes and songs. Use vTaskSuspend
to stop the task.
pvParameters | parameters that FreeRTOS can pass in |
boolean done |
done signal for RT4
unsigned long end_time |
time indicator that the FFTs completed to compute
arduinoFFT FFT = arduinoFFT() |
Create FFT object.
int melody[] = { Note_D4, Note_R, Note_E4, Note_R, Note_C4, Note_R, Note_C3, Note_R, Note_G3, Note_R } |
the "Close Encounter" tune going to play
double rand_array[RAND_SIZE] |
random array RT-3 sent to
unsigned long result |
done result for RT3p1
const uint16_t samples = RAND_SIZE |
numbers of samples for the FFT
const double samplingFrequency = 5000 |
frequency (Hz) of the FFT sample
const double signalFrequency = 1000 |
frequency (Hz) of the FFT signal
unsigned long start_time |
time indicator that the FFTs started to compute
double store_rand[RAND_SIZE] |
random array RT-4 received from
unsigned long store_time[TIME_SIZE] |
time array that each FFT takes to compute that RT-3 will receive once RT-4 sent back
unsigned long time_array[TIME_SIZE] |
time array that each FFT takes to compute that RT-4 will sent back
int tuneLen = sizeof(melody) / 2 |
define tune array length
double vImag[samples] |
sample arrays imaginary part
double vReal[samples] |
sample arrays real part
QueueHandle_t xQueue1 |
FreeRTOS Queue for RT3p0.
QueueHandle_t xQueue2 |
FreeRTOS Queue shared between RT-3 and 4.