Last active
December 22, 2015 14:49
-
-
Save jumahe/68765c81ed7f73bdb1cc to your computer and use it in GitHub Desktop.
Arduino-like code for a Particle Photon project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int ssPin = D2; | |
String lastVal = ""; | |
void setup() | |
{ | |
// -- Serial begin | |
Serial.begin(9600); | |
// -- SPI init | |
pinMode(ssPin, OUTPUT); | |
digitalWrite(ssPin, HIGH); | |
SPI.begin(ssPin); | |
SPI.setClockSpeed(250, KHZ); // from the display specs (max 250KHz) | |
SPI.setDataMode(SPI_MODE0); // from the display specs | |
clearDisplaySPI(); | |
setDecimalsSPI(0b000000); | |
s7sSendStringSPI("-HI-"); // a first message | |
setBrightnessSPI(255); | |
// -- WebHook listener | |
Particle.subscribe("hook-response/fbLikesEvent", gotLikesData, MY_DEVICES); | |
// -- Wait a few seconds for init | |
for(int i=0;i<10;i++) | |
{ | |
Serial.println("waiting " + String(10-i) + " seconds before start."); | |
delay(1000); | |
} | |
// -- send the first request | |
Particle.publish("fbLikesEvent"); | |
} | |
// -- function called in return of the webhook call | |
void gotLikesData(const char *name, const char *data) | |
{ | |
String str = String(data); | |
Serial.println(str); // debug | |
int lenVal = str.length(); | |
// -- update the display only if the value has changed | |
if(str != lastVal && lenVal > 0) | |
{ | |
lastVal = str; | |
switch(lenVal) | |
{ | |
case 1: | |
str = "000" + str; | |
break; | |
case 2: | |
str = "00" + str; | |
break; | |
case 3: | |
str = "0" + str; | |
break; | |
} | |
clearDisplaySPI(); | |
s7sSendStringSPI("----"); // reset count view | |
delay(1000); | |
clearDisplaySPI(); | |
s7sSendStringSPI(str); // set the new count | |
} | |
// -- wait 25 secs before sending a new request | |
delay(25000); | |
Particle.publish("fbLikesEvent"); | |
} | |
// -- handle String messages to be displayed | |
void s7sSendStringSPI(String toSend) | |
{ | |
digitalWrite(ssPin, LOW); | |
SPI.transfer(0x76); | |
digitalWrite(ssPin, HIGH); | |
digitalWrite(ssPin, LOW); | |
for (int i=0; i<4; i++) | |
{ | |
delay(1); | |
SPI.transfer(toSend[i]); | |
delay(100); | |
} | |
digitalWrite(ssPin, HIGH); | |
} | |
// -- clear | |
void clearDisplaySPI() | |
{ | |
digitalWrite(ssPin, LOW); | |
SPI.transfer(0x76); | |
digitalWrite(ssPin, HIGH); | |
} | |
// -- set brightness | |
void setBrightnessSPI(byte value) | |
{ | |
digitalWrite(ssPin, LOW); | |
SPI.transfer(0x7A); | |
SPI.transfer(value); | |
digitalWrite(ssPin, HIGH); | |
} | |
// -- turn on/off decimals | |
void setDecimalsSPI(byte decimals) | |
{ | |
digitalWrite(ssPin, LOW); | |
SPI.transfer(0x77); | |
SPI.transfer(decimals); | |
digitalWrite(ssPin, HIGH); | |
} | |
// -- reset cursor aka "digit pointer" | |
void resetCursor() | |
{ | |
digitalWrite(ssPin, LOW); | |
SPI.transfer(0x79); | |
SPI.transfer(0x00); | |
digitalWrite(ssPin, HIGH); | |
} | |
// -- not used here | |
void loop(){} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment