c++
What is the difference between getting &buffer and &buffer[0]?
How does serial read and write really work?
cu vs. tty?
How to use STD
search paths
header search paths
/usr/local/Cellar/boost/1.52.0/include/
non-recursive
library search path
/usr/local/Cellar/boost/1.52.0/lib
recursive
other linking flags
-lboost_regex-mt
-L/usr/local/Cellar/boost/1.52.0/lib
=======
Regex
include c++0x
int // 32 bits
short int // 16 bits
unsigned
Struct
struct product {
int weight;
float price;
};
product something;
something.weight;
Vector
.push_back
array
int something [5];
int something [5] = { 3, 4, 5}
extern
tell compiler this variable is declared elsewhere.
Vectors
Unlike arrays, storage is handled automatically, expanded and contracted as needed.
Stack
Last in first out (lifo)
List
Sequence container Ordered following a sequence. In order to get to a position, must iterate from beginning or end
Singleton
class Log {
public:
static Log& Instance() {
static Log theLog;
return theLog;
}
void Write(char const *logline);
bool SaveTo(char const *filename);
private:
Log(); // ctor is hidden
Log(Log const&); // copy ctor is hidden
Log& operator=(Log const&); // assign op is hidden
static std::list<std::string> m_data;
};
[Read more][]
[Read more]: http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-I/4/#PR3d1CTSfUXK2E0r.99
Classes
class ExtendedClass {
virtual void Start() = 0;
}; // DON'T FORGET SEMICOLON
class Test : public ExtendedClass {
private: (is default)
protected:
public:
Test (int n, int y);
void Start() {
}
};
Test something (3, 5);
ExtendedClass[] my extendedclasses { new Test(), new Test2}
Enumerators
enum actions {nothing, walking, working}
Switch
switch(something) {
case 1:
break;
default:
break;
}
&x
*x
char * x
offset operator
[] is a dereferencer that offsets by number in brackets!
p[4] == *(p+4);
arrays
void pointers
void * ptr;
- can point to any hardware address, but can not dereference directly. - You can use if (x == sizeof(char)) to see if it's a char and then char * ptr = (char*) void_ptr; to cast it - can be used for generics
pointers to functions allows references functions to be sent as variables, dereferenced and ran int doSomething(int x, int y, int (* something)(int,int)) { return (* something)(x, y); doSomething(x,y, add); }
Reference
- Don't have to dereference a reference, just pointers.
- references are constant pointers, can not be reassigned!
- must be initialized
- can not increment through memory
int & something = x;
something = 3;
templates are generics
template <class identifier> function_declaration;
template <typename identifier> function_declaration;
template <class T>
T GetMax (T a, T b) {}
T result = (a>b)? a : b;
return (result);
}
int k = GetMax<int>(1,2)
long r = GetMax<int>(1,2)
#define PI 3.14159
const
can not be modified after declaration
return 0; // SUCCESS
return -1; // fail
std::list<std::string> data;
std::vector<Game_State*> mStateStack;
#include <iostream>
Byte
stores 8 bit unsigned int
uint8_t
Reset issue
http://stackoverflow.com/questions/11385915/serial-interface-initialisation-with-arduino-and-c
Oh my god, this is the most annoying thing in the world.
http://www.easysw.com/~mike/serial/serial.html
Comprehensive.
http://en.wikipedia.org/wiki/SerialPeripheralInterface_Bus
http://wiring.org.co/learning/tutorials/C++/
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284666138/all
O_RDONLY - read only
O_WRONLY - write only
O_RDWR - read and write
O_NONBLOCK - non blocking
O_NOCTTY - don't make controlling
fd - common name for variable "file descriptor"
tcsetattr - set attributes
TCSANOW - TC set attributes now
termios - unix api for terminal i/o
c_cflags - control flags
c_lflags - local flags
readv?
Arduino
http://www.instructables.com/id/Sending-a-multi-byte-integer-to-Arduinos-serial/
stringOne = String(45, HEX);
// prints "2d", which is the hexadecimal version of decimal 45:
Serial.println(stringOne);
String(255, BIN);
Example:
int fd;
ssize_t len;
unsigned char buf[8192]; // 8 kb (use a multiple of 512)
struct termios tio;
fd = open( "/dev/tty.usbmodem411", O_RDONLY | O_NOCTTY | O_NONBLOCK );
cfmakeraw(&tio);
cfsetispeed(&tio,B9600);
cfsetospeed(&tio,B9600);
tcsetattr(fd,TCSANOW,&tio);
for(;;) {
len = read( fd, &buf[0], 8192 );
std::cout << len;
if( len > 0 ) write(1,buf,len);
sleep(1);
}
so annoying that I can't just take the buffer and read it how I want...
Switch case is in protected scope
C++ is confused. Either don't initialize variables in a case, or wrap it in brackets in the case.