Compiling error in Arduino library - c++

I'm having a strange error compiling a sketch in Arduino, probably due to a mistake in a library I've created.
here's the code:
#include <BlueHartwin.h>
BlueHartwin bH(7,8,115200);
byte incomingByte;
void noOp(void)
{
Serial<<"noOp"<<endl;
};
void leftWindowDown(void)
{
Serial<<"leftWindowDown"<<endl;
};
void setup() {
Serial.begin(115200);
bH.registerCmd(0,&noOp);
bH.registerCmd(1,&leftWindowDown);
}
void loop() {
if(bH.available())
{
incomingByte = bH.read()-48;
Serial<<incomingByte<<endl;
if (incomingByte<nrOfCommands)
bH.runCmd(incomingByte);
}
}
Here are the header of the library:
#ifndef BlueHartwin_H
#define BlueHartwin_H
#include <SoftwareSerial.h>
#include <Streaming.h>;
#include <Arduino.h>
#define nrOfCommands 255
typedef void (* CmdFuncPtr) (); // this is a typedef to command functions
class BlueHartwin {
public:
BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate);
~BlueHartwin();
//void setup(byte bluetoothTx,byte bluetoothRx,long baudRate);
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
boolean unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr);
boolean runCmd(byte id);
void setDataLength(byte dataLength);
byte getDataLength();
boolean available();
byte read();
private:
CmdFuncPtr setOfCmds[255];
byte mDataLength;
};
#endif
and source
#include "BlueHartwin.h";
SoftwareSerial bluetooth(7,8);
byte mDataLength;
BlueHartwin::BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate){
bluetooth = SoftwareSerial(bluetoothTx,bluetoothRx);
bluetooth.begin(baudRate);
}
BlueHartwin::~BlueHartwin(){
}
boolean BlueHartwin::registerCmd(byte id,CmdFuncPtr cmdFuncPtr){
if ((id<=nrOfCommands)&&(setOfCmds[id]==0)) {
setOfCmds[id]=cmdFuncPtr;
return true;
} else return false;
}
boolean BlueHartwin::unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr){
if (id<=nrOfCommands) {
setOfCmds[id]=0;
return true;
} else return false;
}
boolean BlueHartwin::runCmd(byte id){
if ((id<=nrOfCommands)&&(setOfCmds[id]!=0)) {
setOfCmds[id]();
return true;
} else return false;
}
void setDataLength(byte dataLength) {
mDataLength=dataLength;
}
byte getDataLength(){
return mDataLength;
}
boolean available(){
return true;
}
byte read(){
return 0;
}
This is the error I get:
sketch/Test_Bluehartwin00.ino.cpp.o: In function `loop':
/home/paolo/Projects/arduino/sketches/TESTS/Test_Bluehartwin00/Test_Bluehartwin00.ino:25: undefined reference to `BlueHartwin::available()'
/home/paolo/Projects/arduino/sketches/TESTS/Test_Bluehartwin00/Test_Bluehartwin00.ino:27: undefined reference to `BlueHartwin::read()'
collect2: error: ld returned 1 exit status
exit status 1
The strange thing is that I get error only in 2 of the public function of the library class.
I really can't understand what's wrong....
I'm using Arduino UNO and IDE 1.6.8 under Ubuntu 14.04 64 bit. Normally everything runs fine.
Thanks for help in advance.

The problem is in the .cpp file.
The two functions giving you a problem are here:
boolean available(){
return true;
}
byte read(){
return 0;
}
As these are, they are free/global functions. You need to make them part of the class:
boolean BlueHartwin::available(){
return true;
}
byte BlueHartwin::read(){
return 0;
}
Note: you'll also have to modify a few others also:
void BlueHartwin::setDataLength(byte dataLength) {
mDataLength=dataLength;
}
byte BlueHartwin::getDataLength(){
return mDataLength;
}
These can be fixed the same way.

Related

Communicate with Advantech ADAM-6050 through a c++ program

I'm trying to develop a program in c++ that allows reading and setting the logical value of the input and output of the adam 6050.
A practical example: I want the code to read the logical value of DI4 and if it is 1, it sets the logical value of DO4 to 1.
What library do I need to include?
What code do I have to write?
I tried to compile this example code from Adam .net class library samples:
// Adam50XXDIO.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "ADSDIO.h"
#include <string.h>
//////////////////////////////////////////////
//Take ADAM-5056 for example
//Slot : 0
//Reverse channel 0 value
USHORT iSlot = 0;
USHORT iChannel = 0;
//////////////////////////////////////////////
int ADV_SUCCESS = 0;
LONG myprocHandle;
int OpenLib()
{
myprocHandle = NULL;
//OpenDeviceLib
if(ADAMDrvOpen(&myprocHandle) == ADV_SUCCESS)
return ADV_SUCCESS;
else
return -99;
}
void CloseLib()
{
//Close device library
if(myprocHandle!=NULL)
ADAMDrvClose(&myprocHandle);
}
int main(int argc, char* argv[])
{
int iRet;
ULONG dVal;
bool bValue = false;
bool setValue = true;
printf("/*** Adam50XXDIO Sample ***/ \n");
iRet = OpenLib();
if(iRet == ADV_SUCCESS)
{
//Get dio module values
if((DI_GetValues(myprocHandle, iSlot, &dVal)) == ADV_SUCCESS)
{
printf("GetValues: %lx\n", dVal);
bValue = ((dVal & (0x00000001<<iChannel)) > 0);
if(bValue)
{
printf("Ch%d:True\n", iChannel);
setValue = false;
}
else
{
printf("Ch%d:False\n", iChannel);
setValue = true;
}
//Set DO value
if((DO_SetValue(myprocHandle, iSlot, iChannel, setValue) == ADV_SUCCESS))
{
printf("SetValue done.\n", dVal);
//Check value
if((DI_GetValues(myprocHandle, iSlot, &dVal)) == ADV_SUCCESS)
{
printf("GetValues: %lx\n", dVal);
bValue = ((dVal & (0x00000001<<iChannel)) > 0);
if(bValue)
printf("Ch%d:True\n", iChannel);
else
printf("Ch%d:False\n", iChannel);
}
}
else
printf("SetValue() failed.");
}
else
printf("GetValues() failed.");
}
else
printf("OpenLib(%d) failed.\n", iRet);
CloseLib();
printf("/*** END ***/ \n");
getchar();
return 0;
}
But I get this message:
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe
cannot open output file C:\Program Files (x86)\Advantech\AdamApax.NET Class Library\Sample Code\ADAM\Win32\CPlusPlus\ADAM-PAC-Sample\Adam50XX_DIO\Adam50XX_DIO.exe: Permission denied
Then I changed the path, and now pop these messages:
C:\Users\JOOLOP~1\AppData\Local\Temp\cc5WBolj.o Adam50XX_DIO.cpp:(.text+0x18): undefined reference to `ADAMDrvOpen'
C:\Users\JOOLOP~1\AppData\Local\Temp\cc5WBolj.o Adam50XX_DIO.cpp:(.text+0x51): undefined reference to `ADAMDrvClose'
C:\Users\JOOLOP~1\AppData\Local\Temp\cc5WBolj.o Adam50XX_DIO.cpp:(.text+0xb3): undefined reference to `DI_GetValues'
C:\Users\JOOLOP~1\AppData\Local\Temp\cc5WBolj.o Adam50XX_DIO.cpp:(.text+0x17a): undefined reference to `DO_SetValue'
C:\Users\JOOLOP~1\AppData\Local\Temp\cc5WBolj.o Adam50XX_DIO.cpp:(.text+0x1c5): undefined reference to `DI_GetValues'
C:\practice\Adam50XX_DIO\collect2.exe [Error] ld returned 1 exit status
What I can do now?

C++ Static Function/Namespace issue

I am having issues with some code. In the CrossingSensor::loop() function, there is a line with: sonar.ping_timer(readSensor);.
I can call that fine, but in that function I can't access the status variable. What am I missing?
Error: error: 'status' was not declared in this scope status = on;
/*
Name
*/
#ifndef CrossingSensor_h
#define CrossingSensor_h
#include "Arduino.h"
class CrossingSensor
{
enum Status {off, on};
public:
CrossingSensor();
void init();
void loop();
void start();
void stop();
Status status;
private:
unsigned long _startMillis;
};
#endif
/*
NLE
*/
#include "Arduino.h"
#include "CrossingSensor.h"
#include <NewPing.h>
NewPing sonar(2,3,6);//trigger, echo, max distance
unsigned int pingSpeed = 500;// 50ms would be 20 times a second
unsigned long pingTimer;// Holds the next ping time
CrossingSensor::CrossingSensor()
{
Serial.println("CrossingSensor: Init");
//init();
}
void CrossingSensor::init()
{
Serial.println("CrossingSensor: Init/Attach/Reset");
pingTimer = millis();
}
void readSensor()
{
if (sonar.check_timer())
{
int cm = sonar.ping_result / US_ROUNDTRIP_CM;
if(cm > 0 && cm < 10)
{
//status = on;
}
else
{
//status = off;
}
}
}
void CrossingSensor::loop()
{
if (millis() >= pingTimer)
{
pingTimer += pingSpeed;
sonar.ping_timer(readSensor);
}
}
void CrossingSensor::start()
{
if(status == off)
{
Serial.println("CrossingSensor: Turning Sensor On");
status = on;
}
}
void CrossingSensor::stop()
{
if(status == on)
{
Serial.println("CrossingSensor: Turning Sensor Off");
status = off;
}
}
You never declared the function readSensor() as a part of CrossingSensor. Since it isn't a member function, it has no access to status.
All you should have to do is declare the function as a member of CrossingSensor or pass it a CrossingSensor object.
You would also need to change the .cpp file to reflect that change. The declaration for each would look like the following:
class CrossingSensor
{
enum Status {off, on};
public:
CrossingSensor();
void init();
void readSensor();
void loop();
void start();
void stop();
Status status;
private:
unsigned long _startMillis;
};
#endif
This is your method declaration:
void CrossingSensor::readSensor()
{
// your code here
}
This makes readSensor a part of CrossingSensor as well as the other way around.
The readSensor() function isn't part of CrossingSensor so it can't see CrossingSensor's status.
Make it part of Crossingsensor by declaring it there in your header file, and then define it as void CrossingSensor::readSensor(), in your code, and not as void readSensor().
Also, I don't know what sonar.ping_timer() takes as an argument, but I doubt it is readSensor. You will have to find another way of passing status to sonar.ping_timer(), if that is what you intended to do.

Issue with null key/value's when accessing a map/vector from a callback generated from ESP32 bluetooth library

I am trying to fire a function every time an ESP32 node receives a certain char. I can receive the data and parse the char out of a packet structure I have made. I register all my actions in a map of type <char,callback_function>, where
typedef void (*callback_function)(void);
The idea being that I link all the chars to their respective functions. I can then parse a char out and find the pointer to the function I want to fire easily. The only downside is I have to keep a reference to a global object to access this map from within the bluetooth_callback. I've been testing this for almost 2 days now, and I've written some example code without the espressif APIs. On its own, my code seems to work fine and it runs on the ESP32 aswell. But if I let the bluetooth_callback get called after registering it with the espressif API (ie. let a real bluetooth event to fire the callback), the behavior becomes undefined. The size of the map is still correct with the number of pairs I have added to it, but all of the values and keys are null or undefined, and if I try to access them I get a Guru Meditation Error. I have been trying to solve this problem a lot, and the behaviour is very strange to me. If I call the callback manually then everything seems to work, but if I let it get called by a bluetooth event, I get undefined behavior and crashes. I'm not sure if I am doing something wrong c++ wise or if I this is an issue specific to the esp32. I could be making a dumb mistake. I have attached my Arduino script, the header and cpp files of the library I was working on, and a sample application that should show what I am trying to do. Thank you for your help.
Hardware:
Board: ESP32 Dev Module (Sparkfun ESP32 thing)
Core Installation version: 1.0.3
IDE name: Arduino IDE
Flash Frequency: 40Mhz
PSRAM enabled: no
Upload Speed: 115200
Computer OS: Windows 10
Backtrace
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x400014e8 PS : 0x00060430 A0 : 0x800d2410 A1 : 0x3ffcf1a0
A2 : 0x000000fe A3 : 0x000000fc A4 : 0x000000ff A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x800d2228 A9 : 0x3ffcf170
A10 : 0x3ffbed90 A11 : 0x3f40148b A12 : 0x3f40148b A13 : 0x0000ff00
A14 : 0x00ff0000 A15 : 0xff000000 SAR : 0x00000010 EXCCAUSE: 0x0000001c
EXCVADDR: 0x000000fc LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffb
Backtrace: 0x400014e8:0x3ffcf1a0 0x400d240d:0x3ffcf1b0 0x400d242d:0x3ffcf1d0 0x400d14a2:0x3ffcf1f0 0x400d1501:0x3ffcf210 0x400df881:0x3ffcf230 0x400d8362:0x3ffcf280 0x4008d83d:0x3ffcf2b0
Decoded:
PC: 0x400014e8
EXCVADDR: 0x000000fc
Decoding stack results
0x400d240d: Print::write(char const*) at C:\Users\Jake Booth\Documents\ArduinoData\packages\esp32\hardware\esp32\1.0.3\cores\esp32/Print.h line 66
0x400d242d: Print::print(char const*) at C:\Users\Jake Booth\Documents\ArduinoData\packages\esp32\hardware\esp32\1.0.3\cores\esp32\Print.cpp line 89
0x400d14a2: NodeESP::act_message(char*) at C:\Users\Jake Booth\Documents\Arduino\libraries\NodeESP\NodeESP.cpp line 336
0x400d1501: NodeESP::bluetooth_callback(esp_spp_cb_event_t, esp_spp_cb_param_t*) at C:\Users\Jake Booth\Documents\Arduino\libraries\NodeESP\NodeESP.cpp line 176
0x400df881: btc_spp_cb_handler at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/bt/bluedroid/btc/profile/std/spp/btc_spp.c line 152
0x400d8362: btc_task at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/bt/bluedroid/btc/core/btc_task.c line 110
0x4008d83d: vPortTaskWrapper at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/freertos/port.c line 143
I've highlighted line 336 below to show you easily which one it is.
Arduino Sketch
#include "NodeESP.h"
#define LED_PIN 5
#define TOGGLE_LED_CHAR 'l'
#define TURN_ON_LED_CHAR 'p'
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED_PIN,OUTPUT);
char l = TOGGLE_LED_CHAR;
char p = TURN_ON_LED_CHAR;
esp_spp_cb_param_t ex_param;
ex_param.data_ind.data = (uint8_t*)l;
ex_param.data_ind.len = 1;
//ex_param_ptr = *ex_param;
NodeESP node_esp;
node_esp.add_func(&p, toggle_LED);
node_esp.add_func(&l, turn_on_LED);
node_esp.begin("TEST_1");
//Just calling it manually works, but when the espressif API calls it I get undefined behaviour.
// NodeESP::bluetooth_callback(ESP_SPP_DATA_IND_EVT,&ex_param);
}
void toggle_LED()
{
Serial.println("toggle_LED()");
}
void turn_on_LED()
{
Serial.println("turn_on_LED()");
}
void loop() {
// put your main code here, to run repeatedly:
while(1)
{
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
NodeESP.h
/*
NodeESP.cpp - Library for automatically connecting to and using esp32 microcontrollers
Created by Jacob Booth 11/9/2020
*/
#ifndef NodeESP_h
#define NodeESP_h
#include "Arduino.h"
#include <ESPmDNS.h>
#include <WiFi.h>
#include <map>
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_spp_api.h"
#include "esp_gap_bt_api.h"
//---Define buffer constants--------------------------
#define RCV_BUF_SIZE 128
#define TX_BUF_SIZE 256
//---Define communication constants-------------------
#define RFCOMM_CHANNEL 1
#define RFCOMM_MAX_FRAME_SIZE 0xffff
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! please run 'make menuconfig' to enable it
#endif
class NodeESP
{
public:
//Constructor
NodeESP();
//Begin
bool begin(const char *_name);
//Typedef
typedef void (*callback_function)(void);
//Variables
uint32_t bt_handle;
const char *_name;
std::map<char*, callback_function> funcs;
char rx_buf[RCV_BUF_SIZE];
char tx_buf[RCV_BUF_SIZE];
WiFiServer server;
WiFiClient client;
//Generic
void add_func(char* command, callback_function func);
void act_message(char* trigger);
//Bluetooth
void bluetooth_write(char *buf, int len);
bool bluetooth_spp_init();
bool bluetooth_init(const char *deviceName);
static char parse_data(uint8_t *rcvd, uint16_t len);
static void bluetooth_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
String toString(const IPAddress & address);
private:
};
#endif
NodeESP.cpp
/*
NodeESP.cpp - Library for automatically connecting to and using esp32 microcontrollers
Created by Jacob Booth 11/9/2020
*/
#include "Arduino.h"
#include <ESPmDNS.h>
#include <WiFi.h>
#include <map>
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_spp_api.h"
#include "esp_gap_bt_api.h"
#include "NodeESP.h"
using namespace std;
//---State Machine Variables---
//State enum that helps the state machine track its way through
//connecting to wifi.
enum state_t {
UNDEFINED,
DISCONNECTED,
WIFI_CON,
SERVER_CON
};
enum comm_t {
s,
NONE,
WIFI,
BLUETOOTH,
q
};
//The current communication protocol we are using
comm_t cur_comm = s;
//The current state
state_t cur_state = UNDEFINED;
char tx_buf[TX_BUF_SIZE];
char rx_buf[RCV_BUF_SIZE];
//---Wifi
WiFiServer server(0);
WiFiClient client;
//---Bluetooth
uint32_t bt_handle;
void* example_obj;
//---Main Init--------------------------------------------------------
NodeESP::NodeESP()
{
this->_name = "default";
example_obj = (void*)this;
}
bool NodeESP::begin(const char *_name)
{
this->_name = _name;
//For testing set the current communication method to bluetooth
cur_comm = BLUETOOTH;
switch (cur_comm)
{
case UNDEFINED:
return false;
break;
case NONE:
return false;
break;
case WIFI:
return true;
break;
case BLUETOOTH:
if (!this->bluetooth_init(_name))
{
return false;
}
break;
}
return true;
}
//---Bluetooth Functions--------------------------------
bool NodeESP::bluetooth_init(const char *deviceName)
{
//Start Bluetooth
if (!btStart())
{
return false;
}
//Start BlueDroid (bluetooth stack)
if (esp_bluedroid_init()!= ESP_OK)
{
return false;
}
//Enable BlueDroid
if (esp_bluedroid_enable()!= ESP_OK)
{
return false;
}
//Set device name
esp_bt_dev_set_device_name(deviceName);
//Set sannable and discoverable
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
//Init spp server
if (!this->bluetooth_spp_init())
{
return false;
}
//Set send bytes to the correct callback
//send_bytes = write_bluetooth;
return true;
}
bool NodeESP::bluetooth_spp_init()
{
if (esp_spp_register_callback(&bluetooth_callback) != ESP_OK)
{
return false;
}
if (esp_spp_init(ESP_SPP_MODE_CB) != ESP_OK)
{
return false;
}
if (esp_spp_start_srv(ESP_SPP_SEC_NONE, ESP_SPP_ROLE_SLAVE, 1, "") != ESP_OK)
{
return false;
}
return true;
}
void NodeESP::bluetooth_write(char *buf, int len)
{
esp_spp_write(bt_handle, len, (uint8_t*)buf);
}
void NodeESP::bluetooth_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
switch (event)
{
case ESP_SPP_SRV_OPEN_EVT:
{
NodeESP* self = (NodeESP*) example_obj;
self->bt_handle = param->srv_open.handle;
break;
}
case ESP_SPP_CL_INIT_EVT:
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_NONE);
break;
case ESP_SPP_OPEN_EVT:
break;
case ESP_SPP_DATA_IND_EVT:
{
NodeESP* self = (NodeESP*) example_obj;
//char trig = self->parse_data(param->data_ind.data, param->data_ind.len);
self->act_message((char*)param->data_ind.data);
break;
}
case ESP_SPP_CLOSE_EVT:
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
case ESP_SPP_WRITE_EVT:
break;
case ESP_SPP_CONG_EVT:
break;
}
return;
}
//---Parsing functions-------------------------------
//TODO: Add circular buffer
char NodeESP::parse_data(uint8_t *rcvd, uint16_t len)
{
char body_buf[RCV_BUF_SIZE];
uint8_t len_buf[2];
uint8_t rcvd_bytes = 0;
uint8_t recive_stage = 0;
uint16_t calc_len = 0;
//Loop through each char recieved
for (uint16_t i = 0; i < len; i++)
{
uint8_t cur_char = rcvd[i];
rcvd_bytes++;
//If we haven't recieved the length or body
//And we recieve a start delim
if (recive_stage == 0 && cur_char == 0x7E)
{
//We are ready to read the length of the message (next 2 bytes)
recive_stage = 1;
rcvd_bytes = 0;
}
else if (recive_stage == 1)//If we are recieving the length
{
//Next two bytes get stored in the length buffer
len_buf[rcvd_bytes - 1] = cur_char;
//If we have recieved the 2 length bytes
if (rcvd_bytes == 2)
{
//Decode the length
calc_len = ((uint16_t)len_buf[0]) << 8 | (uint16_t)len_buf[1];
//Ready to recieve the body
recive_stage = 2;
rcvd_bytes = 0;
}
}
else if (recive_stage == 2)//If we are recieving the body
{
//Store the recieved bytes
body_buf[rcvd_bytes - 1] = cur_char;
if (rcvd_bytes == calc_len)
{
//Reset
rcvd_bytes = 0;
recive_stage = 0;
return(cur_char);
}
}
}
return '/';
}
void NodeESP::add_func(char* command, callback_function func)
{
Serial.println("Added new callback...");
std::pair<char*, callback_function>* new_pair = new std::pair<char*, callback_function>(command,func);
this->funcs.insert(*new_pair);
}
void NodeESP::act_message(char* trigger)
{
Serial.println("[act message]");
Serial.print("funcs.size() = ");
Serial.print(this->funcs.size()); //Always returns the correct size all of the time
Serial.println("");
for (std::map<char*, callback_function>::iterator it = this->funcs.begin();
it != this->funcs.end();
++it)
{
Serial.print("funcs[i].first = ");
Serial.print(it->first); //Line 336, accessing the first element of the iterator
Serial.println(""); //This works properly when I don't call the function through the callback.
if (trigger == it->first)
{
(it->second)();
}
}
}
String NodeESP::toString(const IPAddress & address)
{
return String(address[0]) + "." + address[1] + "." + address[2] + "." + address[3];
}
Example Program
Here is an example program that shows what I want to happen and that it works correctly.
// Example program
#include <iostream>
#include <string>
#include <map>
//Global object to access the instance from within static function
using namespace std;
class example
{
public:
example();
void begin();
typedef void (*callback_function)(void);
std::map<char, callback_function> funcs;
void add_func(char command, callback_function func);
void do_something(char trigger);
static void example_callback(char cb);
};
void* example_obj;
example::example()
{
example_obj = (void*)this;
}
void example::begin()
{
//Set up everything to do with external library, peripherals, ect...
//Register the callback with the library
//register_callback(&example_callback);
}
//Static func that does something
void example::example_callback(char cb)
{
//Have to do this because the args need to match the library's requirements
example* self = (example*) example_obj;
//Do some processing...
self->do_something(cb);
}
//Add a function pointer and its command char to the vector
void example::add_func(char command, callback_function func)
{
std::pair<char, callback_function>* new_pair = new std::pair<char, callback_function>(command,func);
this->funcs.insert(*new_pair);
}
//This function is called from the callback
void example::do_something(char trigger)
{
cout << "funcs.size() = "<< (this->funcs.size())<<"\n"; //Always returns the correct size all of the time
for (std::map<char, callback_function>::iterator it = this->funcs.begin();
it != this->funcs.end();
++it)
{
cout << "funcs[i].first = "<< (it->first)<<"\n";
if (trigger == it->first)
{
(it->second)();
}
}
}
void callback()
{
cout << "Callback Called!";
}
int main()
{
cout << "Starting program...\n";
cout << "Creating example object...\n";
example ex;
cout << "Adding some callbacks...\n";
ex.add_func('x',callback);
ex.add_func('y',callback);
ex.add_func('z',callback);
cout << "Firing callback...\n";
example::example_callback('z');
}

error: no match for 'operator=' in 'myFile = SDClass::open(const char*, uint8_t)(((const char*)"test.txt"), 19u)'

FILE.CPP: I am using this file and to use this I have created object "myFile"
/*
SD - a slightly more friendly wrapper for sdfatlib
This library aims to expose a subset of SD card functionality
in the form of a higher level "wrapper" object.
License: GNU General Public License V3
(Because sdfatlib is licensed with this.)
(C) Copyright 2010 SparkFun Electronics
*/
#include <SD.h>
/* for debugging file open/close leaks
uint8_t nfilecount=0;
*/
File::File(SdFile f,
const char *n) {
// oh man you are kidding me, new() doesnt exist? Ok we do it by hand!
_file=(SdFile *)malloc(sizeof(SdFile));
if (_file) {
memcpy(_file, &f, sizeof(SdFile));
strncpy(_name, n, 12);
_name[12]=0;
/* for debugging file open/close leaks
nfilecount++;
Serial.print("Created \"");
Serial.print(n);
Serial.print("\": ");
Serial.println(nfilecount, DEC);
*/
}
}
File::File(void) {
_file=0;
_name[0]=0;
//Serial.print("Created empty file object");
}
File::~File(void) {
// Serial.print("Deleted file object");
}
// returns a pointer to the file name
char *File::name(void) {
return _name;
}
// a directory is a special type of file
boolean File::isDirectory(void) {
return (_file && _file->isDir());
}
size_t File::write(uint8_t val) {
return write(&val, 1);
}
size_t File::write(const uint8_t *buf,
size_t size) {
size_t t;
if (!_file) {
setWriteError();
return 0;
}
_file->clearWriteError();
t=_file->write(buf,
size);
if (_file->getWriteError()) {
setWriteError();
return 0;
}
return t;
}
int File::peek() {
if (! _file) return 0;
int c=_file->read();
if (c !=-1) _file->seekCur(-1);
return c;
}
int File::read() {
if (_file) return _file->read();
return -1;
}
// buffered read for more efficient, high speed reading
int File::read(void *buf,
uint16_t nbyte) {
if (_file) return _file->read(buf, nbyte);
return 0;
}
int File::available() {
if (! _file) return 0;
uint32_t n=size() - position();
return n > 0X7FFF ? 0X7FFF: n;
}
void File::flush() {
if (_file) _file->sync();
}
boolean File::seek(uint32_t pos) {
if (! _file) return false;
return _file->seekSet(pos);
}
uint32_t File::position() {
if (! _file) return -1;
return _file->curPosition();
}
uint32_t File::size() {
if (! _file) return 0;
return _file->fileSize();
}
void File::close() {
if (_file) {
_file->close();
free(_file);
_file=0;
/* for debugging file open/close leaks
nfilecount--;
Serial.print("Deleted ");
Serial.println(nfilecount, DEC);
*/
}
}
File::operator bool() {
if (_file) return _file->isOpen();
return false;
}
I am trying to create a SD card read write package using Simulink. What happens is here is I would take SD.cpp and SD.h and other required files and create a wrapper file which consists of required operation (i.e. reading and writing to SD card) and I am getting the following errors
SDcard_wrapper.cpp: In function 'void SDcard_Update_wrapper(const boolean_T*, real_T*, const uint8_T*, int_T)':
./SDcard_wrapper.cpp:113: error: no match for 'operator=' in 'myFile = SDClass::open(const char*, uint8_t)(((const char*)"test.txt"), 19u)'
C:\Users\Samanth\Documents\MATLAB\newSD-trail/utility/SdFat.h:135: note: candidates are: SdFile& SdFile::operator=(const SdFile&)
./SDcard_wrapper.cpp:123: error: could not convert 'myFile' to 'bool'
Following is the SDcard_wrapper.cpp error locations:
myFile = SD.open("test.txt", FILE_WRITE); //This is the line where I am getting 1st error
if (myFile) // this is the point where I am getting the error: could not convert 'myFile' to 'bool'
{
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
File SDClass::open(const char* filepath, uint8_t mode) {
/*
Open the supplied file path for reading or writing.
The file content can be accessed via the `file` property of
the `SDClass` object--this property is currently
a standard `SdFile` object from `sdfatlib`.
Defaults to read only.
If `write` is true, default action (when `append` is true) is to
append data to the end of the file.
If `append` is false then the file will be truncated first.
If the file does not exist and it is opened for writing the file
will be created.
An attempt to open a file for reading that does not exist is an
error.
*/
int pathidx;
// do the interative search
SdFile parentdir = getParentDir(filepath, &pathidx);
// no more subdirs!
filepath += pathidx;
if (!filepath[0]) {
// it was the directory itself!
return File(parentdir, "/");
}
// Open the file itself
SdFile file;
// failed to open a subdir!
if (!parentdir.isOpen())
return File();
// there is a special case for the Root directory since its a static dir
if (parentdir.isRoot()) {
if (!file.open(SD.root, filepath, mode)) {
// failed to open the file :(
return File();
}
// dont close the root!
} else {
if (!file.open(parentdir, filepath, mode)) {
return File();
}
// close the parent
parentdir.close();
}
if (mode & (O_APPEND | O_WRITE))
file.seekSet(file.fileSize());
return File(file, filepath);
}
class SdFile : public Print // This is the line pointed above in the second error just for information SdFat.h:135: note: candidates are: SdFile& SdFile::operator=(const SdFile&)
{
public:
/** Create an instance of SdFile. */
SdFile(void): type_(FAT_FILE_TYPE_CLOSED) {}
/**
* writeError is set to true if an error occurs during a write().
* Set writeError to false before calling print() and/or write() and check
* for true after calls to print() and/or write().
*/
//bool writeError;
/**
* Cancel unbuffered reads for this file.
* See setUnbufferedRead()
*/
void clearUnbufferedRead(void) {
flags_ &= ~F_FILE_UNBUFFERED_READ;
}
uint8_t close(void);
uint8_t contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
uint8_t createContiguous(SdFile* dirFile, const char* fileName, uint32_t size);
/** \return The current cluster number for a file or directory. */
uint32_t curCluster(void) const {
return curCluster_;
}
/** \return The current position for a file or directory. */
uint32_t curPosition(void) const {
return curPosition_;
}
static void dateTimeCallback(void(*dateTime)(uint16_t* date, uint16_t* time)) {
dateTime_ = dateTime;
}
/**
* Cancel the date/time callback function.
*/
static void dateTimeCallbackCancel(void) {
// use explicit zero since NULL is not defined for Sanguino
dateTime_ = 0;
}

FMOD - Unhandled Exception with no source code avaiable

I'm trying to get my audio track to play using FMOD but I keep getting an unhandled exception and then it says there's no source code available, and shows me disassembly code.
main.cpp
bool AudioProject::initAudio()
{
// Audio code
fmSound = new Sound();
fmSound->initialise();
fmSound->load("Music/Rocky_Theme_Tune.mp3");
fmSound->play();
return true;
}
I put break points in the see where it stopped, which was in the initialise function. It even goes into the initialise function and then just randomly breaks. I think I have every include file for fmod as I used it last year no problem.
I'll post my sound.h/.cpp files too.
.h
#include "stdafx.h"
#pragma once
#include "fmod.hpp"
#include "fmod.h"
class Sound
{
private:
bool on; //is sound on?
bool possible; //is it possible to play sound?
char * currentSound; //currently played sound
//FMOD-specific stuff
FMOD_RESULT result;
FMOD_SYSTEM * fmodsystem;
FMOD_SOUND * sound;
FMOD_CHANNEL * channel;
public:
Sound();
~Sound();
void initialise (void);
void setVolume (float v);
void load (const char * filename);
void unload (void);
void play (bool pause = false);
bool getSound (void);
void setPause (bool pause);
void setSound (bool sound);
void toggleSound (void);
void togglePause (void);
};
.cpp
#include "stdafx.h"
#include "Sound.h"
#include "fmod.h"
#include "fmod.hpp"
Sound::Sound()
{
on = true; //is sound on?
possible = true; //is it possible to play sound?
currentSound=""; //currently played sound
sound=0;
}
Sound::~Sound()
{
}
//initialises sound
void Sound::initialise (void)
{
//create the sound system. If fails, sound is set to impossible
result = FMOD_System_Create(&fmodsystem);
if (result != FMOD_OK)
possible = false;
//if initialise the sound system. If fails, sound is set to impossible
if (possible)
result = FMOD_System_Init(fmodsystem,2, FMOD_INIT_NORMAL, 0);
if (result != FMOD_OK)
possible = false;
//sets initial sound volume (mute)
if (possible)
FMOD_Channel_SetVolume(channel,1.0f);
}
//sets the actual playing sound's volume
void Sound::setVolume (float v)
{
if (possible && on && v >= 0.0f && v <= 1.0f)
{
FMOD_Channel_SetVolume(channel,v);
}
}
//loads a soundfile
void Sound::load (const char * filename)
{
currentSound = (char *)filename;
if (possible && on)
{
result = FMOD_Sound_Release(sound);
result = FMOD_System_CreateStream(fmodsystem,currentSound, FMOD_SOFTWARE, 0, &sound);
if (result != FMOD_OK)
possible = false;
}
}
//frees the sound object
void Sound::unload (void)
{
if (possible)
{
result = FMOD_Sound_Release(sound);
}
}
//plays a sound (no argument to leave pause as dafault)
void Sound::play (bool pause)
{
if (possible && on)
{
result = FMOD_System_PlaySound(fmodsystem,FMOD_CHANNEL_FREE, sound, pause, &channel);
FMOD_Channel_SetMode(channel,FMOD_LOOP_NORMAL);
}
}
//toggles sound on and off
void Sound::toggleSound (void)
{
on = !on;
if (on == true)
{
load(currentSound);
play();
}
if (on == false)
{
unload();
}
}
//pause or unpause the sound
void Sound::setPause (bool pause)
{
FMOD_Channel_SetPaused (channel, pause);
}
//turn sound on or off
void Sound::setSound (bool s)
{
on = s;
}
//toggle pause on and off
void Sound::togglePause (void)
{
FMOD_BOOL p;
FMOD_Channel_GetPaused(channel,&p);
FMOD_Channel_SetPaused (channel,!p);
}
//tells whether the sound is on or off
bool Sound::getSound (void)
{
return on;
}
Hit a brick wall here, anyone have any ideas?
You are calling FMOD_Channel_SetVolume(channel,1.0f) in initialise, but the channel variable isn't hasn't been initialized yet, it gets initialized by the FMOD_System_PlaySound(fmodsystem,FMOD_CHANNEL_FREE, sound, pause, &channel); in Sound::play