Unexpected behaviour using function pointers - c

hope everyone one is doing well.
Just out here seeing the craziest programming behaviour I have ever seen, I was wondering if anyone can explain why is this happening?
I finally just finished rewriting how UART packets from an ESP32 is processed within the STM32H753ZI with an array that holds address of functions.
Depending on which packet arrives, for example "0x03" which is "enable line-in input" it will access that function within the function pointer array at the index of the packet, so in this case the "functionPointerArray[3]"
Whats happening is that when I comment out this piece of code:
void I2S_MuteHandler() {
/* SPI1 -> CR1 |= SPI_CR1_CSUSP;
SPI3 -> CR1 |= SPI_CR1_CSUSP;
while (((SPI1 -> CR1) & (SPI_CR1_CSTART)));
while (((SPI3 -> CR1) & (SPI_CR1_CSTART)));
DMA1_Stream0 -> CR &= ~DMA_SxCR_EN;
DMA1_Stream1 -> CR &= ~DMA_SxCR_EN;
DMA1_Stream3 -> CR &= ~DMA_SxCR_EN;
SPI1 -> CR1 &= ~SPI_CR1_SPE;
SPI3 -> CR1 &= ~SPI_CR1_SPE;*/
}
The output wave of which processed through a DSP code and sent out via "I2S3_TxBUFF" of code:
void I2S_HALFCOMPLETE_CALLBACK() {
int * I2S1_RxBUFF = getI2S1_RxBUFF();
int * I2S1_TxBUFF = getI2S1_TxBUFF();
int * I2S3_TxBUFF = getI2S3_TxBUFF();
double INSAMPLE_I2S_L[1024];
double INSAMPLE_I2S_R[1024];
double INSAMPLE_I2S_MONO[2048];
double OUTSAMPLE_I2S_L[1024];
double OUTSAMPLE_I2S_R[1024];
double OUTSAMPLE_I2S_MONO[2048];
for (int i = 0; i < 2048; i += 2) {
//EVEN
INSAMPLE_I2S_L[(i >> 1)] = (double)I2S1_RxBUFF[i] * (double)ADC_COMPENSATOR * 0.01 * centers.volume * centers.inputCompensator;
INSAMPLE_I2S_MONO[i] = (double)((I2S1_RxBUFF[i] + I2S1_RxBUFF[i+1]) >> 1) * (double)ADC_COMPENSATOR * 0.01 * subwoofers.volume;
//ODD
INSAMPLE_I2S_R[(i >> 1)] = (double)I2S1_RxBUFF[i+1] * (double)ADC_COMPENSATOR * 0.01 * centers.volume * centers.inputCompensator;
INSAMPLE_I2S_MONO[i+1] = INSAMPLE_I2S_MONO[i];
}
arm_biquad_cascade_df2T_f64(&subwoofers.audioStream_MONO, INSAMPLE_I2S_MONO, OUTSAMPLE_I2S_MONO, 2048);
arm_biquad_cascade_df2T_f64(&centers.audioStream_L, INSAMPLE_I2S_L, OUTSAMPLE_I2S_L, 1024);
arm_biquad_cascade_df2T_f64(&centers.audioStream_R, INSAMPLE_I2S_R, OUTSAMPLE_I2S_R, 1024);
for (int i = 0 ; i < 2048; i += 2){
//EVEN
I2S1_TxBUFF[i] = (int)OUTSAMPLE_I2S_L[i >> 1];
I2S3_TxBUFF[i] = (int)OUTSAMPLE_I2S_MONO[i];
//ODD
I2S1_TxBUFF[i+1] = (int)OUTSAMPLE_I2S_R[i >> 1];
I2S3_TxBUFF[i+1] = (int)OUTSAMPLE_I2S_MONO[i+1];
}
}
void I2S_TRANSFERCOMPLETE_CALLBACK() {
int * I2S1_RxBUFF = getI2S1_RxBUFF();
int * I2S1_TxBUFF = getI2S1_TxBUFF();
int * I2S3_TxBUFF = getI2S3_TxBUFF();
double INSAMPLE_I2S_L[1024];
double INSAMPLE_I2S_R[1024];
double INSAMPLE_I2S_MONO[2048];
double OUTSAMPLE_I2S_L[1024];
double OUTSAMPLE_I2S_R[1024];
double OUTSAMPLE_I2S_MONO[2048];
for (int i = 2048; i < 4096; i += 2) {
//EVEN
INSAMPLE_I2S_L[(i >> 1)-1024] = (double)I2S1_RxBUFF[i] * (double)ADC_COMPENSATOR * 0.01 * (double)centers.volume * centers.inputCompensator;
INSAMPLE_I2S_MONO[i-2048] = (double)((I2S1_RxBUFF[i] + I2S1_RxBUFF[i+1]) >> 1) * (double)ADC_COMPENSATOR * 0.01 * subwoofers.volume;
//ODD
INSAMPLE_I2S_R[(i >> 1)-1024] = (double)I2S1_RxBUFF[i+1] * (double)ADC_COMPENSATOR * 0.01 * (double)centers.volume * centers.inputCompensator;
INSAMPLE_I2S_MONO[(i+1)-2048] = INSAMPLE_I2S_MONO[i-2048];
}
arm_biquad_cascade_df2T_f64(&subwoofers.audioStream_MONO, INSAMPLE_I2S_MONO, OUTSAMPLE_I2S_MONO, 2048);
arm_biquad_cascade_df2T_f64(&centers.audioStream_L, INSAMPLE_I2S_L, OUTSAMPLE_I2S_L, 1024);
arm_biquad_cascade_df2T_f64(&centers.audioStream_R, INSAMPLE_I2S_R, OUTSAMPLE_I2S_R, 1024);
for (int i = 2048 ; i < 4096; i += 2){
//EVEN
I2S1_TxBUFF[i] = (int)OUTSAMPLE_I2S_L[(i >> 1)-1024];
I2S3_TxBUFF[i] = (int)OUTSAMPLE_I2S_MONO[i-2048];
//ODD
I2S1_TxBUFF[i+1] = (int)OUTSAMPLE_I2S_R[(i >> 1)-1024];
I2S3_TxBUFF[i+1] = (int)OUTSAMPLE_I2S_MONO[(i+1)-2048];
}
}
makes the input sine wave look like square waves and the biquad filters stop working
and when I uncomment the code thats causing the issue, the wave is working perfectly and the biquad filters work.
For a better in-depth look i'll include how I populate the array of function pointers and how it executes.
The function pointer array gets populated during the initialization stage of the MCU along side its peripherals CODE:
void load_functionHandlers() {
messageReceived[0] = I2S_MuteHandler; // I believe the issue stems here in this function
messageReceived[1] = I2S_UnmuteHandler;
// messageReceived[2] = SPDIF_MuteHandler;
// messageReceived[3] = SPDIF_UnmuteHandler;
messageReceived[3] = audioSelectorInlineHandler;
// messageReceived[5] = audioSelectorBluetoothHandler;
// messageReceived[6] = audioSelectorSPDIFHandler;
// messageReceived[7] = audioSelectorHDMIHandler;
// messageReceived[8] = audioSelectorInternalHandler;
// messageReceived[9] = audioSelectorExternalHandler;
// messageReceived[10] = setupIRHandler;
// messageReceived[11] = disableTouchHandler;
// messageReceived[12] = enableTouchHandler;
// messageReceived[13] = nightRiderLightsHandler;
// messageReceived[14] = rainbowLightsHandler;
messageReceived[15] = volumeIncreaseHandler;
messageReceived[16] = volumeDecreaseHandler;
//
messageReceived[17] = n_12dB_subBass_notch_EQHandler;
messageReceived[18] = n_9dB_subBass_notch_EQHandler;
messageReceived[19] = n_6dB_subBass_notch_EQHandler;
messageReceived[20] = n_3dB_subBass_notch_EQHandler;
messageReceived[21] = _0dB_subBass_notch_EQHandler;
messageReceived[22] = p_3dB_subBass_notch_EQHandler;
messageReceived[23] = p_6dB_subBass_notch_EQHandler;
messageReceived[24] = p_9dB_subBass_notch_EQHandler;
messageReceived[25] = p_12dB_subBass_notch_EQHandler;
messageReceived[26] = n_12dB_Bass_notch_EQHandler;
messageReceived[27] = n_9dB_Bass_notch_EQHandler;
messageReceived[28] = n_6dB_Bass_notch_EQHandler;
messageReceived[29] = n_3dB_Bass_notch_EQHandler;
messageReceived[30] = _0dB_Bass_notch_EQHandler;
messageReceived[31] = p_3dB_Bass_notch_EQHandler;
messageReceived[32] = p_6dB_Bass_notch_EQHandler;
messageReceived[33] = p_9dB_Bass_notch_EQHandler;
messageReceived[34] = p_12dB_Bass_notch_EQHandler;
messageReceived[35] = n_12dB_lower_midrange_notch_EQHandler;
messageReceived[36] = n_9dB_lower_midrange_notch_EQHandler;
messageReceived[37] = n_6dB_lower_midrange_notch_EQHandler;
messageReceived[38] = n_3dB_lower_midrange_notch_EQHandler;
messageReceived[39] = _0dB_lower_midrange_notch_EQHandler;
messageReceived[40] = p_3dB_lower_midrange_notch_EQHandler;
messageReceived[41] = p_6dB_lower_midrange_notch_EQHandler;
messageReceived[42] = p_9dB_lower_midrange_notch_EQHandler;
messageReceived[43] = p_12dB_lower_midrange_notch_EQHandler;
messageReceived[44] = n_12dB_midrange_notch_EQHandler;
messageReceived[45] = n_9dB_midrange_notch_EQHandler;
messageReceived[46] = n_6dB_midrange_notch_EQHandler;
messageReceived[47] = n_3dB_midrange_notch_EQHandler;
messageReceived[48] = _0dB_midrange_notch_EQHandler;
messageReceived[49] = p_3dB_midrange_notch_EQHandler;
messageReceived[50] = p_6dB_midrange_notch_EQHandler;
messageReceived[51] = p_9dB_midrange_notch_EQHandler;
messageReceived[52] = p_12dB_midrange_notch_EQHandler;
messageReceived[53] = n_12dB_higher_midrange_notch_EQHandler;
messageReceived[54] = n_9dB_higher_midrange_notch_EQHandler;
messageReceived[55] = n_6dB_higher_midrange_notch_EQHandler;
messageReceived[56] = n_3dB_higher_midrange_notch_EQHandler;
messageReceived[57] = _0dB_higher_midrange_notch_EQHandler;
messageReceived[58] = p_3dB_higher_midrange_notch_EQHandler;
messageReceived[59] = p_6dB_higher_midrange_notch_EQHandler;
messageReceived[60] = p_9dB_higher_midrange_notch_EQHandler;
messageReceived[61] = p_12dB_higher_midrange_notch_EQHandler;
messageReceived[62] = n_12dB_presence_notch_EQHandler;
messageReceived[63] = n_9dB_presence_notch_EQHandler;
messageReceived[64] = n_6dB_presence_notch_EQHandler;
messageReceived[65] = n_3dB_presence_notch_EQHandler;
messageReceived[66] = _0dB_presence_notch_EQHandler;
messageReceived[67] = p_3dB_presence_notch_EQHandler;
messageReceived[68] = p_6dB_presence_notch_EQHandler;
messageReceived[69] = p_9dB_presence_notch_EQHandler;
messageReceived[70] = p_12dB_presence_notch_EQHandler;
messageReceived[71] = n_12dB_brilliance_notch_EQHandler;
messageReceived[72] = n_9dB_brilliance_notch_EQHandler;
messageReceived[73] = n_6dB_brilliance_notch_EQHandler;
messageReceived[74] = n_3dB_brilliance_notch_EQHandler;
messageReceived[75] = _0dB_brilliance_notch_EQHandler;
messageReceived[76] = p_3dB_brilliance_notch_EQHandler;
messageReceived[77] = p_6dB_brilliance_notch_EQHandler;
messageReceived[78] = p_9dB_brilliance_notch_EQHandler;
messageReceived[79] = p_12dB_brilliance_notch_EQHandler;
messageReceived[80] = n_12dB_Bass_lowShelf_EQHandler;
messageReceived[81] = n_9dB_Bass_lowShelf_EQHandler;
messageReceived[82] = n_6dB_Bass_lowShelf_EQHandler;
messageReceived[83] = n_3dB_Bass_lowShelf_EQHandler;
messageReceived[84] = _0dB_Bass_lowShelf_EQHandler;
messageReceived[85] = p_3dB_Bass_lowShelf_EQHandler;
messageReceived[86] = p_6dB_Bass_lowShelf_EQHandler;
messageReceived[87] = p_9dB_Bass_lowShelf_EQHandler;
messageReceived[88] = p_12dB_Bass_lowShelf_EQHandler;
//messageReceived[89] = disableLightsHandler;
}
How the function pointer gets executed CODE:
static void (*messageReceived[255])(void); // Global Variable
void UART4_TRANSFERCOMPELTE_CALLBACK(uint8_t exitPacket) {
//functions pointers
if (exitPacket) {
if ((exitPacket >= 0) && (exitPacket <= 88)) {
(*messageReceived[exitPacket])();
}
} else {
uint8_t packet = getUART4_RxBUFF()[0];
if ((packet >= 0) && (packet <= 88)) {
(*messageReceived[packet])();
}
}
}
I can't never predict the behaviour of this. I try commenting out random function pointers where the function pointers get populated into the array and it works, and vice versa
I thought it was a memory address being overwritten somewhere, however I double checked the the two address of when the wave is square and when the wave is perfect. Its registers, address, anything you name it are identical. I am mind blown right now. If anyone can shine light on this would be amazing. It doesnt even enter the function thats in question at all
I know its an issue where populating the array is happening as when I do this test code to bypass the function pointer and comment out out the block of code it doesnt work still.
static void (*messageReceived[255])(void);
void UART4_TRANSFERCOMPELTE_CALLBACK(uint8_t exitPacket) {
//functions pointers
uint8_t * packet;
if (exitPacket) {
packet = malloc(sizeof(uint8_t));
packet[0] = exitPacket;
} else {
packet = getUART4_RxBUFF();
}
//(*messageReceived[packet[0]])(); // By passing function pointer array
audioSelectorInlineHandler();
}
UPDATE 1:
If I once again bypass the function pointer array and comment EVERYTHING in the function that populates the function pointer array. It works
CODE:
/*
* UART_PACKET_PROCESSOR.c
*
* Created on: May 13, 2021
* Author: Christopher
*/
#include <stdint.h>
#include <stdlib.h>
#include <constants.h>
#include <dspFactory.h>
#include <inputSourceFactory.h>
#include <opcode.h>
#include <packetProcessor.h>
#include <uartFactory.h>
static void (*messageReceived[255])(void);
void UART4_TRANSFERCOMPELTE_CALLBACK(uint8_t exitPacket) {
//functions pointers
uint8_t * packet = NULL;
if (exitPacket) {
//(*messageReceived[packet[exitPacket]])();
} else {
//(*messageReceived[getUART4_RxBUFF()[0]])();
}
audioSelectorInlineHandler();
}
void load_functionHandlers() {
// for (int i = 0; i < 255; i++) {
// messageReceived[i] = 0;
// }
//
// messageReceived[0] = &I2S_MuteHandler;
// messageReceived[1] = &I2S_UnmuteHandler;
//// messageReceived[2] = SPDIF_MuteHandler;
//// messageReceived[3] = SPDIF_UnmuteHandler;
//
// messageReceived[3] = &audioSelectorInlineHandler;
//// messageReceived[5] = audioSelectorBluetoothHandler;
//// messageReceived[6] = audioSelectorSPDIFHandler;
//// messageReceived[7] = audioSelectorHDMIHandler;
//
//// messageReceived[8] = audioSelectorInternalHandler;
//// messageReceived[9] = audioSelectorExternalHandler;
//
//// messageReceived[10] = setupIRHandler;
//// messageReceived[11] = disableTouchHandler;
//// messageReceived[12] = enableTouchHandler;
//
//// messageReceived[13] = nightRiderLightsHandler;
//// messageReceived[14] = rainbowLightsHandler;
//
// messageReceived[15] = &volumeIncreaseHandler;
// messageReceived[16] = &volumeDecreaseHandler;
////
// messageReceived[17] = &n_12dB_subBass_notch_EQHandler;
// messageReceived[18] = &n_9dB_subBass_notch_EQHandler;
// messageReceived[19] = &n_6dB_subBass_notch_EQHandler;
// messageReceived[20] = &n_3dB_subBass_notch_EQHandler;
// messageReceived[21] = &_0dB_subBass_notch_EQHandler;
// messageReceived[22] = &p_3dB_subBass_notch_EQHandler;
// messageReceived[23] = &p_6dB_subBass_notch_EQHandler;
// messageReceived[24] = &p_9dB_subBass_notch_EQHandler;
// messageReceived[25] = &p_12dB_subBass_notch_EQHandler;
//
// messageReceived[26] = &n_12dB_Bass_notch_EQHandler;
// messageReceived[27] = &n_9dB_Bass_notch_EQHandler;
// messageReceived[28] = &n_6dB_Bass_notch_EQHandler;
// messageReceived[29] = &n_3dB_Bass_notch_EQHandler;
// messageReceived[30] = &_0dB_Bass_notch_EQHandler;
// messageReceived[31] = &p_3dB_Bass_notch_EQHandler;
// messageReceived[32] = &p_6dB_Bass_notch_EQHandler;
// messageReceived[33] = &p_9dB_Bass_notch_EQHandler;
// messageReceived[34] = &p_12dB_Bass_notch_EQHandler;
//
// messageReceived[35] = &n_12dB_lower_midrange_notch_EQHandler;
// messageReceived[36] = &n_9dB_lower_midrange_notch_EQHandler;
// messageReceived[37] = &n_6dB_lower_midrange_notch_EQHandler;
// messageReceived[38] = &n_3dB_lower_midrange_notch_EQHandler;
// messageReceived[39] = &_0dB_lower_midrange_notch_EQHandler;
// messageReceived[40] = &p_3dB_lower_midrange_notch_EQHandler;
// messageReceived[41] = &p_6dB_lower_midrange_notch_EQHandler;
// messageReceived[42] = &p_9dB_lower_midrange_notch_EQHandler;
// messageReceived[43] = &p_12dB_lower_midrange_notch_EQHandler;
//
// messageReceived[44] = &n_12dB_midrange_notch_EQHandler;
// messageReceived[45] = &n_9dB_midrange_notch_EQHandler;
// messageReceived[46] = &n_6dB_midrange_notch_EQHandler;
// messageReceived[47] = &n_3dB_midrange_notch_EQHandler;
// messageReceived[48] = &_0dB_midrange_notch_EQHandler;
// messageReceived[49] = &p_3dB_midrange_notch_EQHandler;
// messageReceived[50] = &p_6dB_midrange_notch_EQHandler;
// messageReceived[51] = &p_9dB_midrange_notch_EQHandler;
// messageReceived[52] = &p_12dB_midrange_notch_EQHandler;
//
// messageReceived[53] = &n_12dB_higher_midrange_notch_EQHandler;
// messageReceived[54] = &n_9dB_higher_midrange_notch_EQHandler;
// messageReceived[55] = &n_6dB_higher_midrange_notch_EQHandler;
// messageReceived[56] = &n_3dB_higher_midrange_notch_EQHandler;
// messageReceived[57] = &_0dB_higher_midrange_notch_EQHandler;
// messageReceived[58] = &p_3dB_higher_midrange_notch_EQHandler;
// messageReceived[59] = &p_6dB_higher_midrange_notch_EQHandler;
// messageReceived[60] = &p_9dB_higher_midrange_notch_EQHandler;
// messageReceived[61] = &p_12dB_higher_midrange_notch_EQHandler;
//
// messageReceived[62] = &n_12dB_presence_notch_EQHandler;
// messageReceived[63] = &n_9dB_presence_notch_EQHandler;
// messageReceived[64] = &n_6dB_presence_notch_EQHandler;
// messageReceived[65] = &n_3dB_presence_notch_EQHandler;
// messageReceived[66] = &_0dB_presence_notch_EQHandler;
// messageReceived[67] = &p_3dB_presence_notch_EQHandler;
// messageReceived[68] = &p_6dB_presence_notch_EQHandler;
// messageReceived[69] = &p_9dB_presence_notch_EQHandler;
// messageReceived[70] = &p_12dB_presence_notch_EQHandler;
//
// messageReceived[71] = &n_12dB_brilliance_notch_EQHandler;
// messageReceived[72] = &n_9dB_brilliance_notch_EQHandler;
// messageReceived[73] = &n_6dB_brilliance_notch_EQHandler;
// messageReceived[74] = &n_3dB_brilliance_notch_EQHandler;
// messageReceived[75] = &_0dB_brilliance_notch_EQHandler;
// messageReceived[76] = &p_3dB_brilliance_notch_EQHandler;
// messageReceived[77] = &p_6dB_brilliance_notch_EQHandler;
// messageReceived[78] = &p_9dB_brilliance_notch_EQHandler;
// messageReceived[79] = &p_12dB_brilliance_notch_EQHandler;
//
// messageReceived[80] = &n_12dB_Bass_lowShelf_EQHandler;
// messageReceived[81] = &n_9dB_Bass_lowShelf_EQHandler;
// messageReceived[82] = &n_6dB_Bass_lowShelf_EQHandler;
// messageReceived[83] = &n_3dB_Bass_lowShelf_EQHandler;
// messageReceived[84] = &_0dB_Bass_lowShelf_EQHandler;
// messageReceived[85] = &p_3dB_Bass_lowShelf_EQHandler;
// messageReceived[86] = &p_6dB_Bass_lowShelf_EQHandler;
// messageReceived[87] = &p_9dB_Bass_lowShelf_EQHandler;
// messageReceived[88] = &p_12dB_Bass_lowShelf_EQHandler;
//messageReceived[89] = disableLightsHandler;
}
UPDATE 2:
If I tried another function to comment out and it still does the same behaviour.
CODE:
void load_functionHandlers() {
for (int i = 0; i < 255; i++) {
messageReceived[i] = 0;
}
messageReceived[0] = &I2S_MuteHandler;
messageReceived[1] = &I2S_UnmuteHandler; //Comment this shows same behavoir
// messageReceived[2] = SPDIF_MuteHandler;
// messageReceived[3] = SPDIF_UnmuteHandler;
messageReceived[3] = &audioSelectorInlineHandler;
// messageReceived[5] = audioSelectorBluetoothHandler;
// messageReceived[6] = audioSelectorSPDIFHandler;
// messageReceived[7] = audioSelectorHDMIHandler;
// messageReceived[8] = audioSelectorInternalHandler;
// messageReceived[9] = audioSelectorExternalHandler;
// messageReceived[10] = setupIRHandler;
// messageReceived[11] = disableTouchHandler;
// messageReceived[12] = enableTouchHandler;
// messageReceived[13] = nightRiderLightsHandler;
// messageReceived[14] = rainbowLightsHandler;
messageReceived[15] = &volumeIncreaseHandler; // no affect
messageReceived[16] = &volumeDecreaseHandler; // no affect
//
messageReceived[17] = &n_12dB_subBass_notch_EQHandler; ////////
messageReceived[18] = &n_9dB_subBass_notch_EQHandler;
messageReceived[19] = &n_6dB_subBass_notch_EQHandler;
messageReceived[20] = &n_3dB_subBass_notch_EQHandler;
messageReceived[21] = &_0dB_subBass_notch_EQHandler;
messageReceived[22] = &p_3dB_subBass_notch_EQHandler; any of these shows same behavior
messageReceived[23] = &p_6dB_subBass_notch_EQHandler;
messageReceived[24] = &p_9dB_subBass_notch_EQHandler;
messageReceived[25] = &p_12dB_subBass_notch_EQHandler;
messageReceived[26] = &n_12dB_Bass_notch_EQHandler;
messageReceived[27] = &n_9dB_Bass_notch_EQHandler;
messageReceived[28] = &n_6dB_Bass_notch_EQHandler;
messageReceived[29] = &n_3dB_Bass_notch_EQHandler;
messageReceived[30] = &_0dB_Bass_notch_EQHandler;
messageReceived[31] = &p_3dB_Bass_notch_EQHandler;
messageReceived[32] = &p_6dB_Bass_notch_EQHandler;
messageReceived[33] = &p_9dB_Bass_notch_EQHandler;
messageReceived[34] = &p_12dB_Bass_notch_EQHandler;
messageReceived[35] = &n_12dB_lower_midrange_notch_EQHandler;
messageReceived[36] = &n_9dB_lower_midrange_notch_EQHandler;
messageReceived[37] = &n_6dB_lower_midrange_notch_EQHandler;
messageReceived[38] = &n_3dB_lower_midrange_notch_EQHandler;
messageReceived[39] = &_0dB_lower_midrange_notch_EQHandler;
messageReceived[40] = &p_3dB_lower_midrange_notch_EQHandler;
messageReceived[41] = &p_6dB_lower_midrange_notch_EQHandler;
messageReceived[42] = &p_9dB_lower_midrange_notch_EQHandler;
messageReceived[43] = &p_12dB_lower_midrange_notch_EQHandler;
messageReceived[44] = &n_12dB_midrange_notch_EQHandler;
messageReceived[45] = &n_9dB_midrange_notch_EQHandler;
messageReceived[46] = &n_6dB_midrange_notch_EQHandler;
messageReceived[47] = &n_3dB_midrange_notch_EQHandler; any of these shows same behavior
messageReceived[48] = &_0dB_midrange_notch_EQHandler;
messageReceived[49] = &p_3dB_midrange_notch_EQHandler;
messageReceived[50] = &p_6dB_midrange_notch_EQHandler;
messageReceived[51] = &p_9dB_midrange_notch_EQHandler;
messageReceived[52] = &p_12dB_midrange_notch_EQHandler;
messageReceived[53] = &n_12dB_higher_midrange_notch_EQHandler;
messageReceived[54] = &n_9dB_higher_midrange_notch_EQHandler;
messageReceived[55] = &n_6dB_higher_midrange_notch_EQHandler;
messageReceived[56] = &n_3dB_higher_midrange_notch_EQHandler; any of these shows same behavior
messageReceived[57] = &_0dB_higher_midrange_notch_EQHandler;
messageReceived[58] = &p_3dB_higher_midrange_notch_EQHandler;
messageReceived[59] = &p_6dB_higher_midrange_notch_EQHandler;
messageReceived[60] = &p_9dB_higher_midrange_notch_EQHandler;
messageReceived[61] = &p_12dB_higher_midrange_notch_EQHandler;
messageReceived[62] = &n_12dB_presence_notch_EQHandler;
messageReceived[63] = &n_9dB_presence_notch_EQHandler;
messageReceived[64] = &n_6dB_presence_notch_EQHandler;
messageReceived[65] = &n_3dB_presence_notch_EQHandler; any of these shows same behavior
messageReceived[66] = &_0dB_presence_notch_EQHandler;
messageReceived[67] = &p_3dB_presence_notch_EQHandler;
messageReceived[68] = &p_6dB_presence_notch_EQHandler;
messageReceived[69] = &p_9dB_presence_notch_EQHandler;
messageReceived[70] = &p_12dB_presence_notch_EQHandler;
messageReceived[71] = &n_12dB_brilliance_notch_EQHandler;
messageReceived[72] = &n_9dB_brilliance_notch_EQHandler;
messageReceived[73] = &n_6dB_brilliance_notch_EQHandler;
messageReceived[74] = &n_3dB_brilliance_notch_EQHandler;
messageReceived[75] = &_0dB_brilliance_notch_EQHandler;
messageReceived[76] = &p_3dB_brilliance_notch_EQHandler; any of these shows same behavior
messageReceived[77] = &p_6dB_brilliance_notch_EQHandler;
messageReceived[78] = &p_9dB_brilliance_notch_EQHandler;
messageReceived[79] = &p_12dB_brilliance_notch_EQHandler;
messageReceived[80] = &n_12dB_Bass_lowShelf_EQHandler;
messageReceived[81] = &n_9dB_Bass_lowShelf_EQHandler;
messageReceived[82] = &n_6dB_Bass_lowShelf_EQHandler;
messageReceived[83] = &n_3dB_Bass_lowShelf_EQHandler;
messageReceived[84] = &_0dB_Bass_lowShelf_EQHandler; any of these shows same behavior
messageReceived[85] = &p_3dB_Bass_lowShelf_EQHandler;
messageReceived[86] = &p_6dB_Bass_lowShelf_EQHandler;
messageReceived[87] = &p_9dB_Bass_lowShelf_EQHandler;
messageReceived[88] = &p_12dB_Bass_lowShelf_EQHandler; ////////////
//messageReceived[89] = disableLightsHandler;
}

It is important to use C properly, otherwise, the one will be used by C. The code (in a question) suffers from numerous issues, I will emphasize just one: naming.
Thus, I had to assume few things since naming is not logical. Maybe I got it right.
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
typedef uint8_t function_id_type ;
/* valid function index is 0 ... (0xFF-1) */
#define MAX_FUNCTIONS 255
/* I assumed bellow has to be in the range above?
if not just change the value here
This first function is for some special purpose
*/
#define EXIT_PACKET_ID 0
#define VALID_FUNCTION_INDEX(IDX_) \
(IDX_ >= 0 && IDX_ < MAX_FUNCTIONS)
//
function_id_type getUART4_RxBUFF() {
// this is OK value as for testing/demo as we have
// not more that two elements in the function table
return (function_id_type)1;
// this will provoke assert exit downstream
// return (function_id_type)(MAX_FUNCTIONS + MAX_FUNCTIONS);
};
// type alias
// function table made of functions
// to process the messages received
typedef void (*function_table[MAX_FUNCTIONS])(void);
// demo workers
static void I2S_MuteHandler() {}
static void I2S_UnmuteHandler() {}
static void create_function_table ( function_table ft_ )
{
ft_[0] = I2S_MuteHandler;
ft_[1] = I2S_UnmuteHandler;
// leave the rest empty
}
// full table validity is obviously an expensive operation
#define PARTIALY_VALID_F_TABLE(FT) ( FT && FT[0] && FT[1] )
static void UART4_TRANSFERCOMPELTE_CALLBACK(
function_id_type exitPacket,
// due to the arg declaration
// will have to contain min MAX_FUNCTIONS
// and can not be null
void (*function_table[static MAX_FUNCTIONS])(void))
{
assert( PARTIALY_VALID_F_TABLE(function_table) );
if (exitPacket == EXIT_PACKET_ID) {
(*function_table[exitPacket])();
} else {
const function_id_type packet = getUART4_RxBUFF();
// in debug builds check before use
assert(VALID_FUNCTION_INDEX(packet)) ;
// index is valid but is there a function
// under that index
assert(function_table[packet]) ;
// all tests passed, use
(*function_table[packet])();
}
}
//
int main(void)
{
function_table my_ft_ = {0};
// this call fails on assert as ft is empty
// UART4_TRANSFERCOMPLETE_CALLBACK(EXIT_PACKET_ID, my_ft_) ;
create_function_table( my_ft_ ) ;
// OK
UART4_TRANSFERCOMPLETE_CALLBACK(EXIT_PACKET_ID, my_ft_) ;
// on this call make sure EXIT_PACKET_ID is not used
UART4_TRANSFERCOMPLETE_CALLBACK(EXIT_PACKET_ID + 1, my_ft_) ;
return EXIT_SUCCESS ;
}
There is nothing tricky or snazzy above and I hope the code is readable, with better naming, and easier to get right for the exact use-case. No Oscilloscope pictures were necessary :)
Also here is the link to the working Godbolt: https://godbolt.org/z/sa37a6o5a
Note there are no compiler arguments given. Please use them, in your work, after learning them and then if proven necessary.
HTH

Related

Can't Pass Array into UBO

I'm trying to initialize a setup in Vulkan where within the Shader there is a uniform array of textures and an corresponding arrays for the textures Width and Heights:
layout(binding=0) uniform UniformBufferObject {
mat4 model; //4x4 array of floats for uniform rotation and positioning
uint texW[32]; //Widths of ith texture
uint texH[32]; //Heights of ith texture
} ubo;
For some reason the shader only reads the mat4 and the first index of the texW. Everything else is initialized to 0.
Here's the Uniform Buffer Setup Code, for reference
void createUniformBuffers() {
/**Uniform Buffer for View Transformation*/
VkDeviceSize bufferSize = sizeof(uniformBufferObject);
uniformBuffers = malloc(swapChainSize * sizeof(VkBuffer));
uniformBuffersMemory = malloc(swapChainSize * sizeof (VkDeviceMemory));
for(uint i = 0; i < swapChainSize; i++)
createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffers[i], &uniformBuffersMemory[i]);
}
void createDescriptorPool() {
VkDescriptorPoolSize poolSizes[4];
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
poolSizes[0].descriptorCount = swapChainSize;
poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
poolSizes[1].descriptorCount = swapChainSize;
VkDescriptorPoolCreateInfo poolInfo;
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
poolInfo.pNext = 0;
poolInfo.flags = 0;
poolInfo.poolSizeCount = 2;
poolInfo.pPoolSizes = poolSizes;
poolInfo.maxSets = swapChainSize;
if(vkCreateDescriptorPool(lDevice, &poolInfo, 0, & descriptorPool)) {
fprintf(stderr, "Failed to create Descriptor Pool\n");
exit(1);
}
}
void createDescriptorSets() {
VkDescriptorSetLayout layouts[swapChainSize];
for(uint i = 0; i < swapChainSize; i++) layouts[i] = descriptorSetLayout;
VkDescriptorSetAllocateInfo allocInfo;
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.pNext = 0;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = swapChainSize;
allocInfo.pSetLayouts = layouts;
descriptorSets = malloc(sizeof(VkDescriptorSet) * swapChainSize);
if(vkAllocateDescriptorSets(lDevice, &allocInfo, descriptorSets) ) {
fprintf(stderr,"Failed to allocate descriptor sets\n");
exit(1);
}
for(uint i = 0; i < swapChainSize; i++) {
VkDescriptorBufferInfo bufferInfo;
bufferInfo.buffer = uniformBuffers[i];
bufferInfo.offset = 0;
bufferInfo.range = VK_WHOLE_SIZE; //sizeof(uniformBufferObject);
VkWriteDescriptorSet descriptorWrites[2];
descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].pNext = 0;
descriptorWrites[0].dstSet = descriptorSets[i];
descriptorWrites[0].dstBinding = 0;
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorWrites[0].descriptorCount = 1;
descriptorWrites[0].pBufferInfo = &bufferInfo;
descriptorWrites[0].pImageInfo = 0;
descriptorWrites[0].pTexelBufferView = 0;
descriptorWrites[0].dstArrayElement = 0;
VkDescriptorImageInfo imageInfo;
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = textureImageView[0];
imageInfo.sampler= textureSampler;
descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[1].pNext = 0;
descriptorWrites[1].dstBinding = 1;
descriptorWrites[1].dstSet = descriptorSets[i];
descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWrites[1].descriptorCount = 1;
descriptorWrites[1].pImageInfo = &imageInfo;
descriptorWrites[1].pBufferInfo = 0;
descriptorWrites[1].pTexelBufferView = 0;
descriptorWrites[1].dstArrayElement = 0;
vkUpdateDescriptorSets(lDevice, 2, descriptorWrites,0,0);
}
}
Here's the code where the uniformBuffer is updated,
void updateUniformBuffer(uint currentImage) {
//FTR, I know I probably shouldn't remap each update, optimizing this is on my todo-list
vkMapMemory(lDevice, uniformBuffersMemory[currentImage], 0, sizeof(uniformBufferObject), 0, (void*) &uData);
memcpy(uData, &uniformData, sizeof(uniformBufferObject));
memset(uData->model, 0, sizeof(uData->model));
//Rotating the View, GLSL acknowledges this data
uData->model[2][2] = uData->model[3][3] = 1;
uData->model[0][0] = cosf(angle);
uData->model[0][1] = -sinf(angle);
uData->model[1][1] = cosf(angle);
uData->model[1][0] = sinf(angle);
angle+= 0.05f;
uData->texW[0] = 1024; //<-------GLSL Vertex Shader will acknowledge this =)
uData->texH[0] = 1024; //<-------GLSL Vertex Shader will ignore this =(
uData->texH[0] = 1024; //<-------GLSL Vertex Shader will also ignore this >_<
vkUnmapMemory(lDevice,uniformBuffersMemory[currentImage]);}
Any pointers or advice would be greatly appreciated.

Ble data sending with nrf52

I am working with nrf52 and I made a custom service for 17 bytes data array and I want to send it but it gives fatal error while in data update (sending) function and resets the program.
Here's my custom service data update function:
uint32_t ble_cus_mydata_update(ble_cus_t * p_cus, uint8_array_t mydata, uint16_t conn_handle)
{
if (p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)
{
ble_gatts_hvx_params_t params;
memset(&params, 0, sizeof(params));
params.type = BLE_GATT_HVX_NOTIFICATION;
params.handle = p_cus->mydata_char_handles.value_handle;
params.p_data = &mydata;
params.p_len = 17;
return sd_ble_gatts_hvx(conn_handle, &params);
}}
and i send data with timeout_handler
static void mydata_timeout_handler(void * p_context)
{
ret_code_t err_code;
UNUSED_PARAMETER(p_context);
bsp_board_led_on(2);
nrf_delay_ms(100);
bsp_board_led_off(2);
uint8_array_t mydata_value [17] = {0x55,0x10,0x01,0x23,0x99,0xFF,0xFF,0xCC,0xBB,0x00,0x00,0x00,0x00,0x00,0x24,0x24,0x20};
err_code = ble_cus_mydata_update(&m_cus, *mydata_value , m_conn_handle);
if ((err_code != NRF_SUCCESS) &&
(err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_BUSY) &&
(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
)
{
APP_ERROR_HANDLER(err_code);
}
}
And here is my service char add
uint8_array_t mydata_char_init_value [17] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = MYDATA_CHAR_UUID;
add_char_params.uuid_type = p_cus->uuid_type;
add_char_params.init_len = 17;
add_char_params.max_len = 17;
add_char_params.p_init_value = mydata_char_init_value;
add_char_params.char_props.read = 1;
add_char_params.char_props.notify = 1;
add_char_params.read_access = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;
err_code = characteristic_add(p_cus->service_handle,
&add_char_params,
&p_cus->mydata_char_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
I get an error:
nrf error_code= 0x2000310c
There is no info about this error message; it throws this fatal error and goes to nrf_breakpoint_cond after error handler and resets itself.
Please help me; I tried everything I know and I can't move forward. Thanks in advance to whomever tries to help me.
i solved it and wanted to help others who might need it its a little different this code is for sending 4 bytes of data you need to do a little changes for 17 bytes but its not that much the other codes are same you just need to change update function to something like this
uint32_t ble_cus_pres_level_update(ble_cus_t * p_cus, uint32_t presvalue)
{
// NRF_LOG_INFO("In ble_cus_presvalue_update. \r\n");
if (p_cus == NULL)
{
return NRF_ERROR_NULL;
}
uint32_t err_code = NRF_SUCCESS;
ble_gatts_value_t gatts_value;
// Initialize value struct.
memset(&gatts_value, 0, sizeof(gatts_value));
gatts_value.len = sizeof(uint32_t);
gatts_value.offset = 0;
gatts_value.p_value = &presvalue;
// Update database.
err_code = sd_ble_gatts_value_set(p_cus->conn_handle,
p_cus->bme280_presvalue_char_handles.value_handle,
&gatts_value);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
// Send value if connected and notifying.
if (p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)
{
ble_gatts_hvx_params_t params;
memset(&params, 0, sizeof(params));
params.type = BLE_GATT_HVX_NOTIFICATION;
params.handle = p_cus->bme280_presvalue_char_handles.value_handle;
params.offset = gatts_value.offset;
params.p_data = gatts_value.p_value;
params.p_len = &gatts_value.len;
err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &params);
// NRF_LOG_INFO("sd_ble_gatts_hvx result: %x. \r\n", err_code);
}
else
{
err_code = NRF_ERROR_INVALID_STATE;
// NRF_LOG_INFO("sd_ble_gatts_hvx result: NRF_ERROR_INVALID_STATE. \r\n");
}
return err_code;
}
Shouldn't err_code = ble_cus_mydata_update(&m_cus, *mydata_value , m_conn_handle); be err_code = ble_cus_mydata_update(&m_cus, mydata_value , m_conn_handle);?

Unable to add more entries to Firebase after 11

I have a database in firebase that is meant to hold user data and I'm able to get it to add account data up to number 10 (zero based), but after that, anytime I call the function to add to the database it doesn't show up in Firebase. I am using this for a game in Unity. Here are the function calls.
`
public void populateDatabase()
{
//populate static account data
AccountData _thisAccount = new AccountData();
_thisAccount.accName = Account.instance.NickName();
_thisAccount.accEmail = PlayerAccount._this.getUserEmail();
_thisAccount.accCash = 100;
_thisAccount.accGem = 100;
//Ranking Values
_thisAccount.Rank = 0;
//rookie
_thisAccount.RookieRank = 0;
_thisAccount.RookieAverage = 0;
_thisAccount.RookieScoreOne = 0;
_thisAccount.RookieScoreTwo = 0;
_thisAccount.RookieScoreThree = 0;
//Amature
_thisAccount.AmatureRank = 0;
_thisAccount.AmatureAverage = 0;
_thisAccount.AmatureScoreOne = 0;
_thisAccount.AmatureScoreTwo = 0;
_thisAccount.AmatureScoreThree = 0;
//Semi
_thisAccount.SemiProRank = 0;
_thisAccount.SemiAverage = 0;
_thisAccount.SemiProScoreOne = 0;
_thisAccount.SemiProScoreTwo = 0;
_thisAccount.SemiProScoreThree = 0;
//Pro
_thisAccount.ProRank = 0;
_thisAccount.ProAverage = 0;
_thisAccount.ProScoreOne = 0;
_thisAccount.ProScoreTwo = 0;
_thisAccount.ProScoreThree = 0;
_thisAccount.accTricks = Account.instance.ReturnTricks();
string tName = _thisAccount.getAccName();
Debug.Log("Name: " + tName + " account " + _thisAccount.accTricks.Length);
Debug.Log("Email: " + _thisAccount.accEmail);
Debug.Log("Cash: " + _thisAccount.accCash);
Debug.Log("Attempt to fill Database info");
ConstuctDatabase(usersdb, _thisAccount);
}
private void ConstuctDatabase(DatabaseReference AccountRef, AccountData _thisAccount)
{
int num = 0;
AccountRef.RunTransaction(MutableData =>
{
num++;
List<object> account = MutableData.Value as List<object>;
if (account == null)
{
account = new List<object>();
}
else
{
Debug.Log("continue");
}
Dictionary<string, object> newAccount =
new Dictionary<string, object>();
newAccount["AccountName"] = _thisAccount.accName;
newAccount["AccountEmail"] = _thisAccount.accEmail;
newAccount["Cash"] = _thisAccount.accCash;
newAccount["Gem"] = _thisAccount.accGem;
//Ranking Values
newAccount["Rank"] = _thisAccount.Rank;
//rookie
newAccount["RookieRank"] = _thisAccount.RookieRank;
newAccount["RookieAverage"] = _thisAccount.RookieAverage;
newAccount["RookieScoreOne"] = _thisAccount.RookieScoreOne;
newAccount["RookieScoreTwo"] = _thisAccount.RookieScoreTwo;
newAccount["RookieScoreThree"] = _thisAccount.RookieScoreThree;
//Amature
newAccount["AmateurRank"] = _thisAccount.AmatureRank;
newAccount["AmateurAverage"] = _thisAccount.AmatureAverage;
newAccount["AmateurScoreOne"] = _thisAccount.AmatureScoreOne;
newAccount["AmateurScoreTwo"] = _thisAccount.AmatureScoreTwo;
newAccount["AmateurScoreThree"] = _thisAccount.AmatureScoreThree;
//Semi
newAccount["SemiProRank"] = _thisAccount.SemiProRank;
newAccount["SemiProAverage"] = _thisAccount.SemiAverage;
newAccount["SemiProScoreOne"] = _thisAccount.SemiProScoreOne;
newAccount["SemiProScoreTwo"] = _thisAccount.SemiProScoreTwo;
newAccount["SemiProScoreThree"] = _thisAccount.SemiProScoreThree;
//Pro
newAccount["ProRank"] = _thisAccount.ProRank;
newAccount["ProAverage"] = _thisAccount.ProAverage;
newAccount["ProScoreOne"] = _thisAccount.ProScoreOne;
newAccount["ProScoreTwo"] = _thisAccount.ProScoreTwo;
newAccount["ProScoreThree"] = _thisAccount.ProScoreThree;
//Trick Logic]
Dictionary<string, bool> newTricks =
new Dictionary<string, bool>();
int i = 0;
while (i < _thisAccount.accTricks.Length)
{
newTricks["Trick" + i] = _thisAccount.accTricks[i]._owned;
i++;
}
newAccount["TrickList"] = newTricks;
Debug.Log("ConstructDB1");
account.Add(newAccount);
MutableData.Value = account;
Debug.Log("ConstructDB2");
return TransactionResult.Success(MutableData);
});
}
`
Unfortunately, this is a known issue in 6.6.0. If you pay attention to the release page, I'd recommend upgrading as soon as possible.
Now an explanation of what's going on with a small workaround: an array is being serialized as effectively a dictionary with a numeric key. It's parsing numbers lexicographically (ex: it's doing some thing like 1, 10, 2, 3, 4, 5, 6...) and it breaks at 11. To work around this, rather than adding a list as you currently are, try serializing a dictionary with lexicographically ordered keys (ex: M001, M002, M003, &c). I know this isn't ideal, but it should unblock you for the time being!

Filling values in 1D array

Code Fragment
if (val==1)
paperR[LIMIT]={100,50,20,10,5,2,1};
else if (val==2)
paperR[LIMIT]={200,100,50,20,10,5,1};
PROBLEM ?
it's like doing
int ask;
LATER IN CODE
ask=1;
How to do it with array ?
You can't assign to an array, you can only provide a list of values if you're initializing the array at the point where it's being declared.
If you want to fill in an array, you can use memcpy from another array that contains the values you want to use. So you can declare:
const int arr100[] = {100,50,20,10,5,2,1};
const int arr200[] = {200,100,50,20,10,5,1};
int paperR[LIMIT];
if (val == 1) {
memcpy(paperR, arr100, sizeof arr100);
} else if (val == 2) {
memcpy(paperR, arr200, sizeof arr200);
}
You may do it by one element after other:
if (val==1)
{
paperR[0] = 100;
paperR[1] = 50;
paperR[2] = 20;
paperR[3] = 10;
paperR[4] = 5;
paperR[5] = 2;
paperR[6] = 1;
}
else if (val==2)
{
paperR[0] = 200;
paperR[1] = 100;
paperR[2] = 50;
paperR[3] = 20;
paperR[4] = 10;
paperR[5] = 5;
paperR[6] = 1;
}

Dynamically populate Text Fields in Flash (AS3)

I am trying to figure out how to dynamically populate textfields in Flash. The text in the fields will either say ON or OFF based on the value of bstatus.
var bstatus will either have a value of 0 or 1
I have the following textfields listed below.
I'm not sure if the syntax is correct, but I was thinking that the text fields would be in an array, and I would create a for loop--that will go through the array in order to have the fields populated.
var textFields:Array = new Array();
textFields[0] = compTxt.text;
textFields[1] = bathLightTxt.text;
textFields[2] = computerLightTxt.text;
textFields[3] = kitchenLight.text;
textFields[4] = livingLightTxt.text;
textFields[5] = descLightTxt.text;
textFields[6] = laundryLightTxt.text;
textFields[3] = ovenTxt.text;
textFields[8] = tvTxt.text;
textFields[9] = washerTxt.text;
I'm thinking that that the textFields Array will go inside a function called populateFields()
// function populateFields ------------------------------------------------------------------
function populateFields(bstatus:int)
{
var displayText:String="";
var textFields:Array = new Array();
textFields[0] = compTxt.text;
textFields[1] = bathLightTxt.text;
textFields[2] = computerLightTxt.text;
textFields[3] = kitchenLight.text;
textFields[4] = livingLightTxt.text;
textFields[5] = descLightTxt.text;
textFields[6] = laundryLightTxt.text;
textFields[3] = ovenTxt.text;
textFields[8] = tvTxt.text;
textFields[9] = washerTxt.text;
if (bstatus == 0) {
// textfield will say OFF
displayText = "OFF";
} else if (bstatus == 1) {
// textfield will say ON
displayText = "ON";
}
/*
for (var i:int=0;i<textFields.length;i++)
{
// do something
}
*/
}
Entire Code:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.EventDispatcher; //event dispatcher
public class House extends MovieClip
{
// List Objects inside Treehouse here....
//private var comp:MovieClip; // comp is a property of TreeHouse
//private var light:MovieClip;
//var comp:HouseObjects = new HouseObjects(); //do this inside another class
var HouseObjects:Array = new Array(); // creates HouseObjects array
var onList:Array = [];
var obj_num:int = 10;
var power:int; // holds value of individual House Objects
var bstate:int; // 0 or 1 (ON or OFF)
var bstatus:int;
var userInput:int; // stores user data (of selected data); holds value of e.currentTarget.power
var currentPower:int; // stores current power
//var objName:String;
var objSelect:Object;
// Constructor--------------------------------------------------------------------
public function House()
{
var currentPower:int = 0;
HouseObjects[0] = new Comp();
HouseObjects[1] = new Light(); // bathroom light
HouseObjects[2] = new LightB(); // computer area light
HouseObjects[3] = new LightC(); // kitchen light
HouseObjects[4] = new LightD(); // living room light
HouseObjects[5] = new LightE(); // description light
HouseObjects[6] = new LightF(); // laundry room light
HouseObjects[7] = new Oven();
HouseObjects[8] = new Tv();
HouseObjects[9] = new Washer();
//HouseObjects[10] = new Car();
//onList.push(objName);
//trace(objName);
//onList.push(HouseObjects[1]);
trace("Tracing onList Array: " + onList);
// list properties of the objects -----------------------------------------------------------------
HouseObjects[0].power = 2; // amount of power
HouseObjects[0].name = "comp";
//comp.bstate = 0; // button state
HouseObjects[1].power = 1; // amount of power
HouseObjects[1].name = "light";
HouseObjects[2].power = 1; // amount of power
HouseObjects[2].name = "lightB";
HouseObjects[3].power = 1; // amount of power
HouseObjects[3].name = "lightC";
HouseObjects[4].power = 1; // amount of power
HouseObjects[4].name = "lightD";
HouseObjects[5].power = 1; // amount of power
HouseObjects[5].name = "lightE";
HouseObjects[6].power = 1; // amount of power
HouseObjects[6].name = "lightF";
HouseObjects[7].power = 3; // amount of power
HouseObjects[7].name = "oven";
HouseObjects[8].power = 4; // amount of power
HouseObjects[8].name = "tv";
HouseObjects[9].power = 5; // amount of power
HouseObjects[9].name = "washer";
//HouseObjects[9].power = 6; // amount of power
//HouseObjects[9].name = "car";
// end of list properties of the house objects -----------------------------------------------------
for (var i:int=0;i<obj_num;i++)
{
HouseObjects[i].buttonMode = true;
//add event listeners -- listens to functions that are called
HouseObjects[i].addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
HouseObjects[i].addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
HouseObjects[i].addEventListener(MouseEvent.CLICK, toggleClick);
HouseObjects[i].addEventListener(MouseEvent.CLICK, toggleClick);
HouseObjects[i].bstate = 0;
stage.addChild(HouseObjects[i]); // add House Objects to stage ------------------------------
}// end of for loop ------------------------------------------------------------------------
trace("tracing...");
//computer's position
HouseObjects[0].x = 585;
HouseObjects[0].y = 233;
//bathroom light's position
HouseObjects[1].x = 340;
HouseObjects[1].y = 161;
//computer lightB's position
HouseObjects[2].x = 579;
HouseObjects[2].y = 158;
//computer lightC's position
HouseObjects[3].x = 316;
HouseObjects[3].y = 368;
//computer lightD's position
HouseObjects[4].x = 657;
HouseObjects[4].y = 367;
//computer lightE's position
HouseObjects[5].x = 517;
HouseObjects[5].y = 549;
//computer lightF's position
HouseObjects[6].x = 531;
HouseObjects[6].y = 1000;
//oven's position
HouseObjects[7].x = 380;
HouseObjects[7].y = 449;
//tv's position
HouseObjects[8].x = 543;
HouseObjects[8].y = 423;
//washer's position
HouseObjects[9].x = 637;
HouseObjects[9].y = 1155;
} // end of Constructor -----------------------------------------------------------
// function rollOver --------------------------------------------------------------
function rolloverToggle(e:MouseEvent) {
if (e.currentTarget.currentFrame == 1)
e.currentTarget.gotoAndStop(2);
if (e.currentTarget.currentFrame == 3)
e.currentTarget.gotoAndStop(4);
}
// function rollOut -----------------------------------------------------------------
function rolloutToggle(e:MouseEvent) {
if (e.currentTarget.currentFrame == 2)
e.currentTarget.gotoAndStop(1);
if (e.currentTarget.currentFrame == 4)
e.currentTarget.gotoAndStop(3);
}
// function toggleClick-------------------------------------------------------------
function toggleClick(e:MouseEvent) {
// On MouseEvent gotoAndStop(Frame Number)
if (e.currentTarget.currentFrame == 2)
{
e.currentTarget.gotoAndStop(3);
e.currentTarget.bstate = 1;
}
if (e.currentTarget.currentFrame == 4)
{
e.currentTarget.gotoAndStop(1);
e.currentTarget.bstate = 0;
}
// trace statements -------------------------------------------------------
//trace("movieClip Instance Name = " + e.currentTarget); // [object Comp]
//trace(houseArray[e.currentTarget.name]); // comp
trace("using currentTarget: " + e.currentTarget.name); // comp
//trace("powerData: " + powerData); // power of user data
//trace("houseArray: " + houseArray[0]); // the 0 index of house array
// trace(e.currentTarget.power); // currentTarget's power************
//trace ("bstate in click function: " + e.currentTarget.bstate);
//objName = e.currentTarget.name;
trace("target: " + e.currentTarget);
bstatus = e.currentTarget.bstate;
userInput = e.currentTarget.power;
objSelect = e.currentTarget;
trace("objSelect: " + objSelect);
// end of trace statements -------------------------------------------------
calcPower(userInput, bstatus);
//trace("i am bstatus: " + bstatus);
//trace("i am power: " + userInput);
populateFields(bstatus);
sendPhp();
//trackItems(objSelect, bstatus);
} // end of function toggleClick ----------------------------------------------------
// function calcPower ------------------------------------------------------------------
function calcPower(userInput:int, bstatus:int):void
{
//trace("i arrived");
//trace("power: " + userInput + " and " + "bstate: " + bstatus);
if (bstatus == 0) {
trace("i have been clicked OFF");
currentPower -= userInput; // if OFF then minus
trace("currentPower: " + currentPower);
} else if (bstatus == 1) {
trace("i have been clicked ON");
currentPower += userInput; // if OFF then minus
trace("currentPower: " + currentPower);
}
}
// function populateFields ------------------------------------------------------------------
function populateFields(bstatus:int)
{
var displayText:String="";
var textFields:Array = new Array();
textFields[0] = compTxt.text;
textFields[1] = bathLightTxt.text;
textFields[2] = computerLightTxt.text;
textFields[3] = kitchenLight.text;
textFields[4] = livingLightTxt.text;
textFields[5] = descLightTxt.text;
textFields[6] = laundryLightTxt.text;
textFields[3] = ovenTxt.text;
textFields[8] = tvTxt.text;
textFields[9] = WasherTxt.text;
if (bstatus == 0) {
// textfield will say OFF
} else if (bstatus == 1) {
// textfield will say ON
}
/*
for (var i:int=0;i<textFields.length;i++)
{
// do something
}
*/
}
// function pwrPercentage ------------------------------------------------------------------
/*function pwrPercentage()
{
}
*/
// function trackItems ------------------------------------------------------------------
function trackItems(objSelect:Object, bstatus:int):void
{
trace("i arrived in trackItems");
trace("tracing objSelect in function trackItems: " + objSelect);
//trace("tracing Array onList :" + onList);
//trace("power: " + userInput + " and " + "bstate: " + bstatus);
if (bstatus == 0) {
//onList.removeItemAt(onList.getItemIndex(objSelect));
// remove from ON list (array) pop
// call function removeArrayItem
removeArrayItem(objSelect);
} else if (bstatus == 1) {
//trace("adding items to list");
onList.push(objSelect);
//add to ON list (array) push
}
trace("Array:" + onList);
//return array .... only have one array...contain objects that are ONLY ON
}
// function removeArrayItem ------------------------------------------------------------------
function removeArrayItem(objSelect:Object):void
{
var arrayLength:int = onList.length;
// Searches item in array
for (var i:int=0; i<arrayLength; i++)
{
// Finds item and removes it
if (onList[i] == objSelect)
{
onList.splice(i, 1);
///*/**/*/trace("Item Removed: " + onList[i]);
trace("Array Updated: " + onList);
}
}
}
// function frameloop ------------------------------------------------------------------
/* function frameloop(e:Event)
{
}
*/
// function sendPhp ------------------------------------------------------------------
function sendPhp(e:MouseEvent):void
{
// send vars to php
var request:URLRequest = new URLRequest("write_xml.php"); // the php file to send data to
var variables:URLVariables = new URLVariables(); // create an array of POST vars
variables['bathLight'] = compTxt.text;
variables['bathLight'] = bathLightTxt.text;
variables['computer'] = computerLightTxt.text;
variables['kitchenLight'] = kitchenLight.text;
variables['livingLight'] = livingLightTxt.text;
variables['descLight'] = descLightTxt.text;
variables['laundryLight'] = laundryLightTxt.text;
variables['oven'] = ovenTxt.text;
variables['tv'] = tvTxt.text;
variables['washer'] = washerTxt.text;
request.data = variables; // send the vars to the data property of the requested url (our php file)
request.method = URLRequestMethod.POST; // use POST as the send method
try
{
var sender:URLLoader = new URLLoader();
sender.load(request); // load the php file and send it the variable data
navigateToURL(new URLRequest("vars.xml"), '_blank'); //show me the xml
}
catch (e:Error)
{
trace(e); // trace error if there is a problem
}
}
// function called when user clicks on update button-----------------------------------
/*function updateStage():void
{
for (var i:int = 0; i<=onList.length;i++) {
addChild(onList[i]);
}
}*/
} //end of class
} // end of package
You will be better off keeping track of the reference to your textboxes in order to populate them afterwards:
var textFields:Array = new Array();
textFields[0] = compTxt;
textFields[1] = bathLightTxt;
textFields[2] = computerLightTxt;
textFields[3] = kitchenLight;
textFields[4] = livingLightTxt;
textFields[5] = descLightTxt;
textFields[6] = laundryLightTxt;
textFields[3] = ovenTxt;
textFields[8] = tvTxt;
textFields[9] = washerTxt;
I tried it out this way:
import fl.controls.*;
var textFields:Array = new Array(9);
textFields[0] = new TextInput();
textFields[1] = new TextInput();
textFields[2] = new TextInput();
textFields[3] = new TextInput();
textFields[4] = new TextInput();
textFields[5] = new TextInput();
textFields[6] = new TextInput();
textFields[7] = new TextInput();
textFields[8] = new TextInput();
textFields[9] = new TextInput();
var i:uint = 0;
for(i = 0; i < textFields.length; ++i){
textFields[i].text = "Set text at " + i;
trace(textFields[i].text);
}

Resources