Skip to content

Commit

Permalink
Merge pull request #26 from dhhagan/dev
Browse files Browse the repository at this point in the history
updated examples and tests and added new function for resetting firmw…
  • Loading branch information
dhhagan committed Jun 6, 2018
2 parents 4366076 + 1b89c2e commit d097667
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 60 deletions.
Binary file modified .DS_Store
Binary file not shown.
135 changes: 118 additions & 17 deletions examples/tests/tests.cpp
Expand Up @@ -13,13 +13,129 @@ PMData pm;
ConfigVars vars;
ConfigVars2 vars2;

char incomingChar;

//forward declarations
void printInstructions();

void setup(){
delay(2000);
Serial.begin(9600);

while (!Serial.isConnected()){
Particle.process();
}

delay(1000);

printInstructions();
}

void loop() {

while (Serial.available()) {
incomingChar = Serial.read();

Serial.println("");

switch (incomingChar) {
case 'h':
printInstructions();
break;
case '?':
printInstructions();
break;
case '+': {
int k = alpha.on();
Serial.printlnf("ON? %d", k);
break;
}
case '-': {
int k = alpha.off();
Serial.printlnf("OFF? %d", k);
break;
}
case 'i':
Serial.println(alpha.read_information_string());
break;
case 's': {
Status s = alpha.read_status();
String r = "Status";
r += "\n\tfanON = " + String(s.fanON);
r += "\n\tlaserON = " + String(s.laserON);
r += "\n\tfanDAC = " + String(s.fanDAC);
r += "\n\tlaserDAC = " + String(s.laserDAC);

Serial.println(r);
break;
}
case 'f': {
Firmware fw = alpha.read_firmware_version();

Serial.println(String::format("v%d.%d", fw.major, fw.minor));
break;
}
case 'v': {
alpha.set_firmware_version();
Serial.println(String::format("fw v%d.%d", alpha.firm_ver.major, alpha.firm_ver.minor));
break;
}
case 'w': {
int r = alpha.toggle_fan(true);

Serial.println("Fan ON = " + String(r));
break;
}
case 'x': {
int r = alpha.toggle_fan(false);

Serial.println("Fan OFF = " + String(r));
break;
}
case 'y': {
int r = alpha.toggle_laser(true);

Serial.println("Laser ON = " + String(r));
break;
}
case 'z': {
int r = alpha.toggle_laser(false);

Serial.println("Laser OFF = " + String(r));
break;
}
case '\n':
break;
default:
Serial.println("Invalid command. Press 'h' or '?' for help.");
}
}
}


void printInstructions() {
String helpMessage = "Command Options:";
helpMessage += "\n\th/?: Print Instructions";
helpMessage += "\n\t+: OPC.on()";
helpMessage += "\n\t-: OPC.off()";
helpMessage += "\n\ti: OPC.read_information_string()";
helpMessage += "\n\ts: OPC.read_status()";
helpMessage += "\n\tf: OPC.read_firmware()";
helpMessage += "\n\tv: OPC.set_firmware_version()";
helpMessage += "\n\tw: OPC.toggle_fan(true)";
helpMessage += "\n\tx: OPC.toggle_fan(false)";
helpMessage += "\n\ty: OPC.toggle_laser(true)";
helpMessage += "\n\tz: OPC.toggle_laser(false)";

// Print out the instructions
Serial.println(helpMessage);
};
/*
// Test the Firmware methods
Serial.println("Testing Firmware Methods");
delay(1000);
// Test the Info String
Serial.print("\t.read_information_string()");
Serial.println("\t" + alpha.read_information_string());
Expand Down Expand Up @@ -162,19 +278,4 @@ void setup(){
Serial.println("\t" + String(alpha.off()));
Serial.println("\n\nTests are now complete!\n\n");


}

void loop(){
// If firmware >= v18, try just using the PM read
if (alpha.firm_ver.major >= 18){
delay(3000);

pm = alpha.read_pm_data();

Serial.print("\tPM1: "); Serial.println(pm.pm1);
Serial.print("\tPM1.5: "); Serial.println(pm.pm25);
Serial.print("\tPM10: "); Serial.println(pm.pm10);
}
}
*/
2 changes: 1 addition & 1 deletion library.properties
@@ -1,5 +1,5 @@
name=opcn2
version=1.2.1
version=1.3.0
license=MIT
author=David H Hagan <dhagan@mit.edu>
sentence=A simple library to operate the Alphasense OPC-N2 using a Particle Photon/Electron
Expand Down
87 changes: 45 additions & 42 deletions src/opcn2.cpp
Expand Up @@ -10,7 +10,7 @@ OPCN2::OPCN2(uint8_t chip_select)
SPI.begin(_CS);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);
SPI.setClockSpeed(1000000);
SPI.setClockSpeed(500000);

// Set the firmware version
_fv = this->read_information_string().replace(".", "").trim().substring(24, 26).toInt();
Expand All @@ -28,6 +28,23 @@ OPCN2::OPCN2(uint8_t chip_select)
}
}

void OPCN2::set_firmware_version( void )
{
// Set the firmware version in case it previously failed
// Set the firmware version
_fv = this->read_information_string().replace(".", "").trim().substring(24, 26).toInt();

if (_fv < 18) {
this->firm_ver.major = _fv;
this->firm_ver.minor = 0;
} else {
Firmware tmp = this->read_firmware_version();

this->firm_ver.major = tmp.major;
this->firm_ver.minor = tmp.minor;
}
}

uint16_t OPCN2::_16bit_int(byte LSB, byte MSB)
{
// Combine two bytes into a 16-bit unsigned int
Expand Down Expand Up @@ -167,35 +184,27 @@ struct Status OPCN2::read_status()
Status data;
byte vals[4];

if (this->firm_ver.major < 18) {
data.fanON = -999;
data.laserON = -999;
data.fanDAC = -999;
data.laserDAC = -999;
}
else {
// Read the status
digitalWrite(this->_CS, LOW);
SPI.transfer(0x13);
digitalWrite(this->_CS, HIGH);
// Read the status
digitalWrite(this->_CS, LOW);
SPI.transfer(0x13);
digitalWrite(this->_CS, HIGH);

delay(10);
delay(10);

// Send the read command and build the array of data
digitalWrite(this->_CS, LOW);
for (int i = 0; i < 4; i++){
vals[i] = SPI.transfer(0x13);
delayMicroseconds(4);
}
// Send the read command and build the array of data
digitalWrite(this->_CS, LOW);
for (int i = 0; i < 4; i++){
vals[i] = SPI.transfer(0x13);
delayMicroseconds(4);
}

digitalWrite(this->_CS, HIGH);
digitalWrite(this->_CS, HIGH);

// Calculate the values!
data.fanON = (unsigned int)vals[0];
data.laserON = (unsigned int)vals[1];
data.fanDAC = (unsigned int)vals[2];
data.laserDAC = (unsigned int)vals[3];
}
// Calculate the values!
data.fanON = (unsigned int)vals[0];
data.laserON = (unsigned int)vals[1];
data.fanDAC = (unsigned int)vals[2];
data.laserDAC = (unsigned int)vals[3];

return data;
}
Expand All @@ -207,24 +216,18 @@ struct Firmware OPCN2::read_firmware_version()
// $ alpha.read_firmware_version()
Firmware res;

if (this->firm_ver.major < 18) {
res.major = -999;
res.minor = -999;
}
else {
// Read the Firmware version
digitalWrite(this->_CS, LOW);
SPI.transfer(0x12);
digitalWrite(this->_CS, HIGH);
// Read the Firmware version
digitalWrite(this->_CS, LOW);
SPI.transfer(0x12);
digitalWrite(this->_CS, HIGH);

delay(10);
delay(10);

digitalWrite(this->_CS, LOW);
res.major = (unsigned int)SPI.transfer(0x00);
delayMicroseconds(4);
res.minor = (unsigned int)SPI.transfer(0x00);
digitalWrite(this->_CS, HIGH);
}
digitalWrite(this->_CS, LOW);
res.major = (unsigned int)SPI.transfer(0x00);
delayMicroseconds(4);
res.minor = (unsigned int)SPI.transfer(0x00);
digitalWrite(this->_CS, HIGH);

return res;
}
Expand Down
1 change: 1 addition & 0 deletions src/opcn2.h
Expand Up @@ -184,6 +184,7 @@ class OPCN2
Firmware firm_ver;

// methods
void set_firmware_version();
bool ping();
bool on();
bool off();
Expand Down

0 comments on commit d097667

Please sign in to comment.