Originally Posted by
Salem
In the .h file you include everywhere you need it, you put
extern SerialPort serialPort;
In exactly ONE .cpp file, you put
SerialPort serialPort(4800);
Sorry I am trying to understand your post, but I think I do not understand it, could you give some random example.
I put extern SerialPort serialPort; ,in every .h file where it is used (used in .cpp file of course):
Code:
#ifndef _DOWN_JOYSTICK_STATE_H_
#define _DOWN_JOYSTICK_STATE_H_
#include "JoystickState.h"
#include "Joystick.h"
#include "IdleJoystickState.h"
extern SerialPort serialPort;
class DownJoystickState : public JoystickState
{
private:
static JoystickState* sInstance;
public:
static JoystickState* getInstance();
void handleCoordinates(int nX, int nY, Joystick& joystick);
};
#endif
Code:
#ifndef _LEFT_JOYSTICK_STATE_H_
#define _LEFT_JOYSTICK_STATE_H_
#include "JoystickState.h"
#include "Joystick.h"
#include "IdleJoystickState.h"
extern SerialPort serialPort;
class LeftJoystickState : public JoystickState
{
private:
static JoystickState* sInstance;
public:
static JoystickState* getInstance();
void handleCoordinates(int nX, int nY, Joystick& joystick);
};
#endif
Code:
#ifndef _JOYSTICK_H_
#define _JOYSTICK_H_
#include "JoystickState.h"
#include "IdleJoystickState.h"
#define POSITIONS_LENGTH 4
#define MAX_X_POSITION 1000
#define MIN_X_POSITION 20
#define MAX_Y_POSITION 1000
#define MIN_Y_POSITION 20
#define MODE_KORISTI 0
#define MODE_POSTAVI 1
#define INVALID_PARAMETER -1
#define IN_DEADZONE 0
#define NOT_IN_DEADZONE 1
extern SerialPort serialPort;
class Joystick
{
private:
uint8_t currentMode;
JoystickState* currentState;
public:
JoystickState* position_states[POSITIONS_LENGTH];
public:
Joystick();
void setMode(int newMode);
int getMode();
Joystick& operator=(const Joystick& other);
friend bool operator==(const Joystick& joystickOne, const Joystick& joystickTwo);
void handlePositions(void);
int8_t getZone(int nX, int nY);
bool isPasswordSet(void);
void changeState(JoystickState* newState);
};
#endif // !_JOYSTICK_H_
etc...
Also in main.cpp I instantiated an object serialPort(4600); before all the includes, but now I get serialPort was not declared in this scope and serialPort does not name a type errors
Code:
#ifndef F_CPU
#define F_CPU 1000000UL
#endif
#include "SerialPort.h"
SerialPort serialPort(4600);
#include "avr/io.h"
#include "avr/interrupt.h"
#include <util/delay.h>
#include <stdlib.h>
#include "i2cmaster.h"
#include "I2C.h"
#include "new.h"
#include "AnalogConverter.h"
// This include uses serialPort object
#include "Joystick.h"
Joystick joystick;
void start(void)
{
uint8_t nKeyPressed;
Joystick tempJoystick;
serialPort.write( "Enter the key: ");
//cin >> nKeyPressed;
if (nKeyPressed == 'P')
{
serialPort.write( "Postavi Mode running!");
joystick.setMode(MODE_POSTAVI);
}
else if (nKeyPressed == 'K')
{
serialPort.write("Koristi Mode running!");
joystick.setMode(MODE_KORISTI);
}
tempJoystick.handlePositions();
if (joystick.getMode() == MODE_POSTAVI)
{
// Copy values from one object to another
joystick = tempJoystick;
}
else if (joystick.getMode() == MODE_KORISTI)
{
// Check if user entered right combination
if (joystick == tempJoystick)
{
serialPort.write("Suitcase opened");
}
else
{
serialPort.write("Invalid combination");
}
}
else
{
// Handle error
}
serialPort.write("");
}
//AnalogConverter analog;
int main (void)
{
DDRC = 0xFF;
DDRA &= ~(1 << PINA0);
uint16_t readVal;
AnalogConverter_Init();
while (true)
{
readVal = AnalogConverter_Read(0);
if (readVal != CONVERTER_NO_NEW_DATA)
{
serialPort.write("Analog Read");
serialPort.write(readVal);
serialPort.write("");
}
else
{
serialPort.write("No new data from ADC");
}
}
}
EDIT:
I forgot to add #include "SerialPort.h" in every .h file where I wrote extern SerialPort serialPort; ... It compiled, thank you, gonna check now if it works!