Lab 4
Code for ECE 474 lab4
lab4rt_all.ino File Reference

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...
 

Detailed Description

Code for PartIIA.

Author
Fusco Li, Eric Yu
Date
9-June-2022

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.

Macro Definition Documentation

◆ LED_BLINK

#define LED_BLINK   49

pin for the external LED to blink

◆ LED_OFF

#define LED_OFF   200

time in miliseconds for the LED to stay off

◆ LED_ON

#define LED_ON   100

time in miliseconds for the LED to stay on

◆ Note_C3

#define Note_C3   7692

130Hz frequency to play

◆ Note_C4

#define Note_C4   3830

261Hz frequency to play

◆ Note_D4

#define Note_D4   3400

294Hz frequeny to play

◆ Note_E4

#define Note_E4   3038

329Hz frequency to play

◆ Note_G3

#define Note_G3   5102

196Hz frequencys to play

◆ Note_R

#define Note_R   0

slience frequency

◆ RAND_MAX

#define RAND_MAX   32000

Maximum number for creating a random number.

◆ RAND_MIN

#define RAND_MIN   -32000

Minimum number for creating a random number.

◆ RAND_SIZE

#define RAND_SIZE   128

Size of the random array size.

◆ REPEAT_TIMES

#define REPEAT_TIMES   3

Numbers of times to repeat the tune.

◆ SLEEP_DUR

#define SLEEP_DUR   3000UL

define sleep time

◆ SOUND_DUR

#define SOUND_DUR   500

define the duration for each note/sound the speaker play

◆ STACK_SMALL

#define STACK_SMALL   128

Size of the stack size 1.

◆ TIME_SIZE

#define TIME_SIZE   5

Size of the size of the time array.

Function Documentation

◆ loop()

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.

◆ RT3p0()

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

Parameters
pvParametersparameters that FreeRTOS can pass in

◆ RT3p1()

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

Parameters
pvParametersparameters that FreeRTOS can pass in
See also
RT4

◆ RT4()

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()

Parameters
pvParametersparameters that FreeRTOS can pass in
See also
RT3p1
https://raw.githubusercontent.com/kosme/arduinoFFT/master/Examples/FFT_01/FFT_01.ino

◆ setup()

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.

◆ TaskBlink()

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

Parameters
pvParametersparameters that FreeRTOS can pass in

◆ TaskTheme()

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.

Parameters
pvParametersparameters that FreeRTOS can pass in

Variable Documentation

◆ done

boolean done

done signal for RT4

◆ end_time

unsigned long end_time

time indicator that the FFTs completed to compute

◆ FFT

arduinoFFT FFT = arduinoFFT()

Create FFT object.

◆ melody

the "Close Encounter" tune going to play

◆ rand_array

double rand_array[RAND_SIZE]

random array RT-3 sent to

◆ result

unsigned long result

done result for RT3p1

◆ samples

const uint16_t samples = RAND_SIZE

numbers of samples for the FFT

◆ samplingFrequency

const double samplingFrequency = 5000

frequency (Hz) of the FFT sample

◆ signalFrequency

const double signalFrequency = 1000

frequency (Hz) of the FFT signal

◆ start_time

unsigned long start_time

time indicator that the FFTs started to compute

◆ store_rand

double store_rand[RAND_SIZE]

random array RT-4 received from

◆ store_time

unsigned long store_time[TIME_SIZE]

time array that each FFT takes to compute that RT-3 will receive once RT-4 sent back

◆ time_array

unsigned long time_array[TIME_SIZE]

time array that each FFT takes to compute that RT-4 will sent back

◆ tuneLen

int tuneLen = sizeof(melody) / 2

define tune array length

◆ vImag

double vImag[samples]

sample arrays imaginary part

◆ vReal

double vReal[samples]

sample arrays real part

◆ xQueue1

QueueHandle_t xQueue1

FreeRTOS Queue for RT3p0.

◆ xQueue2

QueueHandle_t xQueue2

FreeRTOS Queue shared between RT-3 and 4.