我試圖在Arduino中使用類對象,但是我一直在遇到問題。我想要做的就是聲明一個類並創建該類的一個對象。一個例子會是什麼?與Arduino一起使用類
回答
你能提供一個沒有奏效的例子嗎?正如您可能知道的那樣,Wiring language基於C/C++,但是,not all of C++ is supported。
無論您是否被允許在Wiring IDE中創建類,我都不確定(我的第一個Arduino現在在郵件中)。我知道如果你編寫了一個C++類,使用AVR-GCC編譯它,然後使用AVRDUDE將它加載到你的Arduino上,它就可以工作。
我之前創建了這個簡單的一個。我面臨的主要挑戰是創建一個良好的構建環境 - 一個makefile,可以編譯和鏈接/部署所有內容,而無需使用GUI。對於代碼,這裏是標題:
class AMLed
{
private:
uint8_t _ledPin;
long _turnOffTime;
public:
AMLed(uint8_t pin);
void setOn();
void setOff();
// Turn the led on for a given amount of time (relies
// on a call to check() in the main loop()).
void setOnForTime(int millis);
void check();
};
這裏是主要來源
AMLed::AMLed(uint8_t ledPin) : _ledPin(ledPin), _turnOffTime(0)
{
pinMode(_ledPin, OUTPUT);
}
void AMLed::setOn()
{
digitalWrite(_ledPin, HIGH);
}
void AMLed::setOff()
{
digitalWrite(_ledPin, LOW);
}
void AMLed::setOnForTime(int p_millis)
{
_turnOffTime = millis() + p_millis;
setOn();
}
void AMLed::check()
{
if (_turnOffTime != 0 && (millis() > _turnOffTime))
{
_turnOffTime = 0;
setOff();
}
}
它更嬌滴滴這裏格式:http://amkimian.blogspot.com/2009/07/trivial-led-class.html
用,我只是做這樣的事情在.pde文件:
#include "AM_Led.h"
#define TIME_LED 12 // The port for the LED
AMLed test(TIME_LED);
有an excellent tutorial on how to create a library for the Arduino platform。圖書館基本上是一個班,所以它應該告訴你它是如何完成的。
在Arduino的,你可以使用類,但也有一些限制:
- 沒有新的和刪除關鍵字
- 沒有例外
- 沒有的libstdC++,因此沒有標準功能,模板或類
您還需要爲您的課程創建新文件,您不能在主草圖中聲明它們。重新編譯庫時,您還需要關閉Arduino IDE。這就是爲什麼我使用Eclipse作爲我的Arduino IDE。
新的/刪除就好了Arduino 1.0.6 –
我的Webduino library都基於一個C++類,它在Arduino以太網盾之上實現了一個Web服務器。我在一個.h文件中定義了全部類,任何Arduino代碼都可以#include。隨意查看代碼,看看我是如何做到的......我最終只是將它全部內聯定義,因爲沒有真正的理由使用Arduino IDE單獨編譯對象。
在this page上,Arduino草圖定義了幾個Structs(加上一些方法),然後在設置循環和主循環中調用它們。即使對於像我這樣一個文盲程序員來說,也足夠簡單地解釋。
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230935955狀態:
默認情況下,Arduino的IDE和 庫不使用操作 new和operator delete。它支持malloc()和free(),它的確支持 。所以 的解決方案是爲你自己實現新的和 刪除操作符,使用 這些函數。
代碼:
#include <stdlib.h> // for malloc and free void* operator new(size_t size) { return malloc(size); } void operator delete(void* ptr) { free(ptr); }
這使你可以創建對象,例如
C* c; // declare variable
c = new C(); // create instance of class C
c->M(); // call method M
delete(c); // free memory
問候, tamberg
在Arduino的1.0,該編譯就好:
class A
{
public:
int x;
virtual void f() { x=1; }
};
class B : public A
{
public:
int y;
virtual void f() { x=2; }
};
A *a;
B *b;
const int TEST_PIN = 10;
void setup()
{
a=new A();
b=new B();
pinMode(TEST_PIN,OUTPUT);
}
void loop()
{
a->f();
b->f();
digitalWrite(TEST_PIN,(a->x == b->x) ? HIGH : LOW);
}
請注意,我沒有虛擬析構函數---建立動態對象似乎沒問題,但刪除可能導致碎片化,所以我不相信這是一個好主意不僅僅是動態配置您的環境。 –
- 1. 與Arduino一起使用iPhone LCD
- 2. 與Arduino一起工作Android
- 3. Arduino顯示器與PHP一起工作
- 4. 設置Xcode與Arduino一起工作
- 5. 與HC-6藍牙設備一起使用Arduino Lily Pad
- 6. 迷你直流電機無法與Arduino Uno一起使用?
- 7. 使用類與arduino的問題
- 8. Arduino系列與Debian一起工作正常,但與Raspbian掛起
- 9. BaseAdapter類與Json解析一起使用
- 10. 與靜態類一起使用LocalBroadcastManager
- 11. 與Sinatra一起使用自定義類
- 12. JavaBeans與複雜類型一起使用
- 13. 將RegisterHotKey API與NativeWindow類一起使用?
- 14. C#:與對象一起使用的類
- 15. 與靜態類一起使用arrayaccess
- 16. xcode:與NSTimers一起使用UIImageView子類
- 17. 將jQuery與ES6的類一起使用
- 18. 是否可以使用Arduino藍牙到Wifi中繼與信標一起使用?
- 19. Arduino SD卡在與另一個SPI設備一起使用時無法寫入
- 20. -forwardInvocation與Clang-LLVM一起使用,但不與GCC一起使用
- 21. Arduino的Pyserial問題 - 與Python shell一起使用,但不在程序中
- 22. Arduino Serial.read不起作用
- 23. arduino按鈕不起作用
- 24. 與使用多個css3風格的css類一起使用modernizr?
- 25. 嘗試使用CSS類與Nth-child選擇器一起使用
- 26. 與Kinect一起使用System.Speech
- 27. 與HTTPS一起使用javax.xml.ws.Endpoint
- 28. 與Eclemma一起使用Jmockit
- 29. 與Socket.io一起使用WebRTC
- 30. 與AVVideoCompositing一起使用GPUImage
你將如何通過'test'爲另一個函數中的參數?這種類的函數簽名是什麼樣子的?你沒有使用'新的AMLed(TIME_LED)',那麼'AMLed測試(TIME_LED)'看起來怎麼樣?我問,因爲我有兩個對象,都是'AMLed'類型,我想在函數中引用它們。 –