This commit is contained in:
workinghard
2016-10-17 00:06:19 -07:00
parent e254c1da9a
commit 893f93c9c5
2 changed files with 422 additions and 541 deletions

View File

@@ -1 +0,0 @@
/Users/nrinas/ownCloud/Arduino/Cube4Fun_CUBE_v1/Cube4Fun_CUBE_v1.ino

View File

@@ -0,0 +1,422 @@
#include <Rainbowduino.h>
#include <Wire.h>
//#define DEBUG true
#define COLORSHIFTTIME 100
#define MAXSTACKSIZE 256
#define NEWFRAMETIMEOUT 5000
#ifdef DEBUG
#define DEBUG_PRINTLN(x) Serial.println (x)
#define DEBUG_PRINT(x) Serial.print (x)
#define DEBUG_PRINTLN_TXT(x) Serial.println (F(x))
#define DEBUG_PRINT_TXT(x) Serial.print (F(x))
#else
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN_TXT(x)
#define DEBUG_PRINT_TXT(x)
#endif
// New balanced colors
static unsigned char RED[256] = {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,247,243,238,230,226,217,213,209,
200,196,188,183,179,171,166,158,154,145,141,137,128,124,115,111,107,98,94,85,81,77,68,64,56,51,47,39,34,
26,22,13,9,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,9,13,22,26,35,39,43,52,56,
64,68,77,81,85,94,98,107,111,115,124,128,136,141,145,153,158,166,171,179,183,188,196,200,209,213,217,226,
230,239,243,247,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0};
static unsigned char GREEN[256] = {0,0,7,17,22,30,34,39,47,51,60,64,68,77,81,90,94,98,107,111,119,124,132,136,141,149,153,162,166,170,179,
183,192,196,200,209,213,221,226,230,238,243,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,251,243,238,230,226,221,213,209,200,196,
192,183,179,170,166,158,154,149,141,136,128,124,120,111,107,98,94,90,81,77,68,64,56,52,47,39,34,26,22,18,9,
5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0};
static unsigned char BLUE[256] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,9,18,22,26,34,39,47,52,56,64,
68,77,81,90,94,98,107,111,120,124,128,136,141,149,154,158,166,170,179,183,188,196,200,209,213,221,226,230,
238,243,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,251,243,239,234,226,221,213,209,200,196,192,183,179,171,166,162,153,149,141,
136,132,124,120,111,107,103,94,90,81,77,68,64,60,52,47,39,35,30,22,17,255,0};
//unsigned char _buffer_2D[8][8]; // PlasmaMatrix = buffer_2D
unsigned char _buffer2_3D[4][4][4]; //
unsigned char _buffer1_3D[4][4][4]; //
unsigned char (* displayBuffer)[4][4][4];
unsigned char (* writeBuffer)[4][4][4];
int writeBuffer_top = 0;
unsigned char _receive_buffer[MAXSTACKSIZE];
unsigned char _receive_buffer_top = 0;
unsigned char _receive_buffer_bot = 0;
unsigned long streamStartedTime = 0;
unsigned long newFrameMillis = 0;
unsigned char z,x,y,colorshift=0;
long animation = 0;
unsigned long lastChangeTime = 0;
unsigned long lastColorShift = 0;
const long animInterval = 300000; // 5 minutes
int __state = 0; // 0 = waiting for cmd
int __stateEnd = 0; // 0 = start
boolean newFrame = false;
unsigned const char lc_slash = '/';
unsigned const char lc_question = '?';
unsigned const char lc_coma = ',';
unsigned const char lc_space = ' ';
// --------------- FUNCTIONS ---------------- //
void genPlasmaMatrix() {
// Generate Plasma-Array
for(x = 0; x < 8; x++) {
for(y = 0; y < 8; y++) {
uint32_t color = int(32.0 + (32.0 * sin(x / 4.0)) + 32.0 + (32.0 * sin(y / 4.0))) / 2;
setByteColor2D(color);
}
}
}
void genPlasmaMatrix2() {
// Generate Plasma2-Array
for(x = 0; x < 8; x++) {
for(y = 0; y < 8; y++) {
uint32_t color = int(32.0 + (32.0 * sin(x / 1.0)) + 32.0 + (32.0 * sin(y / 1.0))) / 2;
setByteColor2D(color);
}
}
}
void genPlasmaCube() {
// Generate PlasmaCube-Array
for(x = 0; x < 4; x++) {
for(y = 0; y < 4; y++) {
for(z = 0; z < 4; z++) {
int color = int(32.0 + (32.0 * sin(x / 1.0))+ 32.0 + (32.0 * sin(y / 1.0)) + 32.0 + (32.0 * sin(z / 1.0))) / 3;
setByteColor3D(color);
}
}
}
}
void genPlasmaCube2() {
// Generate PlasmaCube2-Array
for(x = 0; x < 4; x++) {
for(y = 0; y < 4; y++) {
for(z = 0; z < 4; z++) {
int color = int(32.0 + (32.0 * sin(x / 4.0))+ 32.0 + (32.0 * sin(y / 4.0)) + 32.0 + (32.0 * sin(z / 4.0))) / 3;
setByteColor3D(color);
}
}
}
}
void genPlasmaCube3() {
// Generate PlasmaCube3-Array
for(x = 0; x < 4; x++) {
for(y = 0; y < 4; y++) {
for(z = 0; z < 4; z++) {
int color = int(32.0 + (32.0 * sin(x / 8.0))+ 32.0 + (32.0 * sin(y / 8.0)) + 32.0 + (32.0 * sin(z / 8.0))) / 3;
setByteColor3D(color);
}
}
}
}
void setByteColor2D(uint32_t color) {
unsigned char color255 = (color * 4 + colorshift ) % 256; // Transform to 8 Bit color
if ( color255 > 0 ) {
if ( color255 > 253 ) { // 254 and 255 are reserved
color255 = 253;
}
}else{
color255 = 0;
} // Range 0 ... 253
Rb.setPixelXY(x,y,RED[color255],GREEN[color255],BLUE[color255]);
}
void setByteColor3D(uint32_t color) {
unsigned char color255 = (color * 4 + colorshift ) % 256; // Transform to 8 Bit color
if ( color255 > 0 ) {
if ( color255 > 253 ) { // 254 and 255 are reserved
color255 = 253;
}
}else{
color255 = 0;
} // Range 0 ... 253
Rb.setPixelZXY(z,x,y,RED[color255],GREEN[color255],BLUE[color255]);
}
void drawNewFrame() {
for(x=0;x<4;x++) {
for(y=0;y<4;y++) {
for(z=0;z<4;z++) {
Rb.setPixelZXY(z,x,y,(RED[(*displayBuffer)[x][y][z]]),(GREEN[(*displayBuffer)[x][y][z]]),(BLUE[(*displayBuffer)[x][y][z]])); //uses R, G and B color bytes
}
}
}
// Update the timeout timer
streamStartedTime = millis();
}
void changeAnim() {
animation = random(0,5);
//animation = 0;
}
// Flip the write and display buffer
void flipBuffer() {
unsigned char (* p_tmp)[4][4][4];
p_tmp = displayBuffer;
displayBuffer = writeBuffer;
writeBuffer = p_tmp;
// Remember the timestamp
newFrameMillis = millis();
}
void receiveEvent(int howMany) {
while ( Wire.available() > 0 ) {
if ( _receive_buffer_top < MAXSTACKSIZE ) {
_receive_buffer[_receive_buffer_top] = (unsigned char)Wire.read();
_receive_buffer_top ++;
}else{
DEBUG_PRINTLN_TXT("Buffer overflow");
Wire.read(); // Empty the queue
}
}
}
void fillBuffer(unsigned char pc_input, int p_pos) {
int li_tmp = 0;
for(int lx=0;lx<4;lx++) {
for(int ly=0;ly<4;ly++) {
for(int lz=0;lz<4;lz++) {
if ( li_tmp == p_pos ) {
(*writeBuffer)[lx][ly][lz] = pc_input;
return;
}
li_tmp++;
}
}
}
}
unsigned char getBuffer(int p_pos) {
int li_tmp = 0;
for(int lx=0;lx<4;lx++) {
for(int ly=0;ly<4;ly++) {
for(int lz=0;lz<4;lz++) {
if ( li_tmp == p_pos ) {
return (*writeBuffer)[lx][ly][lz];
}
li_tmp++;
}
}
}
return 0;
}
int checkEnd(unsigned char pc_input) {
int isEnd = 1;
switch (__stateEnd) {
case 0:
if ( pc_input == lc_coma) {
__stateEnd = __stateEnd + 1;
}
break;
case 1:
if ( pc_input == lc_coma ) {
__stateEnd = __stateEnd + 1;
}else{
__stateEnd = 0; // Roll back
}
break;
case 2:
if ( pc_input == lc_space ) {
__stateEnd = __stateEnd + 1;
}else{
__stateEnd = 0; // Roll back
}
break;
case 3:
if ( pc_input == lc_space ) {
// End found
isEnd = 0;
}
__stateEnd = 0; // Roll back
}
return isEnd;
}
void processIn(unsigned char pc_input) {
switch (__state) {
case 0:
if ( checkEnd(pc_input) == 0 ) {
// Reset the values
//DEBUG_PRINTLN_TXT("End found.");
//DEBUG_PRINT_TXT("Pos: ");
//DEBUG_PRINTLN(gi_buffer2_pos);
__state = 0;
writeBuffer_top = 0;
}
if ( pc_input == lc_slash ) {
__state = __state + 1;
//DEBUG_PRINTLN_TXT("0->1");
}
break;
case 1:
if ( pc_input == lc_slash ) {
__state = __state + 1;
//DEBUG_PRINTLN_TXT("1->2");
}else{
// something went wrong, roll back
__state = 0;
//DEBUG_PRINTLN_TXT("1->0");
}
break;
case 2:
if ( pc_input == lc_question ) {
__state = __state + 1;
//DEBUG_PRINTLN_TXT("2->3");
}else{
// something went wrong, roll back
__state = 0;
//DEBUG_PRINTLN_TXT("2->0");
}
break;
case 3:
if ( pc_input == lc_question ) {
__state = __state + 1;
//DEBUG_PRINTLN_TXT("3->4");
}else{
// something went wrong, roll back
__state = 0;
}
break;
case 4:
//DEBUG_PRINT(writeBuffer_top);
//DEBUG_PRINT_TXT(":");
//DEBUG_PRINTLN(pc_input);
if ( checkEnd(pc_input) == 0 ) {
// Reset the values
__state = 0;
writeBuffer_top = 0;
DEBUG_PRINTLN_TXT("ERR: End found.");
}else{
// Reading data
fillBuffer(pc_input, writeBuffer_top);
if ( writeBuffer_top < 64 ) { // Fill 64 chars
writeBuffer_top++;
}
if ( writeBuffer_top == 64 ) {
unsigned char lc_check = 0;
for (int i=0; i<64;i++) {
lc_check = lc_check + getBuffer(i);
//DEBUG_PRINT(i);
//DEBUG_PRINT_TXT(":");
//DEBUG_PRINTLN(getBuffer(i));
}
DEBUG_PRINT_TXT("Checksum: ");
DEBUG_PRINTLN(lc_check);
newFrame = true; // Draw new frame
flipBuffer(); // Change to the second buffer, so the first one can be displayed
writeBuffer_top = 0; // Reset the writeBuffer
__state = 0; // Change state to listen
__stateEnd = 0; // Reset endState check
}
}
}
}
// ---------------- MAIN --------------- //
void setup() {
Rb.init(); //initialize Rainbowduino driver
Wire.begin(2); // initialize wire connection as slave #2
Wire.onReceive(receiveEvent); // set function to be called
#ifdef DEBUG
Serial.begin(57600);
#endif
DEBUG_PRINTLN_TXT("Empfaenger 2");
randomSeed(analogRead(0)); // Init randomizer
displayBuffer = &_buffer2_3D;
writeBuffer = &_buffer1_3D;
changeAnim(); // First random animation
}
void loop() {
if ( _receive_buffer_top != _receive_buffer_bot ) {
while ( _receive_buffer_top != _receive_buffer_bot ) {
//DEBUG_PRINT(_receive_buffer[_receive_buffer_bot]);
processIn(_receive_buffer[_receive_buffer_bot]);
_receive_buffer_bot++; // Consume input
}
//DEBUG_PRINTLN_TXT("");
}
if ( _receive_buffer_top == _receive_buffer_bot && _receive_buffer_top > 0) {
_receive_buffer_top = 0;
_receive_buffer_bot = 0;
}
unsigned long currentMillis = millis();
if ( newFrame == true ) {
// Draw received frame
drawNewFrame();
// Check for the timeout
if ( currentMillis - newFrameMillis > NEWFRAMETIMEOUT ) {
newFrameMillis = currentMillis;
newFrame = false;
}
}else{
if ( currentMillis - lastChangeTime > animInterval) {
lastChangeTime = currentMillis;
changeAnim();
}
switch (animation) {
case 0: // Plasma1
genPlasmaMatrix();
break;
case 1: // Plasma2
genPlasmaMatrix2();
break;
case 2: // Cube3
genPlasmaCube3();
break;
case 3: // Cube1
genPlasmaCube();
break;
case 4: // Cube2
genPlasmaCube2();
break;
default:
genPlasmaCube();
}
if ( currentMillis - lastColorShift > COLORSHIFTTIME) { //
lastColorShift = currentMillis;
colorshift = colorshift + 1;
}
//delay(100);
}
}

View File

@@ -1,540 +0,0 @@
#define DEBUG 1
#define ANIM_FILE_NAME "/ANIMS.FRM"
#define FILE_READ "r"
#define FILE_WRITE "w"
#define FBLENGTH 65
#define SERBFLENGTH 32
#define SERVER_PORT 80
#define MAX_SEND_RETRY 10
#define MAX_ANIMKEY_LENGTH 12
#define MY_CUBE_ADDR 2
//#include <ESP8266WiFi.h>
#include <FS.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
//#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#ifdef DEBUG
#define DEBUG_PRINTLN(x) Serial.println (x)
#define DEBUG_PRINT(x) Serial.print (x)
#define DEBUG_PRINT2(x,y) Serial.print (x,y)
#define DEBUG_PRINTLN_TXT(x) Serial.println (F(x))
#define DEBUG_PRINT_TXT(x) Serial.print (F(x))
#else
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINT(x)
#define DEBUG_PRINT2(x,y)
#define DEBUG_PRINTLN_TXT(x)
#define DEBUG_PRINT_TXT(x)
#endif
unsigned const char lc_slash = '/';
unsigned const char lc_question = '?';
unsigned const char lc_coma = ',';
unsigned const char lc_space = ' ';
unsigned const char lc_f = 'f';
unsigned const char lc_F = 'F';
unsigned const char lc_s = 's';
unsigned const char lc_S = 'S';
unsigned char myAnimationCount = 0;
unsigned int _animationSpeed = 0;
unsigned long _animationLength = 0;
unsigned long _animationStartPos = 0;
unsigned long _animationEndPos = 0;
unsigned int _animationActFrame = 0;
unsigned long _previousMillis = 0; // will store last time animation frame was sent
unsigned char ga_buffer1[FBLENGTH];
unsigned char gi_buffer1_pos = 0;
unsigned char ga_buffer2[FBLENGTH];
unsigned char gi_buffer2_pos = 0;
unsigned char ga_sendBuffer[SERBFLENGTH];
unsigned char * gp_read_buffer;
//unsigned char * gi_read_buffer_pos;
unsigned char * gp_write_buffer;
//unsigned char * gi_write_buffer_pos;
int gi_status = 0; // 0 =
unsigned long sendDelay = 1000;
unsigned long lastChangeTime = 0;
int gi_pos = 0;
// Initialize the WiFi server library
const char* ssid = "Enjoy";
const char* password = "iWLSpo8zZ3NS";
ESP8266WebServer server(SERVER_PORT);
const char* www_username = "admin";
const char* www_password = "esp8266";
void fill_buffer_random(unsigned char * pp_buff) {
for (int i=0; i<FBLENGTH-1;i++){
pp_buff[i] = (unsigned char)random(0,254);
//pp_buff[i] = 1;
}
unsigned char li_check_digit = 0;
// Simple check digit
for (int i=0; i<FBLENGTH-1;i++){
li_check_digit = li_check_digit + pp_buff[i];
//DEBUG_PRINTLN(li_check_digit);
}
pp_buff[FBLENGTH-1] = li_check_digit;
}
void fill_buffer_run1(unsigned char * pp_buff) {
for (int i=0; i<FBLENGTH-1;i++){
if ( i == gi_pos ) {
pp_buff[i] = 128;
}else{
pp_buff[i] = 255;
}
}
if ( gi_pos < 63 ) {
gi_pos++;
}else{
gi_pos = 0;
}
}
void print_buffer(unsigned char * pp_buff) {
DEBUG_PRINTLN("");
for (int i=0; i<FBLENGTH-1;i++){
DEBUG_PRINT(i);
DEBUG_PRINT_TXT(":");
DEBUG_PRINTLN(pp_buff[i]);
}
}
boolean wireSendBytes(const uint8_t *data, size_t quantity) {
boolean try_again = true;
uint8_t error;
unsigned char send_retries = 0;
boolean success = false;
while ( try_again == true ) {
Wire.beginTransmission(MY_CUBE_ADDR);
if ( quantity == 1 ) {
Wire.write(data[0]);
}else{
Wire.write(data, quantity);
}
error = Wire.endTransmission();
if ( error == 0 || send_retries > MAX_SEND_RETRY ) {
// everything went well or timeout
try_again = false;
success = true;
}else{
// Something went wrong
DEBUG_PRINTLN_TXT("Send failed");
}
delayMicroseconds(10);
}
send_retries++;
return success;
}
void sendBufferedFrame() {
boolean lb_send_success;
// Send start key
ga_sendBuffer[0] = lc_slash;
ga_sendBuffer[1] = lc_slash;
ga_sendBuffer[2] = lc_question;
ga_sendBuffer[3] = lc_question;
lb_send_success = wireSendBytes(ga_sendBuffer, 4);
if (lb_send_success == true ) {
// First half frame
wireSendBytes(gp_read_buffer, 32);
for (unsigned char i=0;i<32;i++) { // Second half frame
ga_sendBuffer[i] = gp_read_buffer[i+32];
}
wireSendBytes(ga_sendBuffer, 32);
}
/*
DEBUG_PRINTLN("");
for (int i=0;i<FBLENGTH;i++) {
DEBUG_PRINT(gp_read_buffer[i]);
}
DEBUG_PRINTLN("");
*/
// Send end key
ga_sendBuffer[0] = lc_coma;
ga_sendBuffer[1] = lc_coma;
ga_sendBuffer[2] = lc_space;
ga_sendBuffer[3] = lc_space;
wireSendBytes(ga_sendBuffer, 4);
}
//START:-------FILE handler ------------------//
void printFileContent() {
File myProjectFile = SPIFFS.open(ANIM_FILE_NAME, FILE_READ);
if (myProjectFile) {
while (myProjectFile.available()) {
unsigned char myC = myProjectFile.read();
DEBUG_PRINTLN(myC);
}
}
}
void checkAnimationSDCard() {
// Open File for read
File myProjectFile = SPIFFS.open(ANIM_FILE_NAME, FILE_READ);
if (myProjectFile) {
DEBUG_PRINTLN_TXT("Reading keys");
unsigned char myReadStatus = 0; // 0 = expect key
unsigned char myC = 0;
unsigned char myKeyBuffer[MAX_ANIMKEY_LENGTH]; // maximum length for the key
unsigned char myKeyLength = 0;
unsigned char myIntBuffer[2]; // Byte to int
unsigned char myIntBufferLength = 0;
while (myProjectFile.available() && myReadStatus < 20) {
myC = myProjectFile.read();
switch (myReadStatus) {
case 0: // expect key
if ( myC == lc_coma ) {
myReadStatus = 1; // Startkey found
}
break;
case 1: // expect keyline confirmation
if ( myC == lc_F ) {
myReadStatus = 2; // Key-line confirmed
}else{
myReadStatus = 0; // Reset
}
break;
case 2: // Reading animation key
if ( myC == lc_coma ) {
myReadStatus = 3; // change to read next element
}else if ( myKeyLength < MAX_ANIMKEY_LENGTH ) { // Fill till max buffer
myKeyBuffer[myKeyLength] = myC;
myKeyLength++;
}
break;
case 3: // Save playtime
if ( myIntBufferLength < 2 ) { // Save playtime value
myIntBuffer[myIntBufferLength] = myC;
myIntBufferLength++;
}
if ( myIntBufferLength == 2 ) { // Buffer is full, set values
unsigned int animationLength = word(myIntBuffer[1], myIntBuffer[0]);
// Save the time the animation will end
_animationLength = animationLength * 1000 + millis();
// clear buffer
myIntBufferLength = 0;
// expect next value
myReadStatus = 4;
}
break;
case 4: // Save speed
if ( myIntBufferLength < 2 ) { // Save speed value
myIntBuffer[myIntBufferLength] = myC;
myIntBufferLength++;
}
if ( myIntBufferLength == 2 ) { // Buffer is full, set values
_animationSpeed = word(myIntBuffer[1], myIntBuffer[0]);
// clear buffer
myIntBufferLength = 0;
// expect next value
myReadStatus = 5;
}
break;
case 5: // Save frames
if ( myIntBufferLength < 2 ) { // Save speed value
myIntBuffer[myIntBufferLength] = myC;
myIntBufferLength++;
}
if ( myIntBufferLength == 2 ) { // Buffer is full, set values
// set to complete read
myReadStatus = 6;
unsigned int animationFrames = word(myIntBuffer[1], myIntBuffer[0]);
// Calculate start and end position
_animationStartPos = myProjectFile.position() + 1;
_animationEndPos = _animationStartPos + 65 * animationFrames;
// Check if the key matches
if(readBufferCompare2(reinterpret_cast<const char*>(myKeyBuffer), myKeyLength) > -1) {
// We found animation
DEBUG_PRINTLN_TXT("Found animation");
myReadStatus = 20; // End search, we found our animation
}else{
DEBUG_PRINTLN_TXT("No animation found");
if (_animationEndPos > _animationStartPos ) {
// Goto next frame
myProjectFile.seek(_animationEndPos, SeekSet);
myReadStatus = 0;
}
}
// clear buffer
myIntBufferLength = 0;
}
break;
case 6:
if( myC == lc_newline ) { // uncomplete keyline or no animation found
// check for values
if ( _animationEndPos > 0 && _animationStartPos > 0 && _animationSpeed > 0 ) {
DEBUG_PRINTLN_TXT("Anim key found");
}else{
DEBUG_PRINTLN_TXT("--keyline failed--");
// Reset everything
myReadStatus = 0;
myKeyLength = 0;
myIntBufferLength = 0;
clearSavedAnimation();
}
}
break;
}
}
}
myProjectFile.close();
}
boolean readAnimationSD() {
unsigned char myC;
unsigned char myBytes = 0;
// Read SD Card
File myProjectFile = SPIFFS.open(ANIM_FILE_NAME, FILE_READ);
// Goto start position
if ( myProjectFile.available() ) {
unsigned long _animationPos = _animationStartPos + _animationActFrame * 65; // startPos + offset
if ( _animationPos < _animationEndPos ) {
myProjectFile.seek(_animationPos, SeekSet);
}else{
_animationActFrame = 0; // Start at the first frame again
myProjectFile.seek(_animationStartPos, SeekSet);
}
}
// Read one frame
while ( myProjectFile.available() && myBytes < 64 ) {
myC = myProjectFile.read();
myReceiveBuffer[myBytes] = myC;
myBytes++;
}
// close the file:
myProjectFile.close();
// If we have complete frame, return true
if ( myBytes > 63 ) {
// read successfull
_animationActFrame++;
return true;
}else{
return false;
}
}
unsigned long byte2Long(unsigned char* byteArray) {
// little endian conversion
unsigned long retval;
retval = (unsigned long) byteArray[3] << 24 | (unsigned long) byteArray[2] << 16;
retval |= (unsigned long) byteArray[1] << 8 | byteArray[0];
return retval;
}
void writeAnimationSDCard(WiFiClient client) {
// Send answer
unsigned char readBuffer[4];
// First 10 bytes is a key, following by the length
readBuffer[0] = myReadBuffer[10];
readBuffer[1] = myReadBuffer[11];
readBuffer[2] = myReadBuffer[12];
readBuffer[3] = myReadBuffer[13];
unsigned long fileSize = byte2Long(readBuffer);
// DEBUG_PRINT_TXT("fileSizeBuffer: ");
for (unsigned char i=0; i<4; i++ ) {
client.write(readBuffer[i]);
// DEBUG_PRINT(readBuffer[i]);
}
client.write(lc_return);
client.write(lc_newline);
// DEBUG_PRINTLN_TXT(" ");
// DEBUG_PRINT_TXT("Filesize expected:");
// DEBUG_PRINTLN(fileSize);
// Blocking mode to write receiving data to file
if (client) {
// Remove file if exists
if ( SPIFFS.exists(ANIM_FILE_NAME) ) {
SPIFFS.remove(ANIM_FILE_NAME);
}
File myProjectFile = SPIFFS.open(ANIM_FILE_NAME, FILE_WRITE);
unsigned long receivedBytes = 0;
if (myProjectFile) {
while (client.connected() && receivedBytes < fileSize) {
if (client.available()) {
unsigned char c = client.read();
//writing bytes
myProjectFile.write(c);
receivedBytes++;
}
}
}
/*
if ( receivedBytes == fileSize ) {
DEBUG_PRINTLN_TXT("Complete data received\n");
}else{
DEBUG_PRINTLN_TXT("Data incomplete\n");
}
DEBUG_PRINT_TXT("Expected:");
DEBUG_PRINTLN(fileSize);
DEBUG_PRINT_TXT("Received:");
DEBUG_PRINTLN(receivedBytes);
*/
myProjectFile.close();
}
}
void displaySavedAnimation() {
if ( _animationLength > 0 ) { // Animation was set
unsigned long currentMillis = millis();
// Display animation set over the network
if ( currentMillis < _animationLength ) { // __animationLength = startTime + animLengthTime
if ( _animationStartPos > 0 && _animationEndPos > 0 && _animationSpeed > 0){ // validity check
if ( _animationEndPos > _animationStartPos ) {
//DEBUG_PRINTLN_TXT("Setting mode to 2");
setServerMode(2); // Switch the server mode
if ( currentMillis - _previousMillis >= _animationSpeed ) { // Sent data with the set speed
// save the last time you sent frame
_previousMillis = currentMillis;
// Fill Buffer
if ( true == readAnimationSD() ) {
// Send buffered frame
sendBufferedFrame();
}
}
}
}
}else{
// End the stream mode
setServerMode(0);
}
}
}
//END:---------FILE handler ------------------//
//START:-------HTTP handler ------------------//
void handleRoot() {
if(!server.authenticate(www_username, www_password)) {
return server.requestAuthentication();
}else{
server.send(200, "text/plain", "Login OK");
}
}
//END:--------HTTP handler -------------------//
//START:------------SETUP-----------------//
void setup() {
Wire.begin(); //for ESP8266-12E SDA=D2 and SLC=D1
Wire.setClockStretchLimit(1500);
#ifdef DEBUG
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
#endif
// always use this to "mount" the filesystem
bool result = SPIFFS.begin();
DEBUG_PRINT_TXT("SPIFFS opened: ");
DEBUG_PRINTLN(result);
// Start the WiFi connection
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if(WiFi.waitForConnectResult() != WL_CONNECTED) {
DEBUG_PRINTLN_TXT("WiFi Connect Failed! Rebooting...");
delay(1000);
ESP.restart();
}
//ArduinoOTA.begin();
server.on("/", handleRoot);
server.begin();
DEBUG_PRINT_TXT("Server started http://");
DEBUG_PRINTLN(WiFi.localIP());
// Init the buffer
gp_read_buffer = ga_buffer1;
gp_write_buffer = ga_buffer2;
// Random number generator
randomSeed(123);
}
//END:---------------SETUP--------------//
//START:-------------MAIN---------------//
void loop() {
#ifdef DEBUG
if ( Serial.available() > 0 ) {
// get incoming byte:
char inByte = Serial.read();
if ( inByte == '+' ) {
if ( sendDelay < 10000 ) {
sendDelay = sendDelay + 100;
}
}
if ( inByte == '-' ) {
if ( sendDelay >= 100 ) {
sendDelay = sendDelay - 100;
}
}
if ( inByte == '1' ) {
gi_status = 1;
}
if ( inByte == '0' ) {
gi_status = 0;
}
//DEBUG_PRINTLN(inByte);
DEBUG_PRINT_TXT("Delay: ");
DEBUG_PRINTLN(sendDelay);
DEBUG_PRINT_TXT("Status: ");
DEBUG_PRINTLN(gi_status);
}
#endif
unsigned long currentMillis = millis();
if ( gi_status == 1 && currentMillis - lastChangeTime > sendDelay) {
lastChangeTime = currentMillis;
// Fill the buffer with random data
//fill_buffer_random(gp_read_buffer);
fill_buffer_run1(gp_read_buffer);
// Send data
sendBufferedFrame();
//print_buffer(gp_read_buffer);
//DEBUG_PRINT_TXT("Data sent. Check digit: ");
//DEBUG_PRINTLN(gp_read_buffer[FBLENGTH-1]);
//delay(sendDelay);
}
// handle the Webserver requests
//ArduinoOTA.handle();
server.handleClient();
}
//END:--------------MAIN----------------------//