Curious as always, I thought I'd spend some time playing with C++ on my current project. This has an xMega chip.
I found "Jax Coder's" page at http://jaxcoder.com/Projects.asp... and blinking an LED seemed an appropriate first task for making a class.
I had to adapt his class to xMega by adapting the class:
CBlink.h
/* * CBlink.h * * Created: 7/20/2014 2:41:00 PM * Author: Tom */ #ifndef CBLINK_H_ #define CBLINK_H_ class CBlink { public: //Constructor with the last parameter set to false as default. CBlink(volatile PORT_t * port, uint8_t pin, bool initiallyLit=false); bool IsLedLit() { return m_isLedLit; } void SetLitState(bool state); void ToggleLitState(); private: bool m_isLedLit; volatile PORT_t * m_port; uint8_t m_pin; }; #endif /* CBLINK_H_ */
(Curiously, the clipboard has been working of late. Perhaps fixed by an MS update?)
CBlink.c
/* * CBlink.cpp * * Created: 7/20/2014 2:41:16 PM * Author: Tom */ #include#include "CBlink.h" CBlink::CBlink(volatile PORT_t * port, uint8_t pin, bool initiallyLit/* =false */) { //Set the port addresses for the DDR and PORT values m_port = port; //Set the DDRn.pin as output m_port->DIRSET = _BV(pin); m_pin = pin; m_isLedLit = initiallyLit; //Set initial state of the LED SetLitState(m_isLedLit); } void CBlink::SetLitState(bool state) { m_isLedLit = state; if (m_isLedLit) m_port->OUTCLR = _BV(m_pin); else m_port->OUTSET = _BV(m_pin); } void CBlink::ToggleLitState() { SetLitState(!m_isLedLit); }
Things went haywire when I tried to build the app, because there wasn't any _cplusplus.h Not knowing what else to do about it, I deleted the include statement, and my app builds and... egad, the led blinks!
/* * LearnC__01.cpp * * Created: 7/20/2014 2:37:23 PM * Author: Tom */ #define F_CPU 2000000 #include#include //#include "_cplusplus.h" #include "CBlink.h" int main(void) { CBlink blink(&PORTA,3); while(1) { blink.ToggleLitState(); _delay_ms(2000); } }
He includes what looks like the _cplusplus.h file, so I added that:
/* * _cplusplus.h * * Created: 7/20/2014 3:22:50 PM * Author: Tom */ #ifndef _CPLUSPLUS_H_ #define _CPLUSPLUS_H_ /* This is applicable if using virtual inheritance. */ __extension__ typedef int __guard __attribute__((mode (__DI__))); extern "C" int __cxa_guard_acquire(__guard *); extern "C" void __cxa_guard_release (__guard *); extern "C" void __cxa_guard_abort (__guard *); int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);}; void __cxa_guard_release (__guard *g) {*(char *)g = 1;}; void __cxa_guard_abort (__guard *) {}; /* This is applicable if using pure virtual inheritance. */ extern "C" void __cxa_pure_virtual(void); void __cxa_pure_virtual(void) {}; /* Operators required for C++ */ void* operator new(size_t size); void operator delete(void* size); void * operator new(size_t size) { return malloc(size); } void operator delete(void* ptr) { free(ptr); } #endif /* _CPLUSPLUS_H_ */
But when I try to include this code I don't understand, I get errors I don't understand. Strange, how that works, uMM?
Under Operators required for C++
/* Operators required for C++ */ void* operator new(size_t size);
The error points to size_t and says two things:
"size_t was not declared in this scope" which is pretty obvious that it doesn't know what a size_t is.
Um, so is there something more I need to know about using C++ in studio 6.2?
I believe it's now time for a Sunday Afternoon walk.