Search code examples
c++scopehandlerinterruptdeclare

'accelerometer' and 'lcd' not declared in this scope


It's telling me that "accelerometer" and "lcd" were not declared in this scope when they're used in PIT0_IRQHandler. None of the solutions I've found online have worked as of yet so I figured I'd just post it up. I'm pretty new to this as well so feel free to point out any and every error included

    #include <stdio.h>
    #include "system.h"
    #include "derivative.h"
    #include "hardware.h"
    #include "delay.h"
    #include "lcd.h"
    #include "i2c.h"
    #include "level.h"
    #include "mma845x.h"
    #include "tone.h"
    #include "accel.h"

    /// LCD dimensions and centerpoint
    #define LCD_WIDTH  (LCD_X_MAX-LCD_X_MIN)
    #define LCD_HEIGHT (LCD_Y_MAX-LCD_Y_MIN)
    #define CENTRE_X   ((LCD_X_MAX-LCD_X_MIN)/2)
    #define CENTRE_Y   ((LCD_Y_MAX-LCD_Y_MIN)/2)

    #define BACKGROUND_COLOUR (RED)
    #define FOREGROUND_COLOUR (WHITE)
    #define CIRCLE_RADIUS (20)

    using namespace USBDM;

    void initialisePIT(int channel, uint32_t interval) {
        // Enable clock to PIT
        SIM->SCGC6  |= SIM_SCGC6_PIT_MASK;
        // Enable PIT module
        PIT->MCR     = PIT_MCR_FRZ_MASK;
        // Set reload value
        PIT->CHANNEL[channel].LDVAL  = interval-1;
        // Enable this channel with interrupts
        PIT->CHANNEL[channel].TCTRL = PIT_TCTRL_TEN_MASK|PIT_TCTRL_TIE_MASK;
        // Enable PITn interrupts in NVIC
        NVIC_EnableIRQ((IRQn_Type)(PIT0_IRQn+channel));
        // Set arbitrary priority level
        NVIC_SetPriority((IRQn_Type)(PIT0_IRQn+channel), 8);
    }

    void initialiseLCD(){
        // Instantiate SPI interface class
        Spi *spi = new Spi0();
        // Instantiate LCD interface class
        Lcd *lcd = new Lcd(spi);
        // Clear Background and set to a preset (Set above) colour
        lcd->clear(BACKGROUND_COLOUR);
        // Draw crosshairs and initial circle on the screen
        lcd->drawLine(LCD_WIDTH/2,LCD_WIDTH/2,0,LCD_HEIGHT,BLACK);
        lcd->drawLine(0,LCD_WIDTH,LCD_HEIGHT/2,LCD_HEIGHT/2,BLACK);
        lcd->drawCircle(CENTRE_X, CENTRE_Y, 20, WHITE);
    }

    void initialiseAccel()
    {
        I2c *i2c = new I2c0();
        MMA845x *accelerometer = new MMA845x(i2c, MMA845x::ACCEL_2Gmode);
    }

    void PIT0_IRQHandler(void) {
        int accelStatus;
        int16_t accelX,accelY,accelZ;
        accelerometer->readAccelerometerXYZ(&accelStatus, &accelX, &accelY, &accelZ);

        lcd->clear(BACKGROUND_COLOUR);
        lcd->drawCircle(accelX,accelY,20,WHITE);

        // Clear the interrupt request from PIT
        PIT->CHANNEL[0].TFLG = PIT_TFLG_TIF_MASK;
    }

Solution

  • accelerometer is declared in initialiseAccel. It will go out of scope at the end of that function, and is not a known name in PIT0_IRQHandler. You should probably create a class to hold those variables and functions, or you'll have to use global variables (possibly in a namespace) to hold those values.