Skip to content

Commit

Permalink
Gyroscope, parts of Controller added
Browse files Browse the repository at this point in the history
  • Loading branch information
gotschi committed Oct 15, 2010
1 parent 34c8ded commit 065c163
Show file tree
Hide file tree
Showing 10 changed files with 752 additions and 456 deletions.
7 changes: 7 additions & 0 deletions iphone2websocketViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@

#import <UIKit/UIKit.h>
#import "Zimt/Zimt.h"
#import <CoreMotion/CoreMotion.h>

@interface iphone2websocketViewController : UIViewController < ZTWebSocketDelegate, UITextFieldDelegate, UIAccelerometerDelegate > {

ZTWebSocket* webSocket;

NSString *ipAdress;
IBOutlet UILabel *connectStatus;
IBOutlet UIButton *connectButton;
IBOutlet UITextField *ipadressTextfield;
IBOutlet UITextField *gameID;
IBOutlet UITextField *gamerID;
IBOutlet UIButton *gyroButton;
CMMotionManager *motionManager;
NSOperationQueue *opQ;

}

-(IBAction) connect;

-(IBAction) activateAccel;

-(IBAction) activateGyro;

-(IBAction) sendHello;

-(IBAction)textFieldDoneEditing:(id)sender;
Expand Down
70 changes: 49 additions & 21 deletions iphone2websocketViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,77 @@ - (void)viewDidLoad {

[super viewDidLoad];

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 20.0f;
motionManager = [[CMMotionManager alloc] init]; // Gyroscope (iphone 4)+

}

- (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler
{
// ACCEL
[webSocket send: [NSString stringWithFormat:@"%f/%f/%f",aceler.x, aceler.y, aceler.z]];
// Accelerator sends x,y,z 60 times / second
[webSocket send: [NSString stringWithFormat:@"%@/%f/%f/%f", @"ACCEL", aceler.x, aceler.y, aceler.z]];
//[connectStatus setText:[NSString stringWithFormat:@"%@/%f/%f/%f", @"ACCEL", aceler.x, aceler.y, aceler.z]];
}

-(void) sendHello {
-(void) sendHello { // DEBUG
[webSocket send: @"Hallo!"];
}

- (void) activateAccel {

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
if(accel.delegate != self) {
//[webSocket send:@"ACCEL"];

accel.delegate = self;
}

if (accel.updateInterval == 1.0f/60.0f) {
accel.updateInterval= 20.0f;
[connectStatus setText:@"20s"];
else {
accel.delegate = nil;
[webSocket send: [NSString stringWithFormat:@"%@/%f/%f/%f", @"ACCEL", 0, 0, 0 ]];
}

}

else {
accel.updateInterval = 1.0f/60.0f;
[connectStatus setText:@"1/60"];
-(void) activateGyro { // Gyroscope

if(motionManager.gyroUpdateInterval == 1.0f/60.0f) {

motionManager.gyroUpdateInterval = 1000.0f;

[webSocket send: [NSString stringWithFormat:@"%@/%f/%f/%f", @"GYRO", 0, 0, 0]];

}

else motionManager.gyroUpdateInterval = 1.0f/60.0f;

[motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler: ^(CMGyroData *gyroData, NSError *error)
{
CMRotationRate rotate = gyroData.rotationRate;
[webSocket send: [NSString stringWithFormat:@"%@/%f/%f/%f", @"GYRO", rotate.x, rotate.y, rotate.z]];
}];

}

-(void) connect {

NSString *myString = [NSString stringWithFormat:@"%@%@%@", @"ws://10.254.0.53:10000/", gameID.text, @"/connect"];
if(!webSocket) {
//Connect to IP in textfield, with gameID
NSString *cString = [NSString stringWithFormat:@"%@%@%@%@%@", @"ws://", ipadressTextfield.text, @":10000/", gameID.text, @"/connect"];

webSocket = [[ZTWebSocket alloc] initWithURLString:myString delegate:self];
// start Websocket
webSocket = [[ZTWebSocket alloc] initWithURLString:cString delegate:self];

// open Websocket
if (!webSocket.connected) {
[webSocket open];
[connectButton setTitle:@"DisConnect" forState:UIControlStateNormal];
}
}
else {
[webSocket release];
webSocket = 0;
[connectButton setTitle:@"Connect" forState:UIControlStateNormal];
}
}

-(void)webSocket:(ZTWebSocket *)webSocket didFailWithError:(NSError *)error {
Expand All @@ -70,17 +99,16 @@ -(void)webSocket:(ZTWebSocket *)webSocket didFailWithError:(NSError *)error {
}

-(void)webSocket:(ZTWebSocket *)webSocket didReceiveMessage:(NSString*)message {
[connectStatus setText:message];
//[connectStatus setText:message]; // DEBUG
[gamerID setText:message]; // Set GamerID
}

-(void)webSocketDidOpen:(ZTWebSocket *)aWebSocket {

[connectStatus setText:@"Connected!"];
}

-(void)webSocketDidClose:(ZTWebSocket *)webSocket {

[connectButton setTitle:@"Connect" forState:UIControlStateNormal];

[connectStatus setText:@"disconnected"];
}

-(void)webSocketDidSendMessage:(ZTWebSocket *)webSocket {
Expand Down
4 changes: 2 additions & 2 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
@games = {};

def generate_game_id
uid = rand(999999)
uid = 2#rand(999999)
while(@games[uid])
uid = rand(999999)
uid = 2#rand(999999)
end
return uid.to_s
end
Expand Down
115 changes: 115 additions & 0 deletions webgl_game/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

var gameId = false;
clientId = false;

function init()
{
output = document.getElementById("output");
serverWebsocket();
}

function serverWebsocket()
{
server = new WebSocket("ws://localhost:10000/game");

server.onopen = function(evt) {
console.log("requesting game id");
};

server.onclose = function(evt) {
console.log("game DISCONNECTED");
$('#connection').html('Disconnected');
};

server.onmessage = function(evt) {
// save game id on first message
if(!gameId) {
gameId = evt.data;
console.log('game id: ' + evt.data);
clientWebsocket(gameId);
} else {

msg = evt.data.split(" ");
//console.log("message received: " + msg[1])

if(msg[0] !='1' && msg[1]=='connected') {
$('#dList').append('<li class="device'+msg[0]+' device">iPhone '+msg[0]+' Connected</li>');
$('.device'+msg[0]).animate({opacity:1}, 500);
}

if(msg[0] !='1' && msg[1]=='disconnected') {
$('.device'+msg[0]).animate({opacity:0}, 500, function(){
$('.device'+msg[0]).remove();
});
}

if(msg[0] != '1') {
if(msg[1] != 'connected') {
message = msg[1].split("/");

if(message[0] == 'ACCEL') {
x = message[1];
y = message[2];
z = message[3];
speed = x/40000*(-1);
yawRate= y/3000*(-1);
}

else if(message[0] == 'GYRO') {
gx = message[1];
gy = message[2];
gz = message[3];
yawRate = gx/12*(-1);
pitchRate = (gy/13);
}
}
}
//console.log("message received: " + evt.data)
}

};

server.onerror = function(evt) {
console.error(evt.data);
};
}

function clientWebsocket(id)
{
console.log("try to connect to game: " + id);
client = new WebSocket("ws://localhost:10000/" + id + "/connect");

client.onopen = function(evt) {

console.log("connected to game");
$('#connection').html('Connected Game to Server!');

};

client.onclose = function(evt) {

console.log("client: DISCONNECTED");

};

client.onmessage = function(evt) {
// save game id on first message
if(!clientId) {
clientId = evt.data;
console.log('client id: ' + evt.data);

} else {
console.log("message received: " + evt.data);
}
};

client.onerror = function(evt) {
console.error(evt.data);
};
}

window.addEventListener("load", init, false);

$(document).ready(function() {

});
Loading

0 comments on commit 065c163

Please sign in to comment.