diff --git a/Cube4Fun/AppDelegate.swift b/Cube4Fun/AppDelegate.swift index 4730474..12ec951 100644 --- a/Cube4Fun/AppDelegate.swift +++ b/Cube4Fun/AppDelegate.swift @@ -12,9 +12,15 @@ import Cocoa class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! + @IBOutlet weak var windowToolbox: NSWindow! func applicationDidFinishLaunching(aNotification: NSNotification) { // Insert code here to initialize your application } + func applicationShouldTerminate(sender: NSApplication) -> NSApplicationTerminateReply { + CubeNetworkObj.closeConnection() + return NSApplicationTerminateReply.TerminateNow + } + } diff --git a/Cube4Fun/Base.lproj/MainMenu.xib b/Cube4Fun/Base.lproj/MainMenu.xib index da57d34..a7b46e9 100644 --- a/Cube4Fun/Base.lproj/MainMenu.xib +++ b/Cube4Fun/Base.lproj/MainMenu.xib @@ -1,8 +1,8 @@ - + - - + + @@ -647,18 +647,18 @@ - + - - + + - + - - + + @@ -669,15 +669,15 @@ - + - + - + diff --git a/Cube4Fun/Cube4Fun-Bridging-Header.h b/Cube4Fun/Cube4Fun-Bridging-Header.h new file mode 100644 index 0000000..1659414 --- /dev/null +++ b/Cube4Fun/Cube4Fun-Bridging-Header.h @@ -0,0 +1,5 @@ +// +// Use this file to import your target's public headers that you would like to expose to Swift. +// + +#import "ObjCtoCPlusPlus.h" \ No newline at end of file diff --git a/Cube4Fun/CubeNetwork.cpp b/Cube4Fun/CubeNetwork.cpp new file mode 100644 index 0000000..78d7b7b --- /dev/null +++ b/Cube4Fun/CubeNetwork.cpp @@ -0,0 +1,244 @@ +// +// CubeNetwork.cpp +// Cube4Fun +// +// Created by Nik on 28.03.15. +// 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" + + +#ifdef WIN32 +#include +#else +#include +#endif // win32 + +using Poco::Net::DialogSocket; +using Poco::Net::SocketAddress; + + +unsigned char buffer3D[64]; +unsigned char receiveBuffer[32]; +int bytesReceived; +int i,x; +unsigned char color; +DialogSocket ds; +int frameChange = 0; + +void sleepcp(int milliseconds) // cross-platform sleep function +{ +#ifdef WIN32 + Sleep(milliseconds); +#else + usleep(milliseconds * 1000); +#endif // win32 +} + +bool frame1[3][64] = { {1,0,0,1, + 0,0,0,0, + 0,0,0,0, + 1,0,0,1, + 0,0,0,0, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, + 0,0,0,0, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, + 1,0,0,1, + 0,0,0,0, + 0,0,0,0, + 1,0,0,1}, + {0,0,0,0, + 0,0,0,0, + 1,0,0,1, + 0,0,0,0, + 1,0,0,1, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, + 0,0,0,0, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, //0,1,1,0, + 1,0,0,1, + 0,0,0,0, + 1,0,0,1, + 0,0,0,0, + 0,0,0,0}, + {0,0,0,0, + 1,0,0,1, + 0,0,0,0, + 0,0,0,0, + 0,0,0,0, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, //0,1,1,0, + 1,0,0,1, + 1,0,0,1, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, //0,1,1,0, + 0,0,0,0, + 0,0,0,0, + 0,0,0,0, + 1,0,0,1, + 0,0,0,0}}; + + +void fillBufferWithMsg() { + buffer3D[0] = 'G'; + buffer3D[1] = 'E'; + buffer3D[2] = 'T'; + buffer3D[3] = ' '; + buffer3D[4] = '/'; + buffer3D[5] = '?'; + buffer3D[6] = 'S'; + buffer3D[7] = 's'; + buffer3D[8] = ' '; + +} + +void testFrame() { +// for (color=128;color<130;color++) { + // Create testframe + for (x=0;x<64;x++) { + buffer3D[x] = color; // Red frame + } + if ( color > 128 ) { + buffer3D[0]=255; + buffer3D[3]=255; + buffer3D[12]=255; + buffer3D[15]=255; + + buffer3D[48]=255; + buffer3D[51]=255; + buffer3D[60]=255; + buffer3D[63]=255; + }else{ + buffer3D[0]=254; + buffer3D[3]=254; + buffer3D[12]=254; + buffer3D[15]=254; + + buffer3D[48]=254; + buffer3D[51]=254; + buffer3D[60]=254; + buffer3D[63]=254; + } + ds.sendBytes(buffer3D, 64); + // sleepcp(50); // 20 FPS + // } + +} + + +void testStream2() { + int frameChange = 0; + while (true) { + unsigned char color = rand() % 254; + for (i=0;i<64;i++) { + if ( frame1[frameChange][i] == 1 ) { + buffer3D[i] = color; // Rot + }else{ + buffer3D[i] = 255; // Aus + } + } + ds.sendBytes(buffer3D, 64); + sleepcp(1000); // 20 FPS + if ( frameChange < 2 ) { + frameChange++; + }else{ + frameChange=0; + } + } +} + +/* +void CubeNetwork::updateFrame() { + unsigned char color = rand() % 254; + for (i=0;i<64;i++) { + if ( frame1[frameChange][i] == 1 ) { + buffer3D[i] = color; // Rot + }else{ + buffer3D[i] = 255; // Aus + } + } + ds.sendBytes(buffer3D, 64); + if ( frameChange < 2 ) { + frameChange++; + }else{ + frameChange=0; + } +} +*/ + +void CubeNetwork::updateFrame(const unsigned char * frameSequence, unsigned int frameCount) { + // check for empty pointer + if ( frameSequence != NULL ) { + for (int frame = 0; frame(myFrames.bytes) + CubeNetworkObj.updateFrame(UnsafePointer(myFrames.bytes), count: myFrameCount) + } + override func awakeFromNib(){ + + //NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(5), invocation: CubeNetworkObj.initObjects(), repeats: true) +// CubeNetworkObj.initObjects(); + + // Init first frame + myFrames = NSMutableData(bytes: emptyFrame, length: 64) + myFrameCount = 1 + // Open connection to the LED cube + CubeNetworkObj.openConnection() + + NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("sendFrame"), userInfo: nil, repeats: true) + + + // create a new scene //let scene = SCNScene(named: "art.scnassets/ship.dae")! let scene = PrimitivesScene(); @@ -22,7 +64,7 @@ class GameViewController: NSViewController { // create and add a camera to the scene let camera = SCNCamera() camera.usesOrthographicProjection = true - camera.orthographicScale = 11 + camera.orthographicScale = 15 camera.zNear = 0 camera.zFar = 100 let cameraNode = SCNNode() @@ -86,6 +128,7 @@ class GameViewController: NSViewController { ship.addAnimation(animation, forKey: nil) */ + // set the scene to the view self.gameView!.scene = scene diff --git a/Cube4Fun/ObjCtoCPlusPlus.h b/Cube4Fun/ObjCtoCPlusPlus.h new file mode 100755 index 0000000..c16a1da --- /dev/null +++ b/Cube4Fun/ObjCtoCPlusPlus.h @@ -0,0 +1,16 @@ +// +// ObjCtoCPlusPlus.h +// Performance_Console +// +// Created by Gian Luigi Romita on 12/06/14. +// Copyright (c) 2014 Gian Luigi Romita. All rights reserved. +// + +#import + +@interface CubeNetworkObj : NSObject ++ (void) updateFrame: (const unsigned char *) frameSequence count: (UInt32) frameCount; ++ (void) initObjects; ++ (void) openConnection; ++ (void) closeConnection; +@end \ No newline at end of file diff --git a/Cube4Fun/ObjCtoCPlusPlus.mm b/Cube4Fun/ObjCtoCPlusPlus.mm new file mode 100755 index 0000000..0c4926b --- /dev/null +++ b/Cube4Fun/ObjCtoCPlusPlus.mm @@ -0,0 +1,26 @@ +#import "ObjCtoCPlusPlus.h" + +#import "CubeNetwork.h" + + +@implementation CubeNetworkObj + ++ (void) initObjects +{ + CubeNetwork::initObjects(); +} ++ (void) updateFrame: (const unsigned char *) frameSequence count: (UInt32) frameCount +{ + CubeNetwork::updateFrame(frameSequence, frameCount); +} ++ (void) openConnection +{ + CubeNetwork::openConnection(); +} ++ (void) closeConnection +{ + CubeNetwork::closeConnection(); +} + + +@end \ No newline at end of file diff --git a/Cube4Fun/PrimitivesScene.swift b/Cube4Fun/PrimitivesScene.swift index f19d4d9..5eb23a3 100644 --- a/Cube4Fun/PrimitivesScene.swift +++ b/Cube4Fun/PrimitivesScene.swift @@ -22,11 +22,18 @@ class PrimitivesScene: SCNScene { let xCount = 4 var myIndex = 0 + let spaceBetweenLEDs:CGFloat = 4.0 + let edgePosX:CGFloat = CGFloat(xCount-1) * spaceBetweenLEDs * -1 / 2 + let edgePosY:CGFloat = CGFloat(yCount-1) * spaceBetweenLEDs * -1 / 2 + let edgePosZ:CGFloat = CGFloat(zCount-1) * spaceBetweenLEDs * -1 / 2 + let boxSizeX:CGFloat = CGFloat(xCount-1) * spaceBetweenLEDs + 2 * radius + let boxSizeZ:CGFloat = CGFloat(zCount-1) * spaceBetweenLEDs + 2 * radius + let boxPosY:CGFloat = (CGFloat(yCount-1) * spaceBetweenLEDs / 2) + radius + 0.5 let text = SCNText(string: "Klick", extrusionDepth: 1) let textNode = SCNNode(geometry: text) textNode.name = "myDescr" - textNode.position = SCNVector3(x:-12.0 , y: 6.0, z: 0.0) + textNode.position = SCNVector3(x:-14.0 , y: 7.0, z: 0.0) textNode.scale = SCNVector3Make(0.1, 0.1, 0.1); self.rootNode.addChildNode(textNode) @@ -35,46 +42,48 @@ class PrimitivesScene: SCNScene { let cubeNode = SCNNode() cubeNode.name = "cubeNode" - let box = SCNBox(width: 11.0, height: 1.0, length: 11.0, chamferRadius: 0.1) - box.firstMaterial?.diffuse.contents = NSColor.blueColor() + let box = SCNBox(width: boxSizeX, height: 1.0, length: boxSizeZ, chamferRadius: 0.1) + box.firstMaterial?.diffuse.contents = NSColor.whiteColor() let boxNode = SCNNode(geometry: box) boxNode.name = "myBox" - boxNode.position = SCNVector3(x:0.0 , y: -6.0, z: 0.0) + boxNode.position = SCNVector3(x:0.0 , y: -boxPosY, z: 0.0) cubeNode.addChildNode(boxNode) - var y:CGFloat = -4.5 + var y:CGFloat = edgePosY for row in 0..