2017-10-13 5 views
0

J'essaie de prendre ce qui est dans cet exemple donné par la bibliothèque Arduino Wire, et de l'appliquer à un programme que j'écris.Fonction Wire.onReceive() sur Arduino

Ceci est mon code. Les instructions Comm.NDP[] sont d'autres instances de classe non enregistrées dans ce fichier, donc je crois que vous pouvez les ignorer.

** 
* I2C.cpp handles sending of event messages 
* between the LORA and MEGA via I2C protocol. 
*/ 

#include "I2C.h" 
#include "DATA.h" 
#include "Globals.h" 
#include <Wire.h> 
#include <Arduino.h> 

/** 
* Constructor used to reference all other variables & functions. 
*/ 
I2C::I2C() { 
} 

/** 
* Assigns the proper address to the current micro controller. 
*/ 
void I2C::initialize() { 
    //Sets the address for the current micro controller. 
    // Mega - 0 
    // LoRa - 1 
    Wire.begin(0); 
    Wire.setClock(8000000); 
    //Registers recieveEvent as a interrupt. 
    Wire.onReceive(receiveEvent); // register event 
} 

/** 
* Receives byte over I2C Connection. 
*/ 
static void receiveEvent(int howmany) { 
    //Iterator used below. 
    int i = 0; 
    for(i=0;i<120;i++) { 
    Comm.NDP[i] = ' '; 
    } 
    //Resets iterator. 
    i = 0; 
    //Checks to see if serial port is empty. 
    while (1 < Wire.available()) { 
    //Reads in single character from serial port. 
    char character = Wire.read(); 
    NDP[i] = character; 
    i++; 
    } 
    Serial.println(Comm.NDP); 
} 

Le code exemple de Wire.h Arduino bibliothèque

#include <Wire.h> 

void setup() { 
    Wire.begin(8); // join i2c bus with address #8 
    Wire.onReceive(receiveEvent); // register event 
    Serial.begin(9600); // start serial for output 
} 

void loop() { 
    delay(100); 
} 

// function that executes whenever data is received from master 
// this function is registered as an event, see setup() 
void receiveEvent(int howMany) { 
    while (1 < Wire.available()) { // loop through all but the last 
    char c = Wire.read(); // receive byte as a character 
    Serial.print(c); // print the character 
    } 
    int x = Wire.read(); // receive byte as an integer 
    Serial.println(x); // print the integer 
} 

Je reçois cette erreur de l'Arduino IDE.

error: invalid use of non-static member function

Wire.onReceive(receiveEvent); // register event 

         ^

état de sortie 1 utilisation non valide de fonction membre non statique

Répondre

0

Vous êtes absent déclaration de receiveEvent avant la première utilisation. Soit déplacer sa définition avant begin ou y ajouter:

void receiveEvent(int howMany);