静态元素和成员元素的编译错误

compilation errors with static and member elements

提问人: 提问时间:3/23/2023 更新时间:3/23/2023 访问量:30

问:

我正在尝试使该程序在Arduino IDE中工作。 我收到消息: “错误:在静态成员函数中无效使用成员 XXX” 它使用一个类中定义的 2 个中断服务例程。它们必须被定义为静态的,这是有效的。但是,它会导致访问同一类中的非静态成员时出现问题。

我需要“静态”、“易失性”等的神奇组合才能使这项工作,谢谢!

interface_8255.ino

#include "intf_8255.h"

class intf_8255 my_intf;

void setup() {
  my_intf.init();
}

void loop() {
}

intf_8255.cpp

#include "intf_8255.h"

intf_8255::intf_8255() {}

//8255 to Redboard
void intf_8255::OBF_Isr(void) {
byte value;

  //PC6  /ACK   input from PC5
  //read data
  digitalWrite(ACK_out, LOW);
  
  // Setup bus as input
  DDRD = DDRD & 0x0F;   // clear bits 7-4
  DDRC = DDRD & 0xF0;   // clear bits 3-0

  value = (PINC & 0x0F) | (PIND & 0xF0);
  
  digitalWrite(ACK_out, HIGH);

  //put in circular buffer  
  if (Rx_8255.isFull()) {
    // volatile RxState = full
  }
  else
  {
    Rx_8255.push(value);
  }
}

//Redboard to 8255
void intf_8255::IBF_Isr(void) {
byte value;

  //get from circular buffer
  if (Tx_8255.isEmpty())
  {
    Wrote1stByte = false;
    return;
  }

  //write data (if not empty)
  value = Tx_8255.pop();
  
//  WriteData(value);  
}

void intf_8255::init() {  
  /* skip some code */
  attachInterrupt(digitalPinToInterrupt(OBF_int), OBF_Isr, FALLING);
  attachInterrupt(digitalPinToInterrupt(IBF_int), IBF_Isr, FALLING);

  Wrote1stByte = false;
}

void intf_8255::WriteData(byte value)
{
  // Setup bus as output
  DDRD = DDRD | 0xF0;   // set bits 7-4
  DDRC = DDRC | 0x0F;   // set bits 3-0

  PORTC = (PORTC & 0x30) | (value & 0x0F);
  PORTD = (PORTD & 0xF2) | (value & 0xF0);
  
  //PC4  /STB   input from PC4
  digitalWrite(STB_out, LOW);
  digitalWrite(STB_out, HIGH);

  // Setup bus as input
  DDRD = DDRD & 0x0F;   // clear bits 7-4
  DDRC = DDRD & 0xF0;   // clear bits 3-0
}

// blocking read
byte intf_8255::ReadFrom8255()
{
  while (Rx_8255.isEmpty()) /* wait */;

  return Rx_8255.pop();
}

void intf_8255::WriteTo8255(byte value)
{
  while (Tx_8255.isFull()) /* wait */;

  /* if empty send 1st byte*/
  if (Wrote1stByte)
  {
    Tx_8255.push(value);
  }
  else
  {
    WriteData(value);
    Wrote1stByte = true;
  }
}

intf_8255.h

#pragma once

#include <Arduino.h>
#include <CircularBuffer.h>

#define CIRCULAR_BUFFER_INT_SAFE

const byte OBF_int = 2;
const byte IBF_int = 3;
const byte ACK_out = A5;   //PC5 A5/D19
const byte STB_out = A4;   //PC4 A4/D18

class intf_8255
{
  private:
    CircularBuffer<byte, 256> Tx_8255;  
    CircularBuffer<byte, 256> Rx_8255;  

    volatile bool Wrote1stByte;
    
    static void OBF_Isr(void);
    static void IBF_Isr(void);

    void WriteData(byte value);
    
  public:
    intf_8255();
    
    void init();
    byte ReadFrom8255();
    void WriteTo8255(byte value);
};
C++ 静态 成员函数

评论

0赞 Some programmer dude 3/23/2023
OT:类(和结构)标签也是类型名。所以你不需要关键字。普通也应该同样有效。class intf_8255 my_intf;classintf_8255 my_intf;
4赞 Some programmer dude 3/23/2023
至于你的问题,成员函数不能访问非静态成员。静态成员是的成员,而非静态成员是单个对象(实例)的成员。我认为你的设计是有缺陷的。static
1赞 Retired Ninja 3/23/2023
您实际上有此类的多个实例还是只有一个实例?
0赞 molbdnilo 3/23/2023
#define CIRCULAR_BUFFER_INT_SAFE必须先去才能使其中断安全。#include <CircularBuffer.h>
1赞 molbdnilo 3/23/2023
不要沉迷于课程。如果只有一个实例有意义,那么一些对(封装)数据进行操作的自由函数有时是最佳选择。

答: 暂无答案