/// <summary> /// Checks current joystick direction, to calculate, which button is pressed. /// </summary> /// <param name="msg">Current Zeemote message</param> /// <returns> /// 0 - no direction, /// 1 - left, /// 2 - right, /// 3 - up, /// 4 - down</returns> private int FindJoystickDirection(ZeemoteMessage msg) { // Define which direction is prevailed, to press apropriate button, get dead zone in account // No, Left, Right, Up, Down int[] directions = new int[5]; directions[0] = JoystickDeadZone; directions[1] = (Math.Abs(msg.JoystickX) > JoystickDeadZone) && msg.JoystickX < 0 ? Math.Abs(msg.JoystickX) : 0; directions[2] = (Math.Abs(msg.JoystickX) > JoystickDeadZone) && msg.JoystickX > 0 ? msg.JoystickX : 0; directions[3] = (Math.Abs(msg.JoystickY) > JoystickDeadZone) && msg.JoystickY < 0 ? Math.Abs(msg.JoystickY) : 0;; directions[4] = (Math.Abs(msg.JoystickY) > JoystickDeadZone) && msg.JoystickY > 0 ? msg.JoystickY : 0; int direction = 0; // Define direction for (int i = 0; i < directions.Length; i++) { if (directions[i] > directions[direction]) { direction = i; } } return(direction); }
/// <summary> /// Analizes hot key message. /// Understands what keys have been pressed or released /// </summary> /// <param name="msg"></param> public void AnalyzeKeys(ZeemoteMessage msg) { // If there are buttons touched in message if (msg.ButtonsTouched) { if (!msg.ButtonA && curZeemoteKeys.A) { curZeemoteKeys.A = msg.ButtonA; if (KeyUp != null) { KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeA)); } } else if (msg.ButtonA && !curZeemoteKeys.A) { curZeemoteKeys.A = msg.ButtonA; if (KeyDown != null) { KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeA)); } } if (!msg.ButtonB && curZeemoteKeys.B) { curZeemoteKeys.B = msg.ButtonB; if (KeyUp != null) { KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeB)); } } else if (msg.ButtonB && !curZeemoteKeys.B) { curZeemoteKeys.B = msg.ButtonB; if (KeyDown != null) { KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeB)); } } if (!msg.ButtonC && curZeemoteKeys.C) { curZeemoteKeys.C = msg.ButtonC; if (KeyUp != null) { KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeC)); } } else if (msg.ButtonC && !curZeemoteKeys.C) { curZeemoteKeys.C = msg.ButtonC; if (KeyDown != null) { KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeC)); } } if (!msg.ButtonD && curZeemoteKeys.D) { curZeemoteKeys.D = msg.ButtonD; if (KeyUp != null) { KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeD)); } } else if (msg.ButtonD && !curZeemoteKeys.D) { curZeemoteKeys.D = msg.ButtonD; if (KeyDown != null) { KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeD)); } } } else // Joystick was touched { int direction = FindJoystickDirection(msg); // 0 - no; 1 - left; 2 - right; 3 - up; 4 - down. if (direction != 1 && curZeemoteKeys.JoyLeft) { curZeemoteKeys.JoyLeft = false; if (KeyUp != null) { KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyLeft)); } } else if (direction == 1 && !curZeemoteKeys.JoyLeft) { curZeemoteKeys.JoyLeft = true; if (KeyDown != null) { KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyLeft)); } } if (direction != 2 && curZeemoteKeys.JoyRight) { curZeemoteKeys.JoyRight = false; if (KeyUp != null) { KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyRight)); } } else if (direction == 2 && !curZeemoteKeys.JoyRight) { curZeemoteKeys.JoyRight = true; if (KeyDown != null) { KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyRight)); } } if (direction != 3 && curZeemoteKeys.JoyUp) { curZeemoteKeys.JoyUp = false; if (KeyUp != null) { KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyUp)); } } else if (direction == 3 && !curZeemoteKeys.JoyUp) { curZeemoteKeys.JoyUp = true; if (KeyDown != null) { KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyUp)); } } if (direction != 4 && curZeemoteKeys.JoyDown) { curZeemoteKeys.JoyDown = false; if (KeyUp != null) { KeyUp.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyDown)); } } else if (direction == 4 && !curZeemoteKeys.JoyDown) { curZeemoteKeys.JoyDown = true; if (KeyDown != null) { KeyDown.Invoke(this, new ZeemoteKeysArgs(ZeemoteKeys.KeyCodeJoyDown)); } } } }
/// <summary> /// Dispatch zeemote message /// </summary> /// <param name="message"></param> /// <param name="numberOfBytes"></param> public void DispatchBluetoothMessage(byte[] message, int numberOfBytes) { ZeemoteMessage msg = new ZeemoteMessage(); if ((numberOfBytes == 8) && (message[0] == 7 && message[1] == 161 && message[2] == 5 && message[3] == 255)) { // Device is connected. } // Check if message about button else if ((numberOfBytes == 9) && (message[0] == 8 && message[1] == 161 && message[2] == 7)) { for (int i = 3; i <= 6; i++) { if (message[i] == 0) { msg.ButtonA = true; } else if (message[i] == 1) { msg.ButtonB = true; } else if (message[i] == 2) { msg.ButtonC = true; } else if (message[i] == 3) { msg.ButtonD = true; } } // Button was touched even if there are no messages about it. // That means, button was repeased. msg.ButtonsTouched = true; msg.Touched = true; } // Battery charge message else if (numberOfBytes == 5) { try { string batteryStatus = "Voltage " + BitConverter.ToUInt16(new byte[] { message[4], message[3]}, 0).ToString(); // Currently write battery status to log file, to get the info, when battery dies //SimpleLog.Write(batteryStatus, true); } catch (Exception exc) { //SimpleLog.Write(exc.Message, false); } } // Joystick else if ((numberOfBytes == 6) && (message[0] == 5 && message[1] == 161 && message[2] == 8 && message[3] == 0)) { int axysX = 0; int axysY = 0; if (message[4] >= 128) axysX = message[4] - 255; else if (message[4] > 0) axysX = message[4]; if (message[5] >= 128) axysY = message[5] - 255; else axysY = message[5]; msg.JoystickX = axysX; msg.JoystickY = axysY; } // Send Zeemote message if (msg.Touched) bgWorkerProcessData.ReportProgress(0, msg); }
public MessageEventArgs(ZeemoteMessage msg) { ZeemoteMessage = msg; }
/// <summary> /// Dispatch zeemote message /// </summary> /// <param name="message"></param> /// <param name="numberOfBytes"></param> public void DispatchBluetoothMessage(byte[] message, int numberOfBytes) { ZeemoteMessage msg = new ZeemoteMessage(); if ((numberOfBytes == 8) && (message[0] == 7 && message[1] == 161 && message[2] == 5 && message[3] == 255)) { // Device is connected. } // Check if message about button else if ((numberOfBytes == 9) && (message[0] == 8 && message[1] == 161 && message[2] == 7)) { for (int i = 3; i <= 6; i++) { if (message[i] == 0) { msg.ButtonA = true; } else if (message[i] == 1) { msg.ButtonB = true; } else if (message[i] == 2) { msg.ButtonC = true; } else if (message[i] == 3) { msg.ButtonD = true; } } // Button was touched even if there are no messages about it. // That means, button was repeased. msg.ButtonsTouched = true; msg.Touched = true; } // Battery charge message else if (numberOfBytes == 5) { try { string batteryStatus = "Voltage " + BitConverter.ToUInt16(new byte[] { message[4], message[3] }, 0).ToString(); // Currently write battery status to log file, to get the info, when battery dies //SimpleLog.Write(batteryStatus, true); } catch (Exception exc) { //SimpleLog.Write(exc.Message, false); } } // Joystick else if ((numberOfBytes == 6) && (message[0] == 5 && message[1] == 161 && message[2] == 8 && message[3] == 0)) { int axysX = 0; int axysY = 0; if (message[4] >= 128) { axysX = message[4] - 255; } else if (message[4] > 0) { axysX = message[4]; } if (message[5] >= 128) { axysY = message[5] - 255; } else { axysY = message[5]; } msg.JoystickX = axysX; msg.JoystickY = axysY; } // Send Zeemote message if (msg.Touched) { bgWorkerProcessData.ReportProgress(0, msg); } }