This commit is contained in:
workinghard
2015-04-15 23:32:30 -07:00
parent b9d4bf0ca7
commit 29010486bb
4 changed files with 147 additions and 26 deletions

View File

@@ -41,7 +41,7 @@ class Animations: NSObject {
var _animationSelected: Int = 0 var _animationSelected: Int = 0
let _minSendDelay: NSTimeInterval = 0.200 // fastes speed 200 milliseconds let _minSendDelay: NSTimeInterval = 0.200 // fastes speed 200 milliseconds
let maxFrameSpeed = 3000 // the lowest possible speed allowed let maxFrameSpeed = 3000 // the lowest possible speed allowed
let minFrameSpeed = 200 // the fastest possible speed allowed let minFrameSpeed = 100 // the fastest possible speed allowed
let frameSpeedStep = 100 // how fast increase or decrease speed let frameSpeedStep = 100 // how fast increase or decrease speed
// var _playSendDelay: NSTimeInterval = 0.5 // 500 milliseconds as default // var _playSendDelay: NSTimeInterval = 0.5 // 500 milliseconds as default
@@ -60,6 +60,33 @@ class Animations: NSObject {
return _animationArray.count return _animationArray.count
} }
func sendFrame() {
let time = NSDate.timeIntervalSinceReferenceDate()
if (_previousUpdateTime == 0.0) {
_previousUpdateTime = time;
}
// need to do this because of rounding issues
let deltaTime: Int = Int( round( (time - _previousUpdateTime) * 10 ) * 100);
//var ms = Int((time % 1) * 1000)
if ( _playAllFrames ) {
println("Delta: \(deltaTime) Speed: \(__animations.animationSpeedInt())")
if ( deltaTime >= __animations.animationSpeedInt() ){
_previousUpdateTime = time;
if (self.getAnimationFrameID() >= self.getAnimationFrameCount()) {
_gameView.firstButtonPressed()
}else{
_gameView.nextButtonPressed()
}
CubeNetworkObj.updateFrame(self.getAnimDataSelected(), count: UInt32(self.getAnimationFrameID()))
}
}else{
CubeNetworkObj.updateFrame(self.getAnimDataSelected(), count: UInt32(self.getAnimationFrameID()))
}
}
func loadAnimations(animArray: NSArray) { func loadAnimations(animArray: NSArray) {
// clear the array first // clear the array first
_animationArray.removeAll(keepCapacity: true) _animationArray.removeAll(keepCapacity: true)
@@ -116,7 +143,7 @@ class Animations: NSObject {
} }
func getAnimationFrameCount(id: Int) -> (Int) { func getAnimationFrameCount(id: Int) -> (Int) {
let myFrames: NSMutableData = (self.getAnimation(id)).objectForKey(AnimFrames) as! NSMutableData let myFrames: NSMutableData = (self.getAnimation(id)).objectForKey(AnimFrames) as! NSMutableData
return myFrames.length / 64 return (myFrames.length / 64)
//let value = (self.getAnimation(id)).objectForKey("AnimFrames") as Int //let value = (self.getAnimation(id)).objectForKey("AnimFrames") as Int
//return value //return value
} }
@@ -223,16 +250,37 @@ class Animations: NSObject {
return _minSendDelay return _minSendDelay
} }
func getAnimData() -> (UnsafePointer<UInt8>) { func getAnimDataSelected() -> (UnsafePointer<UInt8>) {
let myData: NSMutableData = (self.getAnimation(_animationSelected)).objectForKey(AnimFrames) as! NSMutableData let myData: NSMutableData = (self.getAnimation(_animationSelected)).objectForKey(AnimFrames) as! NSMutableData
let byteArray = UnsafePointer<UInt8>(myData.bytes) let byteArray = UnsafePointer<UInt8>(myData.bytes)
return byteArray return byteArray
} }
func getAnimData(id: Int) -> (UnsafePointer<UInt8>) {
let myData: NSMutableData = (self.getAnimation(id)).objectForKey(AnimFrames) as! NSMutableData
let byteArray = UnsafePointer<UInt8>(myData.bytes)
return byteArray
}
func getAnimDataLength(id: Int) -> Int {
let myFrames: NSMutableData = (self.getAnimation(id)).objectForKey(AnimFrames) as! NSMutableData
return myFrames.length
}
func setLEDColor(color: UInt8, led: Int) { func setLEDColor(color: UInt8, led: Int) {
println("Led pressed: \(led)")
var myByte: [UInt8] = [color] var myByte: [UInt8] = [color]
let myData: NSMutableData = (self.getAnimation(_animationSelected)).objectForKey(AnimFrames) as! NSMutableData let myData: NSMutableData = (self.getAnimation(_animationSelected)).objectForKey(AnimFrames) as! NSMutableData
myData.replaceBytesInRange(NSMakeRange(((self.getAnimationFrameID()-1)*64)+led, 1), withBytes: myByte) let bytePosition = NSMakeRange(((self.getAnimationFrameID()-1)*64)+led, 1)
myData.replaceBytesInRange(bytePosition, withBytes: myByte)
// Send updated frame
self.sendFrame()
}
func getLEDColor(pos: Int) -> (UInt8) {
let myData = self.getAnimDataSelected()
return myData[pos]
} }
func clearLEDColor() { func clearLEDColor() {

View File

@@ -159,20 +159,62 @@ class AnimationsController: NSObject, NSTableViewDataSource, NSTableViewDelegate
} }
@IBAction func exportAnimations(send: AnyObject) { @IBAction func exportAnimations(send: AnyObject) {
let testdata: [UInt8] = [254, 1, 128, 255] var sendData: [UInt8] = [UInt8]()
println("Import button pressed") println("Import button pressed")
// for each animation // for each animation
for ( var i = 0; i < __animations.count(); i++ ) {
// Create header line per animation // Create header line per animation
// Syntax: ,F<key:12Byte>,<playtime:2Byte><speed:2Byte><frames:2Byte>\n
// Key
sendData.append(UInt8(ascii: ","))
sendData.append(UInt8(ascii: "F"))
let key = __animations.getAnimationKey(i)
let keyArray: NSData = key.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)!
let keyBytes: UnsafePointer<UInt8> = UnsafePointer<UInt8>(keyArray.bytes)
for (var j = 0; j < keyArray.length; j++) {
sendData.append(UInt8(keyBytes[j]))
}
sendData.append(UInt8(ascii: ","))
// Playtime
let playTime: [UInt8] = convertInt16(UInt16(__animations.getAnimationDuration(i)))
sendData.append(playTime[0])
sendData.append(playTime[1])
// Speed
let playSpeed: [UInt8] = convertInt16(UInt16(__animations.getAnimationSpeed(i)))
sendData.append(playSpeed[0])
sendData.append(playSpeed[1])
// Frames
let playFrames: [UInt8] = convertInt16(UInt16(__animations.getAnimationFrameCount(i)))
sendData.append(playFrames[0])
sendData.append(playFrames[1])
// End line
sendData.append(UInt8(ascii: "\n"))
// Append frame, separated by new-Line // Append frame, separated by new-Line
let animData = __animations.getAnimData(i)
for ( var count = 1; count <= __animations.getAnimDataLength(i); count++) {
sendData.append(animData[count-1])
// End line for each frame
if ( (count % 64) == 0 ) {
sendData.append(UInt8(ascii: "\n"))
}
}
}
// Calculate overall data to send // Calculate overall data to send
// Send data // Send data
CubeNetworkObj.sendBytes(testdata, count: UInt32(testdata.count)) CubeNetworkObj.sendBytes(sendData , count: UInt32(sendData.count))
} }

View File

@@ -16,12 +16,29 @@ let relativeBarPosition: CGFloat = 500.0
class GameView: SCNView { class GameView: SCNView {
var playTimer: NSTimer = NSTimer()
func scheduleTimer() {
playTimer = NSTimer.scheduledTimerWithTimeInterval(__animations.animationSpeedFloat(), target: __animations, selector: Selector("sendFrame"), userInfo: nil, repeats: true)
}
func resetTimer() {
playTimer.invalidate()
}
func rescheduleTimer() {
resetTimer();
scheduleTimer();
}
func resetView() { func resetView() {
println("Reset view")
// goto first frame // goto first frame
self.firstButtonPressed() self.firstButtonPressed()
// Update speed // Update speed
self.updateSpeedText() self.updateSpeedText()
// Send updated frame
__animations.sendFrame()
} }
func updateButtonVisibility() { func updateButtonVisibility() {
@@ -100,13 +117,16 @@ class GameView: SCNView {
} }
} }
// Send updated frame
__animations.sendFrame()
} }
func updateLEDFrame() { func updateLEDFrame() {
// get actuall frame data // get actuall frame data
// let data : UnsafePointer<Byte> = UnsafePointer<Byte>(myFrames.mutableBytes) // let data : UnsafePointer<Byte> = UnsafePointer<Byte>(myFrames.mutableBytes)
let data = __animations.getAnimData() //let data = __animations.getAnimDataSelected()
//NSMutableData //NSMutableData
if let rootNode = self.scene?.rootNode { if let rootNode = self.scene?.rootNode {
if let cubeNode = rootNode.childNodeWithName("cubeNode", recursively: true) { if let cubeNode = rootNode.childNodeWithName("cubeNode", recursively: true) {
@@ -118,8 +138,8 @@ class GameView: SCNView {
if let material: SCNMaterial = geometry.firstMaterial { if let material: SCNMaterial = geometry.firstMaterial {
var color: NSColor = NSColor() var color: NSColor = NSColor()
let colorPosition: Int = ((__animations.getAnimationFrameID()-1)*64) + ledID let colorPosition: Int = ((__animations.getAnimationFrameID()-1)*64) + ledID
let savedColor: UInt8 = data[colorPosition] let savedColor: UInt8 = __animations.getLEDColor(colorPosition) //data[colorPosition]
if data[colorPosition] == 255 { if savedColor == 255 {
color = NSColor.grayColor() color = NSColor.grayColor()
}else{ }else{
let hueColor = CGFloat(savedColor) / 255.0 let hueColor = CGFloat(savedColor) / 255.0
@@ -135,6 +155,7 @@ class GameView: SCNView {
} }
} }
} }
//__animations.sendFrame()
} }
func plusButtonPressed() { func plusButtonPressed() {
@@ -226,6 +247,7 @@ class GameView: SCNView {
// _playSendDelay = _playSendDelay + 0.1 // _playSendDelay = _playSendDelay + 0.1
updateSpeedText() updateSpeedText()
// } // }
rescheduleTimer()
} }
func minusSpeedButtonPressed() { func minusSpeedButtonPressed() {
@@ -235,6 +257,7 @@ class GameView: SCNView {
// _playSendDelay = _playSendDelay - 0.1 // _playSendDelay = _playSendDelay - 0.1
updateSpeedText() updateSpeedText()
// } // }
rescheduleTimer()
} }
func updateSpeedText() { func updateSpeedText() {
@@ -261,6 +284,7 @@ class GameView: SCNView {
// Start the animation // Start the animation
_playAllFrames = true _playAllFrames = true
scheduleTimer();
} }
func pauseButtonPressed() { func pauseButtonPressed() {
@@ -272,6 +296,7 @@ class GameView: SCNView {
// Stop the animation // Stop the animation
_playAllFrames = false _playAllFrames = false
resetTimer()
} }
func openAnimationWindow() { func openAnimationWindow() {
@@ -409,6 +434,15 @@ class GameView: SCNView {
} }
ledPressed = Int(name.intValue) ledPressed = Int(name.intValue)
// Update the LED frame
var myByte: UInt8
if ledColorOn {
myByte = klickedColor
}else{
myByte = 255 // Off
}
__animations.setLEDColor(myByte, led: ledPressed)
} }
} }
} }
@@ -460,14 +494,6 @@ class GameView: SCNView {
SCNTransaction.commit() SCNTransaction.commit()
// Update the LED frame
var myByte: UInt8
if ledColorOn {
myByte = klickedColor
}else{
myByte = 255 // Off
}
__animations.setLEDColor(myByte, led: ledPressed)
//myFrames.replaceBytesInRange(NSMakeRange(((myFrameCount-1)*64)+ledPressed, 1), withBytes: myByte) //myFrames.replaceBytesInRange(NSMakeRange(((myFrameCount-1)*64)+ledPressed, 1), withBytes: myByte)
} }

View File

@@ -62,10 +62,12 @@ class GameViewController: NSViewController { // SCNSceneRendererDelegate
} }
*/ */
func sendFrame() {
sendFrame(NSDate.timeIntervalSinceReferenceDate())
}
//func sendFrame() {
// sendFrame(NSDate.timeIntervalSinceReferenceDate())
//}
/*
func sendFrame(time: NSTimeInterval) { func sendFrame(time: NSTimeInterval) {
//println(time) //println(time)
@@ -82,12 +84,12 @@ class GameViewController: NSViewController { // SCNSceneRendererDelegate
}else{ }else{
self.gameView!.nextButtonPressed() self.gameView!.nextButtonPressed()
} }
CubeNetworkObj.updateFrame(__animations.getAnimData(), count: UInt32(__animations.getAnimationFrameID())) CubeNetworkObj.updateFrame(__animations.getAnimDataSelected(), count: UInt32(__animations.getAnimationFrameID()))
_previousUpdateTime = time; _previousUpdateTime = time;
} }
}else{ }else{
if ( _deltaTime >= __animations.getMinSendDelay() ) { if ( _deltaTime >= __animations.getMinSendDelay() ) {
CubeNetworkObj.updateFrame(__animations.getAnimData(), count: UInt32(__animations.getAnimationFrameID())) CubeNetworkObj.updateFrame(__animations.getAnimDataSelected(), count: UInt32(__animations.getAnimationFrameID()))
//println("SendFrame: \(_deltaTime)") //println("SendFrame: \(_deltaTime)")
_previousUpdateTime = time; _previousUpdateTime = time;
} }
@@ -95,6 +97,8 @@ class GameViewController: NSViewController { // SCNSceneRendererDelegate
//CubeNetworkObj.updateFrame(UnsafePointer<UInt8>(myFrames.bytes), count: myFrameCount) //CubeNetworkObj.updateFrame(UnsafePointer<UInt8>(myFrames.bytes), count: myFrameCount)
} }
*/
override func awakeFromNib(){ override func awakeFromNib(){
_gameView = gameView; _gameView = gameView;
@@ -107,9 +111,10 @@ class GameViewController: NSViewController { // SCNSceneRendererDelegate
// myFrameCount = 1 // myFrameCount = 1
// Open connection to the LED cube // Open connection to the LED cube
CubeNetworkObj.openConnection() CubeNetworkObj.openConnection()
__animations.sendFrame()
// Fallback timer if nothing render at the moment // Fallback timer if nothing render at the moment
NSTimer.scheduledTimerWithTimeInterval(__animations.getMinSendDelay(), target: self, selector: Selector("sendFrame"), userInfo: nil, repeats: true) // NSTimer.scheduledTimerWithTimeInterval(__animations.getMinSendDelay(), target: self, selector: Selector("sendFrame"), userInfo: nil, repeats: true)