(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);
- 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 = SD.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);
- }else{
- _animationActFrame = 0; // Start at the first frame again
- myProjectFile.seek(_animationStartPos);
- }
- }
- // 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(EthernetClient client) {
- // Send answer
- unsigned char readBuffer[4];
- readBuffer[0] = myReadBuffer[2];
- readBuffer[1] = myReadBuffer[3];
- readBuffer[2] = myReadBuffer[4];
- readBuffer[3] = myReadBuffer[5];
- 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 ( SD.exists(ANIM_FILE_NAME) ) {
- SD.remove(ANIM_FILE_NAME);
- }
-
- File myProjectFile = SD.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);
- }
- }
-}
-
-void keepAliveFrame() {
- // Check if we need to send a keep alive frame
- if ( millis() - _lastTimeFrameSent > MY_STREAM_KEEPALIVE_TIME ) {
- // Send buffered frame
- sendBufferedFrame();
- }
-}
-
-// --------- MAIN ---------- //
-
-void setup() {
- // put your setup code here, to run once:
- Wire.begin();
-
-#ifdef DEBUG
- Serial.begin(9600);
-#endif
- DEBUG_PRINTLN_TXT("Sender1");
-
- DEBUG_PRINTLN_TXT("Init SD card...");
- // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
- // Note that even if it's not used as the CS pin, the hardware SS pin
- // (10 on most Arduino boards, 53 on the Mega) must be left as an output
- // or the SD library functions will not work.
- // disable w5100 SPI
- pinMode(10, OUTPUT);
- //digitalWrite(10,HIGH);
-
- if (!SD.begin(4)) {
- DEBUG_PRINTLN_TXT("init failed!");
- // TODO: deny some functions
- }else{
- DEBUG_PRINTLN_TXT("init done.");
- }
-
- // start the Ethernet connection:
- Ethernet.begin(mac, ip, gateway, subnet);
-
-// ----------------------
-/* Sorry, but had to drop DHCP support due to missing memory
-// If you have a MEGA, feel free to use it
- if (Ethernet.begin(mac) == 0) {
- DEBUG_PRINTLN_TXT("Failed to configure Ethernet using DHCP");
- DEBUG_PRINTLN_TXT("Using defaults 192.168.1.79");
- // initialize the ethernet device not using DHCP:
- Ethernet.begin(mac, ip, gateway, subnet);
- }
-*/
-// -----------------------
-
-#ifdef DEBUG
- // print your local IP address:
- DEBUG_PRINTLN_TXT("My IP: ");
- for (byte thisByte = 0; thisByte < 4; thisByte++) {
- // print the value of each byte of the IP address:
- DEBUG_PRINT2(Ethernet.localIP()[thisByte], DEC);
- DEBUG_PRINT(".");
- }
- DEBUG_PRINTLN_TXT("");
-#endif
-
- // Print free memory
- //DEBUG_PRINT("freeMemory()=");
- //DEBUG_PRINTLN(freeMemory());
-}
-
-
-void loop() {
- // Process Server requests
- // listen for incoming clients
- EthernetClient client = server.available();
-
- if (client) { // Client connected and sending data
- DEBUG_PRINTLN_TXT("Client attached");
- // Init status
- unsigned char readState = 0;
- unsigned char lastChar = 0; // Make sure you never check for 0-value !!!
- int bufferSize = 0;
- // If we receive data, interrupt other states
- setServerMode(0);
-
- // Read incoming data
- while (client.connected()) {
- if (client.available()) {
- // get incoming byte
- unsigned char c = client.read();
-
- // check for server mode
- switch (serverMode) {
- case 0:
- // Checking incoming requests
- readState = checkRequest(c, readState);
-
- // Processing if request is recognized
- if ( readState == 7 ) { // HTTP GET recognized
- processRequest(client);
- // process only one time
- readState = 0;
- myReadBufferCount = 0;
- }
- break;
-
- case 1:
- // Streaming frames
- bufferSize = sendFrameCached(c, bufferSize);
-
- // Check for streaming end
- if ( c == lc_S && lastChar == lc_s ) { // "sS" received
- //sendEndFrameStream();
- setServerMode(0);
- bufferSize = 0;
- }
-
- break;
- }
-
- // if we got whole HTTP Head and still waiting for commands,
- // send HTTP answer
- if ( serverMode == 0 && c == lc_newline && lastChar == lc_return ) { // \r\n was send
- // send a standard http response header
- client.println(F("HTTP/1.1 200 OK\nContent-Type: text/html\r\n\nRainbowduino Webserver
Rainbowduino Webserver 1.0
Functions:
Blink:
| |
Frametest
| |
Streamtest
| |
Print file to serial
| |
"));
- // Close the connection
- client.stop();
- }
-
- lastChar = c;
- }
-
- // If we are in the streaming mode and don't receive any frames,
- // check for timeout
- if ( serverMode == 1 ) {
- // keep the connection to the Rainbowduino alive
- keepAliveFrame();
- }
- }
-
- // give web browser time to receive the data
- delay(1);
- // close the connection
- client.stop();
- }
-
- if ( serverMode == 1 && client == false) {
- // If client closed the connection and we are still in the
- // stream mode, reset to normal operation
- setServerMode(0);
- }
-
- // ------------------------------------------------//
- // Display Animation
- displaySavedAnimation();
-
-}
diff --git a/Arduino/Webserver4Rainbow_v3.ino b/Arduino/Webserver4Rainbow_v3.ino
new file mode 120000
index 0000000..d635dc8
--- /dev/null
+++ b/Arduino/Webserver4Rainbow_v3.ino
@@ -0,0 +1 @@
+/Users/nrinas/ownCloud/Arduino/Webserver4Rainbow_v3/Webserver4Rainbow_v3.ino
\ No newline at end of file
diff --git a/Cube4Fun/Base.lproj/MainMenu.xib b/Cube4Fun/Base.lproj/MainMenu.xib
index 451f384..a63937d 100644
--- a/Cube4Fun/Base.lproj/MainMenu.xib
+++ b/Cube4Fun/Base.lproj/MainMenu.xib
@@ -1,8 +1,9 @@
-
+
-
-
+
+
+
@@ -512,9 +513,11 @@
+
+
@@ -529,15 +532,16 @@
-
+
-
+
+
@@ -614,24 +618,30 @@
+
+
+
+
+
+
@@ -705,6 +769,7 @@
+
@@ -725,31 +790,43 @@
-
+
-
+
-
+
-
-
+
+
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -757,8 +834,9 @@
-
+
+
@@ -768,8 +846,8 @@
-
-
+
+
@@ -783,10 +861,12 @@
+
-
+
+
@@ -796,9 +876,23 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -807,17 +901,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
diff --git a/Cube4Fun/src/AnimationsController.swift b/Cube4Fun/src/AnimationsController.swift
index 09a1cfb..afe89ce 100644
--- a/Cube4Fun/src/AnimationsController.swift
+++ b/Cube4Fun/src/AnimationsController.swift
@@ -140,6 +140,10 @@ class AnimationsController: NSObject, NSTableViewDataSource, NSTableViewDelegate
myTableView.reloadData()
}
+ @IBAction func importAnimation(sender: AnyObject) {
+ print("Animation import pressed")
+ }
+
@IBAction func moveUpItem(send: AnyObject) {
if ( __animations.getSelectedAnimationID() > 0 ) {
__animations.moveUpSelected()
diff --git a/Cube4Fun/src/AppDelegate.swift b/Cube4Fun/src/AppDelegate.swift
index 5e02ef9..2e6fe95 100644
--- a/Cube4Fun/src/AppDelegate.swift
+++ b/Cube4Fun/src/AppDelegate.swift
@@ -37,6 +37,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {
@IBOutlet weak var levelInd: NSProgressIndicator!
@IBOutlet weak var ipAddr: NSTextField!
@IBOutlet weak var port: NSTextField!
+ @IBOutlet weak var passwd: NSTextField!
@IBOutlet weak var waitAnim: NSProgressIndicator!
func applicationDidFinishLaunching(aNotification: NSNotification) {
@@ -48,6 +49,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {
port.stringValue = String(__prefData.portNR())
ipAddr.stringValue = __prefData.ipAddr()
+ passwd.stringValue = String(__prefData.passwdStr())
if CubeNetworkObj.connected() {
showConnActive(true)
}else{
@@ -103,7 +105,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {
if CubeNetworkObj.connected() {
CubeNetworkObj.closeConnection()
}
- if CubeNetworkObj.openConnection(__prefData.ipAddr(), port: UInt32(__prefData.portNR())) {
+ if CubeNetworkObj.openConnection(__prefData.ipAddr(), port: UInt32(__prefData.portNR()), passwd: __prefData.passwdStr()) {
showConnActive(true)
}else{
showConnActive(false)
@@ -139,6 +141,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {
//^(6553[0-5]|655[0-2]\d|65[0-4]\d\d|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)$
}
+ func validPasswdStr(myPasswd: String) -> Bool {
+ var valid: Bool = false
+ if myPasswd.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 8 {
+ valid = true
+ }
+ return valid
+ }
+
override func controlTextDidChange(obj: NSNotification) {
let myField: NSTextField = obj.object as! NSTextField
if myField.identifier == "IPADDR_FIELD" {
@@ -153,6 +163,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {
print("Changing port number")
}
}
+ if myField.identifier == "PASSWD_FIELD" {
+ if validPasswdStr(myField.stringValue) {
+ __prefData.setPasswd(myField.stringValue)
+ print("Changing password")
+ }
+ }
}
@IBAction func cmdCopyPressed(send: AnyObject) {
diff --git a/Cube4Fun/src/CubeNetwork.cpp b/Cube4Fun/src/CubeNetwork.cpp
index c8a0802..0347a75 100644
--- a/Cube4Fun/src/CubeNetwork.cpp
+++ b/Cube4Fun/src/CubeNetwork.cpp
@@ -35,6 +35,7 @@ TCPConnector* connector;
unsigned char buffer3D[64];
unsigned char receiveBuffer[32];
+char _passwd[8];
int bytesReceived;
int i,x;
unsigned char color;
@@ -119,36 +120,40 @@ void msgCloseFrameStream() {
void msgOpenFrameStream() {
if (stream) {
- buffer3D[0] = 'G';
- buffer3D[1] = 'E';
- buffer3D[2] = 'T';
- buffer3D[3] = ' ';
- buffer3D[4] = '/';
- buffer3D[5] = '?';
- buffer3D[6] = 'S';
- buffer3D[7] = 's';
- buffer3D[8] = ' ';
- stream->send(reinterpret_cast(buffer3D), 9);
+ char mySendBuffer[18];
+ strlcpy(mySendBuffer, "GET /?Ss", 9);
+ for (int i=8; i<16; i++) {
+ mySendBuffer[i] = _passwd[i-8];
+ }
+ mySendBuffer[16] = ' ';
+ mySendBuffer[17] = '\0';
+ printf("%s\n", mySendBuffer);
+
+ for (int i=0 ; i<18;i++) {
+ buffer3D[i] = mySendBuffer[i];
+ }
+ stream->send(reinterpret_cast(buffer3D), 17);
}
}
void msgStartWrite(u_int32_t msgLength) {
+ char mySendBuffer[22];
unsigned char myBuffer[4];
byte2uint32(myBuffer, msgLength);
if (stream) {
- buffer3D[0] = 'G';
- buffer3D[1] = 'E';
- buffer3D[2] = 'T';
- buffer3D[3] = ' ';
- buffer3D[4] = '/';
- buffer3D[5] = '?';
- buffer3D[6] = 'W';
- buffer3D[7] = 'w';
- buffer3D[8] = myBuffer[0]; // int32 to byte array conversion
- buffer3D[9] = myBuffer[1];
- buffer3D[10] = myBuffer[2];
- buffer3D[11] = myBuffer[3];
- buffer3D[12] = ' ';
+ strlcpy(mySendBuffer, "GET /?Ww", 9);
+ for (int i=8; i<16; i++) {
+ mySendBuffer[i] = _passwd[i-8];
+ }
+ mySendBuffer[16] = myBuffer[0]; // int32 to byte array conversion
+ mySendBuffer[17] = myBuffer[1];
+ mySendBuffer[18] = myBuffer[2];
+ mySendBuffer[19] = myBuffer[3];
+ mySendBuffer[20] = ' ';
+ mySendBuffer[21] = '\0';
+ for (int i=0 ; i<22;i++) {
+ buffer3D[i] = mySendBuffer[i];
+ }
printf("sending Length:\n");
printf("0: %u\n", myBuffer[0]);
@@ -156,7 +161,7 @@ void msgStartWrite(u_int32_t msgLength) {
printf("2: %u\n", myBuffer[2]);
printf("3: %u\n", myBuffer[3]);
- stream->send(reinterpret_cast(buffer3D), 13);
+ stream->send(reinterpret_cast(buffer3D), 21);
}
}
@@ -263,17 +268,22 @@ void CubeNetwork::updateFrame(const unsigned char * frameSequence, unsigned int
}
-bool CubeNetwork::openConnection(const char* ipAddr, unsigned int port) {
+bool CubeNetwork::openConnection(const char* ipAddr, unsigned int port, const char* myPasswd) {
bool connectionEstablished = false;
- printf("Try to open the connection\n");
//std::string ipAddr_str(reinterpret_cast(ipAddr));
//Poco::UInt16 portNr = port;
- connector = new TCPConnector();
- stream = connector->connect(ipAddr, port, 10); //Connect with 10 seconds timout
- if (stream) {
- msgOpenFrameStream();
- streamMode = 1;
- connectionEstablished = true;
+ if ( strlen(myPasswd) == 8 ) {
+ printf("Try to open the connection\n");
+ connector = new TCPConnector();
+ stream = connector->connect(ipAddr, port, 10); //Connect with 10 seconds timout
+ if (stream) {
+ strlcpy(_passwd, myPasswd, 9); // Save password
+ msgOpenFrameStream();
+ streamMode = 1;
+ connectionEstablished = true;
+ }
+ }else{
+ printf("No Password provided\n");
}
return connectionEstablished;
diff --git a/Cube4Fun/src/CubeNetwork.h b/Cube4Fun/src/CubeNetwork.h
index c5d5d8a..4256c84 100644
--- a/Cube4Fun/src/CubeNetwork.h
+++ b/Cube4Fun/src/CubeNetwork.h
@@ -31,7 +31,8 @@
#include /* time */
#include
#include "tcpconnector.h"
-#include
+#include
+
class CubeNetwork
{
@@ -40,7 +41,7 @@ public:
//static void initObjects();
static void updateFrame(const unsigned char * frameSequence = NULL, unsigned int frameCount = 0);
static void sendBytes(const unsigned char* byteBuffer = NULL, u_int32_t byteLength=0);
- static bool openConnection(const char* ipAddr, unsigned int port);
+ static bool openConnection(const char* ipAddr, unsigned int port, const char* myPasswd);
//static void openConnection();
static void closeConnection();
};
diff --git a/Cube4Fun/src/GameViewController.swift b/Cube4Fun/src/GameViewController.swift
index 92ede06..ec4d583 100644
--- a/Cube4Fun/src/GameViewController.swift
+++ b/Cube4Fun/src/GameViewController.swift
@@ -122,7 +122,7 @@ class GameViewController: NSViewController { // SCNSceneRendererDelegate
// myFrames = NSMutableData(bytes: emptyFrame, length: 64)
// myFrameCount = 1
// Open connection to the LED cube
- let established = CubeNetworkObj.openConnection(__prefData.ipAddr(), port: UInt32(__prefData.portNR()))
+ let established = CubeNetworkObj.openConnection(__prefData.ipAddr(), port: UInt32(__prefData.portNR()), passwd: __prefData.passwdStr())
if established {
print("connection established")
}else{
diff --git a/Cube4Fun/src/ObjCtoCPlusPlus.h b/Cube4Fun/src/ObjCtoCPlusPlus.h
index c87324e..4caaef7 100755
--- a/Cube4Fun/src/ObjCtoCPlusPlus.h
+++ b/Cube4Fun/src/ObjCtoCPlusPlus.h
@@ -23,7 +23,7 @@
+ (void) updateFrame: (const unsigned char *) frameSequence count: (UInt32) frameCount;
+ (void) sendBytes: (const unsigned char *) byteBuffer count: (u_int32_t) byteLength;
//+ (void) initObjects;
-+ (bool) openConnection: (const char *) ipAddress port: (UInt32) port;
++ (bool) openConnection: (const char *) ipAddress port: (UInt32) port passwd: (const char*) myPasswd;
+ (void) closeConnection;
+ (bool) connected;
@end
diff --git a/Cube4Fun/src/ObjCtoCPlusPlus.mm b/Cube4Fun/src/ObjCtoCPlusPlus.mm
index 2d202f3..21c467e 100755
--- a/Cube4Fun/src/ObjCtoCPlusPlus.mm
+++ b/Cube4Fun/src/ObjCtoCPlusPlus.mm
@@ -39,10 +39,10 @@
CubeNetwork::sendBytes(byteBuffer, byteLength);
}
//+ (void) openConnection
-+ (bool) openConnection: (const char *) ipAddress port: (UInt32) port
++ (bool) openConnection: (const char *) ipAddress port: (UInt32) port passwd:(const char *)myPasswd
{
bool success = false;
- success = CubeNetwork::openConnection(ipAddress, port);
+ success = CubeNetwork::openConnection(ipAddress, port, myPasswd);
return success;
}
+ (void) closeConnection
diff --git a/Cube4Fun/src/Preferences.swift b/Cube4Fun/src/Preferences.swift
index 164ce65..d8b4aa5 100644
--- a/Cube4Fun/src/Preferences.swift
+++ b/Cube4Fun/src/Preferences.swift
@@ -25,9 +25,11 @@ class Preferences: NSObject {
let _myPrefs: NSUserDefaults = NSUserDefaults()
var _myIPAddr: String = String()
var _myPortNr: Int = Int()
+ var _myPasswd: String = String()
let ipaddr_txt: String = "IPADDR"
let portnr_txt: String = "PORTNR"
+ let passwd_txt: String = "PASSWD"
// ipAddr
@@ -48,6 +50,10 @@ class Preferences: NSObject {
if myPort > 0 {
_myPortNr = myPort
}
+ // Load Password
+ if let myPasswd: String = _myPrefs.stringForKey(passwd_txt) {
+ _myPasswd = myPasswd
+ }
}
func saveFile() {
@@ -55,6 +61,8 @@ class Preferences: NSObject {
_myPrefs.setObject(_myIPAddr, forKey: ipaddr_txt)
// Save port number
_myPrefs.setInteger(_myPortNr, forKey: portnr_txt)
+ // Save Password
+ _myPrefs.setObject(_myPasswd, forKey: passwd_txt)
}
func ipAddr() -> (String) {
@@ -74,4 +82,13 @@ class Preferences: NSObject {
_myPortNr = portNr
self.saveFile()
}
+
+ func passwdStr() -> (String) {
+ return _myPasswd
+ }
+
+ func setPasswd(passwdStr: String) {
+ _myPasswd = passwdStr
+ self.saveFile()
+ }
}
\ No newline at end of file