diff --git a/Cube4Fun/Animations.swift b/Cube4Fun/Animations.swift index cd231d1..6e5e52b 100644 --- a/Cube4Fun/Animations.swift +++ b/Cube4Fun/Animations.swift @@ -71,7 +71,7 @@ class Animations: NSObject { //var ms = Int((time % 1) * 1000) if ( _playAllFrames ) { - println("Delta: \(deltaTime) Speed: \(__animations.animationSpeedInt())") + //println("Delta: \(deltaTime) Speed: \(__animations.animationSpeedInt())") if ( deltaTime >= __animations.animationSpeedInt() ){ _previousUpdateTime = time; if (self.getAnimationFrameID() >= self.getAnimationFrameCount()) { @@ -99,6 +99,7 @@ class Animations: NSObject { } func getAnimation(id: Int) -> (NSDictionary) { + println(_animationArray.count) let myAnimation = _animationArray[id] as NSDictionary return myAnimation } @@ -188,7 +189,7 @@ class Animations: NSObject { } func newAnimation() -> (NSMutableDictionary) { println("create new animation") - return [AnimName: "Animation1", AnimKey: "1=anim1", AnimDuration: 1, AnimSpeed: 500, AnimFrames: self.newFrame()] + return [AnimName: "Animation1", AnimKey: "1=anim1", AnimDuration: 10, AnimSpeed: 500, AnimFrames: self.newFrame()] } func newFrame() -> (NSMutableData) { println("create new frame") diff --git a/Cube4Fun/AnimationsController.swift b/Cube4Fun/AnimationsController.swift index b0ac400..2622770 100644 --- a/Cube4Fun/AnimationsController.swift +++ b/Cube4Fun/AnimationsController.swift @@ -163,7 +163,7 @@ class AnimationsController: NSObject, NSTableViewDataSource, NSTableViewDelegate println("Import button pressed") // for each animation - for ( var i = 0; i < __animations.count(); i++ ) { + for ( var i = 0; i < __animations.count(); ++i ) { // Create header line per animation // Syntax: ,F,\n @@ -173,8 +173,11 @@ class AnimationsController: NSObject, NSTableViewDataSource, NSTableViewDelegate let key = __animations.getAnimationKey(i) let keyArray: NSData = key.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)! let keyBytes: UnsafePointer = UnsafePointer(keyArray.bytes) - for (var j = 0; j < keyArray.length; j++) { - sendData.append(UInt8(keyBytes[j])) + for (var j = 0; j < keyArray.length; ++j) { + + if keyBytes[j] != UInt8(ascii: "\n") && keyBytes[j] != UInt8(ascii: "\r" ) { // ignore line breaks in the key + sendData.append(UInt8(keyBytes[j])) + } } sendData.append(UInt8(ascii: ",")) @@ -198,7 +201,7 @@ class AnimationsController: NSObject, NSTableViewDataSource, NSTableViewDelegate // Append frame, separated by new-Line let animData = __animations.getAnimData(i) - for ( var count = 1; count <= __animations.getAnimDataLength(i); count++) { + for ( var count = 1; count <= __animations.getAnimDataLength(i); ++count) { sendData.append(animData[count-1]) // End line for each frame if ( (count % 64) == 0 ) { diff --git a/Cube4Fun/AppDelegate.swift b/Cube4Fun/AppDelegate.swift index ec4203d..8d1af14 100644 --- a/Cube4Fun/AppDelegate.swift +++ b/Cube4Fun/AppDelegate.swift @@ -10,22 +10,37 @@ import Cocoa var _animationsWindow: NSWindow = NSWindow() var _cubeWindow: NSWindow = NSWindow() +var _prefWindow: NSWindow = NSWindow() var __animations: Animations = Animations() +var __prefData: Preferences = Preferences() @NSApplicationMain -class AppDelegate: NSObject, NSApplicationDelegate { +class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var animationsWindow: NSWindow! @IBOutlet weak var preferencesWindow: NSWindow! @IBOutlet weak var myMenu: NSMenu! + @IBOutlet weak var levelInd: NSProgressIndicator! + @IBOutlet weak var ipAddr: NSTextField! + @IBOutlet weak var port: NSTextField! + @IBOutlet weak var waitAnim: NSProgressIndicator! func applicationDidFinishLaunching(aNotification: NSNotification) { // Insert code here to initialize your application _animationsWindow = animationsWindow _cubeWindow = window + _prefWindow = preferencesWindow //__animations.initialize() + + port.stringValue = String(__prefData.portNR()) + ipAddr.stringValue = __prefData.ipAddr() + if CubeNetworkObj.connected() { + showConnActive(true) + }else{ + showConnActive(false) + } } func applicationShouldTerminate(sender: NSApplication) -> NSApplicationTerminateReply { @@ -70,4 +85,61 @@ class AppDelegate: NSObject, NSApplicationDelegate { preferencesWindow.setIsVisible(true) } + @IBAction func testIPConnection(send: AnyObject) { + //println("TestIP Button clicked") + + if CubeNetworkObj.connected() { + CubeNetworkObj.closeConnection() + } + if CubeNetworkObj.openConnection(__prefData.ipAddr(), port: UInt32(__prefData.portNR())) { + showConnActive(true) + }else{ + showConnActive(false) + } + + } + + func showConnActive(active: Bool) { + if active { + levelInd.doubleValue = 100.0 + }else{ + levelInd.doubleValue = 0.0 + } + } + + func validIPAddress(ipaddr: String) -> Bool { + var valid: Bool = false + let validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" + if ipaddr != "" { + if (ipaddr.rangeOfString(validIpAddressRegex, options: .RegularExpressionSearch) != nil) { + valid = true; + } + } + return valid + } + + func validPortNr(portNr: Int) -> Bool { + var valid: Bool = false + if portNr < 65536 && portNr > 0 { + valid = true + } + return valid + //^(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)$ + } + + override func controlTextDidChange(obj: NSNotification) { + let myField: NSTextField = obj.object as! NSTextField + if myField.identifier == "IPADDR_FIELD" { + if validIPAddress(myField.stringValue) { + __prefData.setIPAddr(myField.stringValue) + println("Changing ip address field") + } + } + if myField.identifier == "PORTNR_FIELD" { + if validPortNr(myField.integerValue) { + __prefData.setPortNr(myField.integerValue) + println("Changing port number") + } + } + } } diff --git a/Cube4Fun/Base.lproj/MainMenu.xib b/Cube4Fun/Base.lproj/MainMenu.xib index e8a5d2f..070572a 100644 --- a/Cube4Fun/Base.lproj/MainMenu.xib +++ b/Cube4Fun/Base.lproj/MainMenu.xib @@ -1,5 +1,5 @@ - + @@ -130,21 +130,21 @@ - + - + - + - + - - - - - - - + @@ -175,162 +169,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -641,7 +479,7 @@ - + @@ -854,7 +692,10 @@ + + + @@ -880,22 +721,13 @@ - + - - - - - - - - - @@ -914,32 +746,57 @@ - - - - - - - - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -952,5 +809,6 @@ + diff --git a/Cube4Fun/Cube6-128j.png b/Cube4Fun/Cube6-128j.png new file mode 100644 index 0000000..01c096e Binary files /dev/null and b/Cube4Fun/Cube6-128j.png differ diff --git a/Cube4Fun/Cube6-16j.png b/Cube4Fun/Cube6-16j.png new file mode 100644 index 0000000..0cefcf4 Binary files /dev/null and b/Cube4Fun/Cube6-16j.png differ diff --git a/Cube4Fun/Cube6-256j.png b/Cube4Fun/Cube6-256j.png new file mode 100644 index 0000000..f2242de Binary files /dev/null and b/Cube4Fun/Cube6-256j.png differ diff --git a/Cube4Fun/Cube6-32j.png b/Cube4Fun/Cube6-32j.png new file mode 100644 index 0000000..d688034 Binary files /dev/null and b/Cube4Fun/Cube6-32j.png differ diff --git a/Cube4Fun/Cube6-64j.png b/Cube4Fun/Cube6-64j.png new file mode 100644 index 0000000..3b3e0a3 Binary files /dev/null and b/Cube4Fun/Cube6-64j.png differ diff --git a/Cube4Fun/CubeNetwork.cpp b/Cube4Fun/CubeNetwork.cpp index 8ff87ca..00bbaec 100644 --- a/Cube4Fun/CubeNetwork.cpp +++ b/Cube4Fun/CubeNetwork.cpp @@ -6,14 +6,6 @@ // Copyright (c) 2015 DerNik. All rights reserved. // -#include -#include "Poco/Net/SocketAddress.h" -#include "Poco/Net/DialogSocket.h" -#include "Poco/Net/NetException.h" -#include /* srand, rand */ -#include /* time */ -#include - #include "CubeNetwork.h" @@ -25,7 +17,7 @@ using Poco::Net::DialogSocket; using Poco::Net::SocketAddress; - +using Poco::Exception; unsigned char buffer3D[64]; unsigned char receiveBuffer[32]; @@ -94,6 +86,8 @@ bool frame1[3][64] = { {1,0,0,1, 1,0,0,1, 0,0,0,0}}; +bool connectionEstablished = false; + void byte2uint32(unsigned char* bytes, u_int32_t msgLength) { unsigned char *vp = (unsigned char *)&msgLength; bytes[0] = vp[0]; // int32 to byte array conversion @@ -112,21 +106,21 @@ void msgCloseFrameStream() { } } -void fillBufferWithMsgStartStream() { - buffer3D[0] = 'G'; - buffer3D[1] = 'E'; - buffer3D[2] = 'T'; - buffer3D[3] = ' '; - buffer3D[4] = '/'; - buffer3D[5] = '?'; - buffer3D[6] = 'S'; - buffer3D[7] = 's'; - buffer3D[8] = ' '; -} - void msgOpenFrameStream() { - fillBufferWithMsgStartStream(); - ds.sendBytes(buffer3D, 9); + try { + buffer3D[0] = 'G'; + buffer3D[1] = 'E'; + buffer3D[2] = 'T'; + buffer3D[3] = ' '; + buffer3D[4] = '/'; + buffer3D[5] = '?'; + buffer3D[6] = 'S'; + buffer3D[7] = 's'; + buffer3D[8] = ' '; + ds.sendBytes(buffer3D, 9); + }catch (const Poco::Net::NetException & e){ + std::cerr << e.displayText() << std::endl; + } } void msgStartWrite(u_int32_t msgLength) { @@ -147,6 +141,12 @@ void msgStartWrite(u_int32_t msgLength) { buffer3D[11] = myBuffer[3]; buffer3D[12] = ' '; + printf("sending Length:\n"); + printf("0: %u\n", myBuffer[0]); + printf("1: %u\n", myBuffer[1]); + printf("2: %u\n", myBuffer[2]); + printf("3: %u\n", myBuffer[3]); + ds.sendBytes(buffer3D, 13); }catch (const Poco::Net::NetException & e){ std::cerr << e.displayText() << std::endl; @@ -232,6 +232,7 @@ void CubeNetwork::updateFrame() { void CubeNetwork::sendBytes(const unsigned char* byteBuffer, unsigned int byteLength) { printf("sendBytes called\n"); + if ( connectionEstablished) { if ( streamMode == 1 ) { // End the frameStreammode first msgCloseFrameStream(); @@ -245,6 +246,7 @@ void CubeNetwork::sendBytes(const unsigned char* byteBuffer, unsigned int byteLe msgStartWrite(byteLength); unsigned char myBuffer[4]; int ret = ds.receiveRawBytes(myBuffer, 4); + printf("received Length:\n"); printf("0: %u\n", myBuffer[0]); printf("1: %u\n", myBuffer[1]); printf("2: %u\n", myBuffer[2]); @@ -263,10 +265,13 @@ void CubeNetwork::sendBytes(const unsigned char* byteBuffer, unsigned int byteLe std::cerr << e.displayText() << std::endl; } } + } + } void CubeNetwork::updateFrame(const unsigned char * frameSequence, unsigned int frameCount) { + if (connectionEstablished) { // check for empty pointer if ( frameSequence != NULL ) { //for (startFrame = 0; startFrame(ipAddr)); + Poco::UInt16 portNr = port; try { - printf("Try to open the connection\n"); - ds.connect(SocketAddress("192.168.1.79", 8081)); + ds.connect(SocketAddress(ipAddr_str, portNr), Poco::Timespan(10, 0)); + msgOpenFrameStream(); streamMode = 1; - }catch (const Poco::Net::NetException & e){ + connectionEstablished = true; + }catch (Poco::Net::NetException & e){ std::cerr << e.displayText() << std::endl; + ds.close(); + }catch (Poco::TimeoutException & e) { + std::cerr << e.displayText() << std::endl; + ds.close(); + }catch (Exception e){ + std::cerr << e.displayText() << std::endl; + ds.close(); } + return connectionEstablished; } void CubeNetwork::closeConnection() { try { + connectionEstablished = false; msgCloseFrameStream(); ds.close(); }catch (const Poco::Net::NetException & e){ @@ -301,6 +322,11 @@ void CubeNetwork::closeConnection() { streamMode = 0; } +bool CubeNetwork::connected() { + return connectionEstablished; +} + +/* void CubeNetwork::initObjects() { srand((unsigned int)time(NULL)); @@ -326,5 +352,5 @@ void CubeNetwork::initObjects() { } - +*/ //void Performance_CPlusPlus::sortArray(unsigned int num_elements) diff --git a/Cube4Fun/CubeNetwork.h b/Cube4Fun/CubeNetwork.h index 1c24dbf..015403a 100644 --- a/Cube4Fun/CubeNetwork.h +++ b/Cube4Fun/CubeNetwork.h @@ -9,16 +9,26 @@ #ifndef __Cube4Fun__CubeNetwork__ #define __Cube4Fun__CubeNetwork__ +#include +#include "Poco/Foundation.h" +#include "Poco/Net/SocketAddress.h" +#include "Poco/Net/DialogSocket.h" +#include "Poco/Net/NetException.h" +#include "Poco/Exception.h" +#include /* srand, rand */ +#include /* time */ +#include + class CubeNetwork { public: - - static void initObjects(); + static bool connected(); + //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 void openConnection(); + static bool openConnection(const char* ipAddr, unsigned int port); + //static void openConnection(); static void closeConnection(); - }; #endif /* defined(__Cube4Fun__CubeNetwork__) */ diff --git a/Cube4Fun/GameViewController.swift b/Cube4Fun/GameViewController.swift index e2cb133..4df200e 100644 --- a/Cube4Fun/GameViewController.swift +++ b/Cube4Fun/GameViewController.swift @@ -110,7 +110,12 @@ class GameViewController: NSViewController { // SCNSceneRendererDelegate // myFrames = NSMutableData(bytes: emptyFrame, length: 64) // myFrameCount = 1 // Open connection to the LED cube - CubeNetworkObj.openConnection() + let established = CubeNetworkObj.openConnection(__prefData.ipAddr(), port: UInt32(__prefData.portNR())) + if established { + println("connection established") + }else{ + println("connection failed") + } __animations.sendFrame() // Fallback timer if nothing render at the moment diff --git a/Cube4Fun/Info.plist b/Cube4Fun/Info.plist index 241d552..d7934d2 100644 --- a/Cube4Fun/Info.plist +++ b/Cube4Fun/Info.plist @@ -25,7 +25,7 @@ LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSHumanReadableCopyright - Copyright © 2015 DerNik. All rights reserved. + Copyright © 2015 Nikolai Rinas. All rights reserved. NSMainNibFile MainMenu NSPrincipalClass diff --git a/Cube4Fun/ObjCtoCPlusPlus.h b/Cube4Fun/ObjCtoCPlusPlus.h index 29a69a0..69aee02 100755 --- a/Cube4Fun/ObjCtoCPlusPlus.h +++ b/Cube4Fun/ObjCtoCPlusPlus.h @@ -11,7 +11,8 @@ @interface CubeNetworkObj : NSObject + (void) updateFrame: (const unsigned char *) frameSequence count: (UInt32) frameCount; + (void) sendBytes: (const unsigned char *) byteBuffer count: (u_int32_t) byteLength; -+ (void) initObjects; -+ (void) openConnection; +//+ (void) initObjects; ++ (bool) openConnection: (const char *) ipAddress port: (UInt32) port; + (void) closeConnection; ++ (bool) connected; @end \ No newline at end of file diff --git a/Cube4Fun/ObjCtoCPlusPlus.mm b/Cube4Fun/ObjCtoCPlusPlus.mm index 7b92828..7a2e7c8 100755 --- a/Cube4Fun/ObjCtoCPlusPlus.mm +++ b/Cube4Fun/ObjCtoCPlusPlus.mm @@ -5,10 +5,12 @@ @implementation CubeNetworkObj +/* + (void) initObjects { CubeNetwork::initObjects(); } + */ + (void) updateFrame: (const unsigned char *) frameSequence count: (UInt32) frameCount { CubeNetwork::updateFrame(frameSequence, frameCount); @@ -17,14 +19,21 @@ { CubeNetwork::sendBytes(byteBuffer, byteLength); } -+ (void) openConnection +//+ (void) openConnection ++ (bool) openConnection: (const char *) ipAddress port: (UInt32) port { - CubeNetwork::openConnection(); + bool success = false; + success = CubeNetwork::openConnection(ipAddress, port); + return success; } + (void) closeConnection { CubeNetwork::closeConnection(); } ++ (bool) connected +{ + return CubeNetwork::connected(); +} @end \ No newline at end of file diff --git a/Cube4Fun/Poco/libPocoFoundation.a b/Cube4Fun/Poco/libPocoFoundation.a new file mode 100644 index 0000000..d52a329 Binary files /dev/null and b/Cube4Fun/Poco/libPocoFoundation.a differ diff --git a/Cube4Fun/Poco/libPocoNet.a b/Cube4Fun/Poco/libPocoNet.a new file mode 100644 index 0000000..76c61d2 Binary files /dev/null and b/Cube4Fun/Poco/libPocoNet.a differ diff --git a/Cube4Fun/Preferences.swift b/Cube4Fun/Preferences.swift new file mode 100644 index 0000000..6076e13 --- /dev/null +++ b/Cube4Fun/Preferences.swift @@ -0,0 +1,65 @@ +// +// Preferences.swift +// Cube4Fun +// +// Created by Nik on 18.04.15. +// Copyright (c) 2015 DerNik. All rights reserved. +// + +import Foundation + +class Preferences: NSObject { + + let _myPrefs: NSUserDefaults = NSUserDefaults() + var _myIPAddr: String = String() + var _myPortNr: Int = Int() + + let ipaddr_txt: String = "IPADDR" + let portnr_txt: String = "PORTNR" + // ipAddr + + + override init() { + super.init() + + // Load defaults + self.loadFile() + } + + func loadFile() { + // Load ip address + if let myIPAddr: String = _myPrefs.stringForKey(ipaddr_txt) { + _myIPAddr = myIPAddr + } + // Load custom port + let myPort: Int = _myPrefs.integerForKey(portnr_txt) + if myPort > 0 { + _myPortNr = myPort + } + } + + func saveFile() { + // Save ip address + _myPrefs.setObject(_myIPAddr, forKey: ipaddr_txt) + // Save port number + _myPrefs.setInteger(_myPortNr, forKey: portnr_txt) + } + + func ipAddr() -> (String) { + return _myIPAddr + } + + func setIPAddr(ipAddr: String) { + _myIPAddr = ipAddr + self.saveFile() + } + + func portNR() -> (Int) { + return _myPortNr + } + + func setPortNr(portNr: Int) { + _myPortNr = portNr + self.saveFile() + } +} \ No newline at end of file