为 Arduino 编写新库后,编译器无法识别这个新类并返回错误,
BinaryCounter:15:错误:“二进制”未命名类型
以下是产生错误的文件。类在 中定义Binary.h
、实现Binary.cpp
和使用BinaryCounter.ino
二进制.h
#ifndef Binary_h
#define Binary_h
#include "Arduino.h"
class Binary {
public:
Binary (int pins[], int numPins);
void display(int number);
void clear();
private:
boolean *_values;
int *_pins;
int _numPins;
void _update();
void _setValues (int num);
};
#endif
二进制文件
#include "Binary.h"
Binary::Binary(int pins[], int numPins) {
_values = new boolean[numPins];
_pins = pins;
_numPins = numPins;
for (int i = 0; i < _numPins; i ++) {
pinMode(_pins[i], OUTPUT);
_values[i] = false;
}
_update();
}
void Binary::_update() {
for (int i = 0; i < _numPins; i ++) {
if (_values[i]) {
digitalWrite(_pins[i], HIGH);
} else {
digitalWrite(_pins[i], LOW);
}
}
}
void Binary::_setValues (int num) {
for (int i = 0; i < _numPins; i ++, num/=2) {
_values[i] = ((num%2)==1);
}
}
void Binary::display (int number) {
_setValues(number);
_update();
}
void Binary::clear() {
display(0);
}
和 BinaryCounter.ino
#include <Binary.h>
int pins[] = {2,3,4,5,6};
int numPins = 5;
Binary dis(pins, numPins);
void setup() {
}
void loop() {
int loopEnd = 1<<numPins;
for (int i = 0; i < loopEnd; i ++) {
dis.display(i);
delay(200);
}
}