Skip to content

Commit

Permalink
added digitalout functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasif committed Jul 30, 2018
1 parent 233ce4f commit 7192f3b
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 2 deletions.
213 changes: 212 additions & 1 deletion EasyAndee.cpp
Expand Up @@ -4,4 +4,215 @@ Author: Muhammad Hasif
The Easy Andee Library is to allow users to use a feature similar to Arduino's Firmata to control their Arduino using their Bluetooth enabled phone. Users no longer need to code the UI and logic. With the Easy Andee feature on the Andee App, users can create their UI and immediately control their electronics.
This library is free software. This library also uses parts of the Arduino Firmata Library */
This library is free software */

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include <SPI.h>
#include <EasyAndee.h>

#define RX_DELAY 50
#define RX_MAX 20
#define TX_MAX 20

unsigned int SS_PIN = 8;

char txBuffer[TX_MAX];
char rxBuffer[RX_MAX];

/****************************************************************************
* EasyAndee Functions
*****************************************************************************/

void EasyAndeeBegin()
{
pinMode(SS_PIN,OUTPUT);
digitalWrite(SS_PIN,HIGH);
#if defined(__SAM3X8E__)
SPI.begin(SS_PIN);
SPI.setClockDivider(SS_PIN, 21);
#else
SPI.begin();
#endif

delay(800);
}
void EasyAndeeBegin(int pin)
{
SS_PIN = pin;
EasyAndeeBegin();
}
void setName(const char* name)
{
char limit[33];
if(strlen(name) > 32)
{
memcpy(limit,name,32);
limit[32] = '\0';
sendAndee(2,(char*)limit);//SET_ANDEE_NAME = 2
}
else
{
sendAndee(2,(char*)name);//SET_ANDEE_NAME = 2
}
}


void EasyAndeePoll()
{
pollRx(rxBuffer);
switch(rxBuffer[1])
{
case EASYANDEE_D_OUT:
//processDOut();
pinMode(rxBuffer[2] - 97,OUTPUT);
digitalWrite(rxBuffer[2] - 97,rxBuffer[3]-48);
break;

case EASYANDEE_D_IN:
processDIn();
break;

case EASYANDEE_A_OUT:
processAOut();
break;

case EASYANDEE_A_IN:
processAIn();
break;
default:
Serial.println("Unknown Command");
break;
}
}

void processDIn()
{

}

void processAOut()
{
char analogBuffer[4];
analogBuffer[0] = rxBuffer[3];
analogBuffer[1] = rxBuffer[4];
analogBuffer[2] = rxBuffer[5];
analogBuffer[3] = '\0';

int analogValue = strtol(analogBuffer,NULL,10);

if(rxBuffer[2] == 'd')//pin 3
{
pinMode(3,OUTPUT);
analogWrite(3,analogValue);
}
else if(rxBuffer[2] == 'f')//pin 5
{
pinMode(5,OUTPUT);
analogWrite(5,analogValue);
}
else if(rxBuffer[2] == 'g')//pin 6
{
pinMode(6,OUTPUT);
analogWrite(6,analogValue);
}
else if(rxBuffer[2] == 'j')//pin 9
{
pinMode(9,OUTPUT);
analogWrite(9,analogValue);
}
}

void processAIn()
{

}

/****************************************************************************
* Function to send and receive data to and from PIC32 using SPI
*****************************************************************************/
void resetRX()
{
memset(rxBuffer,0x00,RX_MAX);
}

void spiSendData(char* txBuffer, size_t bufferLength){
unsigned int txCount = 0;

SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));

digitalWrite(SS_PIN,LOW);
for(txCount = 0;txCount < bufferLength;txCount++)//send whole buffer
{
SPI.transfer(txBuffer[txCount]);//transfer and receive 1 char in SPI
delayMicroseconds(40);
}
digitalWrite(SS_PIN,HIGH);

SPI.endTransaction();
delay(10);
}

bool pollRx(char* buffer)
{
unsigned int rxCount = 0;
unsigned char tempChar;
resetRX();
delay(3);
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));

digitalWrite(SS_PIN,LOW);
for(int i = 0;i<RX_DELAY;)
{
tempChar = SPI.transfer(0x00);
if(tempChar > 32)
{
if(tempChar == ']')
{
//Serial.print("pollRx:");Serial.println(buffer);
buffer[rxCount] = '\0';

digitalWrite(SS_PIN,HIGH);
SPI.endTransaction();

delay(5);
return true;
break;
}
else if(tempChar == 173)
{
//Serial.println("pollRx: No Reply");
digitalWrite(SS_PIN,HIGH);
SPI.endTransaction();

delay(5);
return false;
break;
}
else
{
buffer[rxCount++] = tempChar;
}
}
else
{
i++;
}
}
digitalWrite(SS_PIN,HIGH);

SPI.endTransaction();
delay(5);
return false;
}

void sendAndee(unsigned char andeeCommand,char* message){
memset(txBuffer,0x00,TX_MAX);
sprintf(txBuffer,"#99#%d#%s;",andeeCommand,message);

spiSendData( txBuffer,strlen(txBuffer) );
}
49 changes: 48 additions & 1 deletion EasyAndee.h
Expand Up @@ -4,4 +4,51 @@ Author: Muhammad Hasif
The Easy Andee Library is to allow users to use a feature similar to Arduino's Firmata to control their Arduino using their Bluetooth enabled phone. Users no longer need to code the UI and logic. With the Easy Andee feature on the Andee App, users can create their UI and immediately control their electronics.
This library is free software. This library also uses parts of the Arduino Firmata Library */
This library is free software. This library also uses parts of the Arduino Firmata Library */

#if defined (__arm__)
#include "itoa.h"
#endif

#define EASYANDEE_D_IN 'E'
#define EASYANDEE_D_OUT 'D'
#define EASYANDEE_A_IN 'B'
#define EASYANDEE_A_OUT 'A'

#define REPLY_A_IN 'C'
#define REPLY_D_IN 'F'

/* #define PIN_0 'a';
#define PIN_1 'b';
#define PIN_2 'c';
#define PIN_3 'd';
#define PIN_4 'e';
#define PIN_5 'f';
#define PIN_6 'g';
#define PIN_7 'h';
#define PIN_8 'i';
#define PIN_9 'j';
#define PIN_10 'k';
#define PIN_11 'l';
#define PIN_12 'm';
#define PIN_13 'n';
#define PIN_A0 'o';
#define PIN_A1 'p';
#define PIN_A2 'q';
#define PIN_A3 'r';
#define PIN_A4 's';
#define PIN_A5 't'; */

void EasyAndeeBegin();
void EasyAndeeBegin(int);
void setName(const char*);
void EasyAndeePoll();
void processDIn();
void processAIn();
void processAOut();


void resetRX();
void spiSendData(char*, size_t);
bool pollRx(char*);
void sendAndee(unsigned char,char*);

0 comments on commit 7192f3b

Please sign in to comment.