Create instances dynamically

This commit is contained in:
Kristian Sloth Lauszus 2013-06-10 00:07:14 +02:00
parent b7d86a2f21
commit 2f04cc56ac
3 changed files with 10 additions and 33 deletions

View file

@ -8,18 +8,13 @@
#include <PS3BT.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
PS3BT PS3_1(&Btd);
PS3BT PS3_2(&Btd);
//PS3BT PS3_3(&Btd); // You can create as many instances as you like, but it will take up a lot of RAM!!
PS3BT* PS3[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like
PS3BT* PS3[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
const uint8_t length = sizeof(PS3)/sizeof(PS3[0]); // Get the lenght of the array
boolean printAngle[length];
void setup() {
PS3[0] = &PS3_1; // This will point to the first controller
PS3[1] = &PS3_2; // This will point to the second controller
//PS3[2] = &PS3_3; // You only need to uncomment this if you wanted to use another controller
for(uint8_t i=0;i<length;i++)
PS3[i] = new PS3BT(&Btd); // Create the instances
Serial.begin(115200);
if (Usb.Init() == -1) {
@ -32,7 +27,6 @@ void loop() {
Usb.Task();
for(uint8_t i=0;i<length;i++) {
if(!PS3[i]) continue; // Skip if it hasn't been defined
if(PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) {
if(PS3[i]->getAnalogHat(LeftHatX) > 137 || PS3[i]->getAnalogHat(LeftHatX) < 117 || PS3[i]->getAnalogHat(LeftHatY) > 137 || PS3[i]->getAnalogHat(LeftHatY) < 117 || PS3[i]->getAnalogHat(RightHatX) > 137 || PS3[i]->getAnalogHat(RightHatX) < 117 || PS3[i]->getAnalogHat(RightHatY) > 137 || PS3[i]->getAnalogHat(RightHatY) < 117) {
Serial.print(F("\r\nLeftHatX: "));

View file

@ -7,24 +7,14 @@
#include <SPP.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
// This will set the name to the defaults: "Arduino" and the pin to "1234" for both connections
SPP SPP_1(&Btd); // This will allow you to communicate with two SPP devices simultaneously
SPP SPP_2(&Btd);
//SPP SPP_3(&Btd); // You can create as many instances as you like, but it will take up a lot of RAM!!
// You can also set the name and pin like so
//SPP SerialBT(&Btd, "Lauszus's Arduino","0000");
SPP* SerialBT[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like
SPP* SerialBT[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
const uint8_t length = sizeof(SerialBT)/sizeof(SerialBT[0]); // Get the lenght of the array
boolean firstMessage[length] = { true }; // Set all to true
uint8_t buffer[50];
void setup() {
SerialBT[0] = &SPP_1; // This will point to the first instance
SerialBT[1] = &SPP_2; // This will point to the second instance
//SerialBT[2] = &SPP_3; // You only need to uncomment this if you wanted to use another instance
for(uint8_t i=0;i<length;i++)
SerialBT[i] = new SPP(&Btd); // This will set the name to the default: "Arduino" and the pin to "1234" for all connections
Serial.begin(115200);
if (Usb.Init() == -1) {
@ -51,7 +41,7 @@ void loop() {
if(Serial.available()) {
delay(10); // Wait for the rest of the data to arrive
uint8_t i = 0;
while(Serial.available()) // Read the data
while(Serial.available() && i < sizeof(buffer)) // Read the data
buffer[i++] = Serial.read();
/*
Set the connection you want to send to using the first character

View file

@ -8,19 +8,13 @@
#include <Wii.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
//WII Wii(&Btd,PAIR); // You will have to pair each controller with the dongle before you can define the instances like below
WII Wii_1(&Btd);
WII Wii_2(&Btd);
//WII Wii_3(&Btd); // You can create as many instances as you like, but it will take up a lot of RAM!!
WII* Wii[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like
WII* Wii[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
const uint8_t length = sizeof(Wii)/sizeof(Wii[0]); // Get the lenght of the array
bool printAngle[length];
void setup() {
Wii[0] = &Wii_1;
Wii[1] = &Wii_2;
//Wii[2] = &Wii_3; // You only need to uncomment this if you wanted to use another controller
for(uint8_t i=0;i<length;i++)
Wii[i] = new WII(&Btd); // You will have to pair each controller with the dongle before you can define the instances like so, just add PAIR as the second argument
Serial.begin(115200);
if (Usb.Init() == -1) {
@ -33,7 +27,6 @@ void loop() {
Usb.Task();
for(uint8_t i=0;i<length;i++) {
if(!Wii[i]) continue; // Skip if it hasn't been defined
if(Wii[i]->wiimoteConnected) {
if(Wii[i]->getButtonClick(HOME)) { // You can use getButtonPress to see if the button is held down
Serial.print(F("\r\nHOME"));