Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using directives
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO.Ports;
- #endregion
- namespace ISKNStruct{
- public struct Description {
- public UInt16 physicalWidth;
- public UInt16 physicalHeight;
- public UInt16 clientWidth;
- public UInt16 clientHeight;
- public UInt16 clientPositionX;
- public UInt16 clientPositionY;
- public string deviceID;
- public string firmware;
- }
- public struct Pen3DDataStruct{
- public bool contact;
- public Vector3 pos3D;
- public Vector2 rot2D;
- public UInt16 writtingArea;
- }
- public struct Obj3DDataStruct{
- public Vector3 pos3D;
- public Vector2 rot2D;
- }
- }
- namespace ISKN{
- public enum ObjEventType {NewObject,RemovedObject, HandShake};
- public enum SlateEventType {BOOT, NEW_PAGE, NEW_LAYER, SD_IN, SD_OUT, RESET};
- public enum SlateDiskState {PRESENT_REDEABLE = 0x01, READ = 0x02, WRITTE = 0x04, CHANGE = 0x08,FROM_SD = 0x10, SD_NOT_COMPATIBLE = 0x20}
- public delegate void SendVector3 (Vector3? value);
- public delegate void SendDotAndRotation (Vector3? pos,Vector3? euler);
- public delegate void SendPen2D (Vector2? value,bool contact);
- public delegate void ObjEvent (int idObj,ObjEventType eventType);
- public delegate void SlateEvent (SlateEventType slateEvent);
- public delegate void SendDescription ( ISKNStruct.Description slateDescription);
- public delegate void SendInt (int value);
- public delegate void SendBool (bool value);
- public delegate void SendNothing ();
- public delegate void SendPen3DStruct ( ISKNStruct.Pen3DDataStruct dataStruct );
- public delegate void SendObj3DStruct ( ISKNStruct.Obj3DDataStruct dataStruct );
- public class ISKNSerialConnection : MonoBehaviour
- {
- #region Var
- public Vector3 tempPosMagma;
- public Vector3 posObjet3D;
- public Vector2 rotObjet2D;
- public enum Subscription {STATE = 0x01,OBJECT_EVENT = 0x02, PEN_2D = 0x04,PEN_3D = 0x08, OBJECT_3D = 0x10, ACC = 0x20, EVENT = 0x40, DIST_STATE = 0x80,NULL = 0x00}
- public enum SubscriptionRight {MAG_MOTION_3D = 0x80,MAG_MES_RAW = 0x40, MAG_MES_CAL = 0x20,MAG_MES_USED = 0x10,NULL = 0x00}
- public event SendVector3 OnNewDot = new SendVector3(delegate(Vector3? value){} );
- public event SendVector3 OnNewPos = new SendVector3(delegate(Vector3? value){} );
- public event SendVector3 OnNewMoment = new SendVector3(delegate(Vector3? value){} );
- public event SendVector3 OnNewAcc = new SendVector3(delegate(Vector3? value){} );
- public event SendDotAndRotation OnNewCamera = new SendDotAndRotation(delegate(Vector3? pos, Vector3?euler){});
- public event SendPen2D OnNew2DDot = new SendPen2D(delegate(Vector2? value,bool contact){} );
- public event ObjEvent OnObjectEvent = new ObjEvent(delegate(int idObj,ObjEventType EventType){} );
- public event SlateEvent OnNewSlateEvent = new SlateEvent(delegate (SlateEventType slateEvent) {} );
- public event SendNothing OnTapDetected = new SendNothing(delegate (){} );
- public event SendBool OnSerialPortOpen = new SendBool(delegate (bool value){} );
- public event SendInt OnBatteryStateReceived = new SendInt(delegate (int value) {} );
- public event SendBool OnSDCardStateReceived = new SendBool(delegate (bool value) {} );
- public event SendPen3DStruct OnNewPen3D = new SendPen3DStruct(delegate(ISKNStruct.Pen3DDataStruct dataStruct){} );
- public event SendObj3DStruct OnNewObj3D = new SendObj3DStruct(delegate(ISKNStruct.Obj3DDataStruct dataStruct){} );
- public event SendDescription OnSlateDescriptionReceived = new SendDescription(delegate (ISKNStruct.Description slateDescription) {} );
- private SerialPort sp;
- public string[] list;
- // Decoding state of FIFO
- enum state {receivingBloc, sync, waiting_header_byte_0, waiting_header_byte_1, waiting_header_byte_2}
- state cur_state;
- int readTimeOutCount;
- // List of used buffer type :
- // Buffer = [header] + [bloc 52] + [4] + [CRC]
- // static byte maskObjEvent = 0x02; // 0x0002
- // static byte maskPen2D = 0x04; // 0x0004
- // static byte maskPen3D = 0x08; // 0x0008
- // static byte maskObj3D = 0x10; // 0x0010
- // static byte maskAcc = 0x20; // 0x0020
- // static byte maskEvent = 0x40; // 0x0040
- // static uint maskMag3D = 0x8000; // 0x8000
- private byte[] sub3D={
- 0x33,
- (byte) (Subscription.STATE | Subscription.ACC | Subscription.EVENT | Subscription.OBJECT_EVENT | Subscription.PEN_2D),
- (byte) (SubscriptionRight.MAG_MOTION_3D) }; // block subscription, motion 3D
- private byte[] subdes={0x34,0x02};
- private byte[] header={0xB3,0xA5,0xE1};
- public byte[] sendBuffer;
- private byte[] receiveBuffer;
- public Vector3 pos;
- public Vector3 mom;
- Vector3 lastMom;
- public Vector3 accData;
- // RX FIFO
- private Queue FIFO = new Queue();
- // RX block
- private byte[] receivedBloc;
- private byte idReceivedBloc;
- private byte receivedSize;
- private byte receivedCount;
- #region BlocksDescription
- // Description of blocks
- private const byte stateID = 1;
- private const byte stateSize = 2;
- private const byte descriptionID = 2;
- private const byte descriptionSize = 36;
- private const byte objEventID=3;
- private const byte objEventSize=2;
- private const byte blockPen2DID=4;
- private const byte blockPen2DSize=9;
- private const byte blockPen3DID=5;
- private const byte blockPen3DSize=13;
- private const byte object3DID = 6;
- private const byte object3DSize = 10;
- private const byte blocAccID=7;
- private const byte blocAccSize=13;
- private const byte eventID=8;
- private const byte eventSize=1;
- private const byte stateDiskID = 9;
- private const byte stateDiskSize = 7;
- private const byte fileDescriptionID = 10;
- private const byte fileDescriptionSize = 13;
- private const byte fileDataID = 11;
- private const byte fileDataSize = 77;
- private const byte fileOPID = 12;
- private const byte fileOPSize = 33;
- private const byte bloc3DID=49;
- private const byte bloc3Dsize=24;
- #endregion
- // time
- private float elapsed;
- public float deltaRefresh=0.01f; //temps de rafraichissement en secondes
- private const int toReadWithoutPen = 200;
- private const int toReadWithPen = 1000;
- private int toRead = toReadWithoutPen;
- public float coefAcc = -10;
- public float accFactorInterupt = 0.3f;
- public float accTimeEngage = 0.5f;
- private Vector3? lastAccCapture = null;
- private float accTimeBeforeEngage = 0;
- float timeLastHandshake ;
- // Needed table for CRC
- private UInt16[] crc16tab= {
- 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
- 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
- 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
- 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
- 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
- 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
- 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
- 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
- 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
- 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
- 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
- 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
- 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
- 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
- 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
- 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
- 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
- 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
- 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
- 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
- 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
- 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
- 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
- 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
- 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
- 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
- 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
- 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
- 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
- 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
- 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
- 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
- };
- #endregion
- // Use this for initialization
- void Start () {
- cur_state=state.waiting_header_byte_0;
- list=SerialPort.GetPortNames();
- timeLastHandshake = Time.time +2;
- // No Ports, quit
- if(list == null || list.Length ==0)
- {
- return;
- }
- // TODO better, this code just open the last COM port available
- sp=new SerialPort("\\\\.\\" + list[list.GetLength(0)-1],921600,Parity.None,8,StopBits.One);
- // 200ms Timeout
- sp.ReadTimeout=1;
- sp.Handshake = Handshake.None;
- sp.ReadBufferSize = 8192;
- sp.WriteBufferSize = 128;
- sp.RtsEnable=true;
- sp.DtrEnable=true;
- // Open it!
- try{
- if(sp.IsOpen)
- sp.Close();
- sp.Open();
- OnSerialPortOpen(true);
- Debug.Log(">> Start : " + list[list.GetLength(0)-1]+" is now open");
- // Ask Slate for notifications (subscription)
- subscription();
- }
- catch (Exception){
- Debug.Log("Can't connect to the Slate");
- }
- receiveBuffer=new byte[10000];
- }
- void reconnect()
- {
- cur_state=state.waiting_header_byte_0;
- list=SerialPort.GetPortNames();
- // No Ports, quit
- if(list == null || list.Length ==0)
- {
- return;
- }
- sp=new SerialPort("\\\\.\\" + list[list.GetLength(0)-1],921600,Parity.None,8,StopBits.One);
- // 200ms timeout
- sp.ReadTimeout=1;
- sp.Handshake = Handshake.None;
- sp.ReadBufferSize = 8192;
- sp.WriteBufferSize = 128;
- sp.RtsEnable=true;
- sp.DtrEnable=true;
- // Open COM port
- try{
- sp.Open();
- Debug.Log(">> Start : " + list[list.GetLength(0)-1]+" is now open");
- // Ask Slate for notifications (subscription)
- subscription();
- receiveBuffer=new byte[10000];
- }
- catch(Exception ){
- Debug.Log("Can't reconnect to the Slate");
- }
- }
- void OnApplicationQuit(){
- if(this.sp.IsOpen){
- subScriptionInPlay((byte)Subscription.NULL,(byte)SubscriptionRight.NULL);
- sp.Close();
- }
- }
- // Update is called once per frame
- void FixedUpdate () {
- try
- {
- int nread= sp.Read(receiveBuffer,0,toRead);
- for(int i=0;i<nread;i++)
- {
- FIFO.Enqueue((byte)receiveBuffer[i]);
- }
- }
- catch(Exception)
- {
- if(Time.time - timeLastHandshake > 2){
- //reconnect();
- timeLastHandshake = Time.time;
- }
- }
- while(FIFO.Count !=0 ) //pour chaque élément reçu
- {
- byte readed=(byte)FIFO.Dequeue();
- switch(cur_state)
- {
- case state.waiting_header_byte_0 :
- if (readed==0xB3)cur_state=state.waiting_header_byte_1;
- else cur_state=state.waiting_header_byte_0;
- break;
- case state.waiting_header_byte_1 :
- if (readed==0xA5)cur_state=state.waiting_header_byte_2;
- else cur_state=state.waiting_header_byte_0;
- break;
- case state.waiting_header_byte_2 :
- if (readed==0xE1)cur_state=state.sync;
- else cur_state=state.waiting_header_byte_0;
- break;
- case state.sync :
- idReceivedBloc=readed;
- //print(">> Update >>state.sync: id : "+readed);
- switch(idReceivedBloc)
- {
- case stateID :
- receivedBloc=new byte[stateSize+2];
- receivedSize=stateSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case descriptionID:
- receivedBloc=new byte[descriptionSize+2];
- receivedSize=descriptionSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case objEventID:
- receivedBloc=new byte[objEventSize+2];
- receivedSize=objEventSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case blockPen2DID:
- receivedBloc=new byte[blockPen2DSize+2];
- receivedSize=blockPen2DSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case blockPen3DID:
- receivedBloc=new byte[blockPen3DSize+2];
- receivedSize=blockPen3DSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case object3DID:
- receivedBloc=new byte[object3DSize+2];
- receivedSize=object3DSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case blocAccID :
- receivedBloc=new byte[blocAccSize+2];
- receivedSize=blocAccSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case eventID :
- receivedBloc=new byte[eventSize+2];
- receivedSize=eventSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case stateDiskID :
- receivedBloc=new byte[stateDiskSize+2];
- receivedSize=stateDiskSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case fileDescriptionID :
- receivedBloc=new byte[fileDescriptionSize+2];
- receivedSize=fileDescriptionSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case fileOPID :
- receivedBloc=new byte[fileOPSize+2];
- receivedSize=fileOPSize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- case bloc3DID :
- receivedBloc=new byte[bloc3Dsize+2];
- receivedSize=bloc3Dsize+2;
- receivedCount=0;
- cur_state=state.receivingBloc;
- break;
- default :
- cur_state=state.waiting_header_byte_0;
- break;
- }
- break;
- case state.receivingBloc : // Block reception
- receivedBloc[receivedCount]=readed;
- receivedCount++;
- if(receivedCount>=receivedSize) // We got a valid block (full)
- {
- cur_state=state.waiting_header_byte_0;
- parseBlocReceived();
- }
- break;
- default :
- break;
- }
- } //end while
- }
- // buf = need to be the final output buffer, we a good final size
- // First 4 bytes is [header] + [blockType]
- UInt16 CRC_16_ccitt(Byte[] buf)
- {
- UInt16 CRC = 0x0000;
- Int32 counter;
- for( counter = 4; counter < buf.GetLength(0)-2; counter++)
- {
- CRC = (UInt16)( (CRC<<8) ^ (UInt16)crc16tab[(Byte)(((CRC>>8) ^ buf[counter] )& 0x00FF) ] );
- }
- return (UInt16)CRC;
- }
- UInt16 CRC_16_ccitt(Byte[] buf,UInt16 length)
- {
- UInt16 CRC = 0x0000;
- Int32 counter;
- for( counter = 0; counter < length; counter++)
- {
- CRC = (UInt16)( (CRC<<8) ^ (UInt16)crc16tab[(Byte)(((CRC>>8) ^ buf[counter] )& 0x00FF) ] );
- }
- return (UInt16)CRC;
- }
- void parseBlocReceived()
- {
- ushort CRCCheck;
- UInt16 offsetCRC =0;
- switch(idReceivedBloc)
- {
- case stateID :
- offsetCRC +=stateSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onStateReceived();
- break;
- case descriptionID :
- offsetCRC +=descriptionSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onDescriptionReceived();
- break;
- case objEventID :
- offsetCRC +=objEventSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onObjEventReceived();
- break;
- case blockPen2DID :
- offsetCRC +=blockPen2DSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onPen2DReceived();
- break;
- case blockPen3DID :
- offsetCRC +=blockPen3DSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onPen3DReceived();
- break;
- case object3DID :
- offsetCRC +=object3DSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onObject3DReceived();
- break;
- case blocAccID :
- offsetCRC +=blocAccSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onAccEventReceived();
- break;
- case eventID:
- offsetCRC +=eventSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onEventReceived();
- break;
- case stateDiskID:
- offsetCRC +=stateDiskSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onStateDiskReceived();
- break;
- case fileDescriptionID:
- offsetCRC += fileDescriptionSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onFileDescriptionReceived();
- break;
- case fileDataID :
- offsetCRC += fileDataSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onFileDataReceived();
- break;
- case fileOPID :
- offsetCRC += fileOPSize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- onFileOpReceived();
- break;
- case bloc3DID:
- offsetCRC +=bloc3Dsize;
- CRCCheck = CRC_16_ccitt(receivedBloc,offsetCRC);
- if(CRCCheck == (receivedBloc[offsetCRC ] | (receivedBloc[offsetCRC +1] << 8 )))
- on3DReceived();
- break;
- default :
- break;
- }
- }
- void subscription()
- {
- // Global buffer
- sendBuffer=new Byte[8];
- // Add the header
- header.CopyTo(sendBuffer,0);
- // 3D subscribe
- sub3D.CopyTo(sendBuffer,3);
- // CRC calculation
- UInt16 CRC =(UInt16) CRC_16_ccitt(sendBuffer);
- // Split CRC in 2 parts
- sendBuffer.SetValue( (Byte)(CRC & 0xFF),6);
- sendBuffer.SetValue( (Byte)((CRC & 0xFF00)>>8),7);
- // Send the subscription block to the slate
- sp.Write(sendBuffer,0,8);
- }
- void subscription(byte[] sub)
- {
- if(sp.IsOpen == false){
- Debug.LogError("Port is not open");
- return;
- }
- sub3D = sub;
- // Global buffer
- sendBuffer=new Byte[8];
- // Add header
- header.CopyTo(sendBuffer,0);
- // Set the subscription
- sub.CopyTo(sendBuffer,3);
- // CRC calculation
- UInt16 CRC =(UInt16) CRC_16_ccitt(sendBuffer);
- // Split CRC in 2 parts
- sendBuffer.SetValue( (Byte)(CRC & 0xFF),6);
- sendBuffer.SetValue( (Byte)((CRC & 0xFF00)>>8),7);
- // Send the subscription block to the slate
- sp.Write(sendBuffer,0,8);
- }
- public void subScriptionInPlay(byte left, byte right){
- byte[] sub = new byte[3]{0x33,left,right};
- subscription(sub);
- }
- #region ASK FOR UNITARY REQUEST
- public void askState (){ askBlock(0x34,0x01); }
- public void askDescription(){ askBlock(0x34,0x02); }
- public void askPen(){ askBlock(0x34,0x03); }
- public void askPen2D(){ askBlock(0x34,0x04); }
- public void askPen3D(){ askBlock(0x34,0x05); }
- public void askObj3D(){ askBlock(0x34,0x06); }
- public void askAcc(){ askBlock(0x34,0x07); }
- public void askMom(){ askBlock(0x34,0x08); }
- public void askDiskState(){ askBlock(0x34,0x09); }
- public void askCompletDiskList(){ askBlock(0x34,0x0A); }
- #endregion
- #region DISK OPERATION
- void askOpDisk(byte op,UInt64 opCode,UInt16 fileID,UInt32 firstBlock){
- sendBuffer=new Byte[21];
- subdes[0] = 0x35;
- subdes[1] = op;
- // Add header
- header.CopyTo(sendBuffer,0);
- // Add subscription
- subdes.CopyTo(sendBuffer,3);
- sendBuffer.SetValue( (Byte)(opCode & 0xFF) ,5);
- sendBuffer.SetValue( (Byte)(((opCode & 0xFF00)) >>8 ) ,6);
- sendBuffer.SetValue( (Byte)(((opCode & 0xFF0000)) >>16) ,7);
- sendBuffer.SetValue( (Byte)(((opCode & 0xFF000000)) >>24) ,8);
- sendBuffer.SetValue( (Byte)(((opCode & 0xFF00000000)) >>32) ,9);
- sendBuffer.SetValue( (Byte)(((opCode & 0xFF0000000000)) >>40) ,10);
- sendBuffer.SetValue( (Byte)(((opCode & 0xFF000000000000)) >>48) ,11);
- sendBuffer.SetValue( (Byte)(((opCode & 0xFF00000000000000)) >>56) ,12);
- sendBuffer.SetValue( (Byte)((fileID & 0xFF)) ,13);
- sendBuffer.SetValue( (Byte)(((fileID & 0xFF00)) >>8) ,14);
- sendBuffer.SetValue( (Byte)(firstBlock & 0xFF) ,15);
- sendBuffer.SetValue( (Byte)(((firstBlock & 0xFF00)) >>8) ,16);
- sendBuffer.SetValue( (Byte)(((firstBlock & 0xFF0000)) >>16) ,17);
- sendBuffer.SetValue( (Byte)(((firstBlock & 0xFF000000)) >>24) ,18);
- // Calculation of CRC
- UInt16 CRC =(UInt16) CRC_16_ccitt(sendBuffer);
- // Split the CRC in 2 parts
- sendBuffer.SetValue( (Byte)(CRC & 0xFF),19);
- sendBuffer.SetValue( (Byte)((CRC & 0xFF00)>>8),20);
- // Send block
- sp.Write(sendBuffer,0,21);
- }
- #endregion
- void askBlock (byte id,byte subDes){
- sendBuffer=new Byte[7];
- subdes[0] = id;
- subdes[1] = subDes;
- // Add header
- header.CopyTo(sendBuffer,0);
- // Add subscription
- subdes.CopyTo(sendBuffer,3);
- // Calculation of CRC
- UInt16 CRC =(UInt16) CRC_16_ccitt(sendBuffer);
- // Split CRC in 2 parts
- sendBuffer.SetValue( (Byte)(CRC & 0xFF),5);
- sendBuffer.SetValue( (Byte)((CRC & 0xFF00)>>8),6);
- // Send block
- sp.Write(sendBuffer,0,7);
- }
- #region ReceivedEventFunction
- void onStateReceived(){
- int curStateSize;
- bool sdPresence;
- curStateSize =(int) System.BitConverter.ToChar (receivedBloc,0);
- sdPresence = System.BitConverter.ToBoolean (receivedBloc,1);
- OnBatteryStateReceived(curStateSize);
- OnSDCardStateReceived(sdPresence);
- }
- void onDescriptionReceived(){
- ISKNStruct.Description slateDescription;
- slateDescription.physicalWidth = System.BitConverter.ToUInt16(receivedBloc,0);
- slateDescription.physicalHeight = System.BitConverter.ToUInt16(receivedBloc,2);
- slateDescription.clientWidth = System.BitConverter.ToUInt16(receivedBloc,4);
- slateDescription.clientHeight = System.BitConverter.ToUInt16(receivedBloc,6);
- slateDescription.clientPositionX = System.BitConverter.ToUInt16(receivedBloc,8);
- slateDescription.clientPositionY = System.BitConverter.ToUInt16(receivedBloc,10);
- slateDescription.deviceID = System.BitConverter.ToString(receivedBloc,12,20);
- slateDescription.firmware = System.BitConverter.ToString(receivedBloc,32);
- Debug.Log(slateDescription.firmware );
- OnSlateDescriptionReceived(slateDescription);
- }
- void onObjEventReceived()
- {
- byte IDobject=receivedBloc[0];
- switch(receivedBloc[1])
- {
- case 1 :
- // NEW OBJECT
- OnObjectEvent((int)IDobject,ObjEventType.NewObject);
- toRead = toReadWithPen;
- break;
- case 2 :
- toRead = toReadWithoutPen;
- // REMOVED OBJECT
- OnObjectEvent((int)IDobject ,ObjEventType.RemovedObject);
- break;
- case 3 :
- // HANDSHAKE
- timeLastHandshake = Time.time;
- OnObjectEvent((int)IDobject,ObjEventType.HandShake);
- break;
- }
- }
- void onPen2DReceived(){
- bool contact;
- Vector2 pos2D;
- Vector2 rot2D;
- pos2D.x = ((float) System.BitConverter.ToUInt16(receivedBloc,0));
- pos2D.y = ((float) System.BitConverter.ToUInt16(receivedBloc,2));
- rot2D.x = ((float) System.BitConverter.ToUInt16(receivedBloc,4));
- rot2D.y = ((float) System.BitConverter.ToUInt16(receivedBloc,6));
- contact = System.BitConverter.ToBoolean(receivedBloc,8 );
- OnNew2DDot(pos2D,contact);
- }
- void onPen3DReceived() {
- ISKNStruct.Pen3DDataStruct dataStruct;
- dataStruct.pos3D.x = ((float) ushortToShor(System.BitConverter.ToUInt16(receivedBloc,0)));
- dataStruct.pos3D.y = ((float) ushortToShor(System.BitConverter.ToUInt16(receivedBloc,2)));
- dataStruct.pos3D.z = ((float) ushortToShor(System.BitConverter.ToUInt16(receivedBloc,4)));
- dataStruct.writtingArea = System.BitConverter.ToUInt16(receivedBloc,6);
- dataStruct.rot2D.x = ((float) ushortToShor(System.BitConverter.ToUInt16(receivedBloc,8)));
- dataStruct.rot2D.y = ((float) ushortToShor(System.BitConverter.ToUInt16(receivedBloc,10)));
- dataStruct.contact = System.BitConverter.ToBoolean(receivedBloc,12);
- OnNewPen3D(dataStruct);
- }
- short ushortToShor(ushort val){
- short output = 0;
- output = Convert.ToInt16(val.ToString("X"),16);
- return output;
- }
- void onObject3DReceived(){
- ISKNStruct.Obj3DDataStruct dataStruct;
- dataStruct.pos3D.x = ((float) System.BitConverter.ToInt16(receivedBloc,0));
- dataStruct.pos3D.y = ((float) System.BitConverter.ToInt16(receivedBloc,2));
- dataStruct.pos3D.z = ((float) System.BitConverter.ToInt16(receivedBloc,4));
- dataStruct.rot2D.x = ((float) System.BitConverter.ToInt16(receivedBloc,6));
- dataStruct.rot2D.y = ((float) System.BitConverter.ToInt16(receivedBloc,8));
- OnNewObj3D(dataStruct);
- }
- void onAccEventReceived()
- {
- char op;
- accData.x= coefAcc*System.BitConverter.ToSingle(receivedBloc,0);
- accData.y= coefAcc*System.BitConverter.ToSingle(receivedBloc,4);
- accData.z= coefAcc*System.BitConverter.ToSingle(receivedBloc,8);
- op = System.BitConverter.ToChar(receivedBloc,12);
- #if false
- else if (Vector3.Distance(temp,accData) < -0.5f * coefAcc){
- Vector3? accValueSend = accData;
- OnNewAcc (accValueSend);
- }
- #endif
- Vector3? accValueSend = accData;
- if(lastAccCapture != null){
- accValueSend = accValueSend * 0.1f + lastAccCapture *0.9f;
- }
- OnNewAcc (accValueSend);
- try{
- if( accTimeBeforeEngage < Time.time ){
- float change = (accData.z / lastAccCapture.Value.z);
- if((change < (1 - accFactorInterupt)) || (change > (1 + accFactorInterupt))){
- OnTapDetected ();
- accTimeBeforeEngage = Time.time + accTimeEngage;
- }
- }
- }
- catch (Exception){}
- lastAccCapture = accData;
- }
- void onEventReceived()
- {
- byte maskEvent= receivedBloc[0] ;
- switch (maskEvent)
- {
- case 1 : //Boot
- OnNewSlateEvent(SlateEventType.BOOT);
- break;
- case 2 : //"new Page"
- OnNewSlateEvent(SlateEventType.NEW_PAGE);
- break;
- case 3 : //"new Layer"
- OnNewSlateEvent(SlateEventType.NEW_LAYER);
- break;
- case 4 : //SD card in
- OnNewSlateEvent(SlateEventType.SD_IN);
- break;
- case 5 : //SD card out
- OnNewSlateEvent(SlateEventType.SD_OUT);
- break;
- case 6 : // reset
- OnNewSlateEvent(SlateEventType.RESET);
- break;
- default :
- break;
- }
- }
- void onStateDiskReceived(){
- /* char state;
- UInt32 freeSpace;
- UInt16 countFiles;
- SlateDiskState slateDiskState = new SlateDiskState();
- state = System.BitConverter.ToChar (receivedBloc,0);
- freeSpace = System.BitConverter.ToUInt32 (receivedBloc,1);
- countFiles = System.BitConverter.ToUInt16 (receivedBloc,5);
- slateDiskState = (SlateDiskState) state;
- */
- }
- void onFileDescriptionReceived(){
- UInt16 numberInListFile;
- Int32 size; // Octects
- UInt16 year;
- char month;
- char day;
- char hour;
- char minute;
- char second;
- numberInListFile = System.BitConverter.ToUInt16 (receivedBloc,0);
- size = System.BitConverter.ToInt32 (receivedBloc,2);
- year = System.BitConverter.ToUInt16 (receivedBloc,6);
- month = System.BitConverter.ToChar (receivedBloc,8);
- day = System.BitConverter.ToChar (receivedBloc,9);
- hour = System.BitConverter.ToChar (receivedBloc,10);
- minute = System.BitConverter.ToChar (receivedBloc,11);
- second = System.BitConverter.ToChar (receivedBloc,12);
- Debug.Log("onFileDescriptionReceived");
- Debug.Log(numberInListFile);
- Debug.Log(size);
- Debug.Log(year);
- Debug.Log(month);
- Debug.Log(day);
- Debug.Log(hour);
- Debug.Log(minute);
- Debug.Log(second);
- }
- void onFileDataReceived() {
- UInt16 idFile;
- char transferState;
- UInt32 idBlock;
- UInt32 countBlocks;
- UInt16 bufferSize;
- string buffer;
- idFile = System.BitConverter.ToUInt16 (receivedBloc,0);
- transferState = System.BitConverter.ToChar (receivedBloc,2);
- idBlock = System.BitConverter.ToUInt32 (receivedBloc,3);
- countBlocks = System.BitConverter.ToUInt32 (receivedBloc,7);
- bufferSize = System.BitConverter.ToUInt16 (receivedBloc,11);
- buffer = System.BitConverter.ToString (receivedBloc,13);
- Debug.Log("onFileDataReceived");
- Debug.Log(idFile);
- Debug.Log(transferState);
- Debug.Log(idBlock);
- Debug.Log(countBlocks);
- Debug.Log(bufferSize);
- Debug.Log(buffer);
- }
- void onFileOpReceived() {
- UInt64 packetId;
- UInt64 reserved;
- UInt64 typeId;
- UInt64 blockCode;
- char state;
- packetId = System.BitConverter.ToUInt64 (receivedBloc,0);
- reserved = System.BitConverter.ToUInt64 (receivedBloc,8);
- typeId = System.BitConverter.ToUInt64 (receivedBloc,16);
- blockCode = System.BitConverter.ToUInt64 (receivedBloc,24);
- state = System.BitConverter.ToChar (receivedBloc,32);
- Debug.Log("onFileOpReceived");
- Debug.Log(packetId);
- Debug.Log(reserved);
- Debug.Log(typeId);
- Debug.Log(blockCode);
- Debug.Log(state);
- }
- void on3DReceived()
- {
- // Position in meters
- pos.x=10.0f*System.BitConverter.ToSingle(receivedBloc,0);
- pos.y=10.0f*System.BitConverter.ToSingle(receivedBloc,4);
- pos.z=10.0f*System.BitConverter.ToSingle(receivedBloc,8);
- // Moment (not normalized)
- mom.x=System.BitConverter.ToSingle(receivedBloc,12);
- mom.y=System.BitConverter.ToSingle(receivedBloc,16);
- mom.z=System.BitConverter.ToSingle(receivedBloc,20);
- mom.Normalize();
- mom = toUnityVector(mom,0.4f);
- tempPosMagma = pos;
- OnNewPos(pos);
- OnNewMoment(mom);
- Vector3 ball = pos - mom;
- float lenght = (mom.magnitude * pos.z) / scaleProduct(mom, Vector3.forward);
- Vector3 inter = pos - (mom /lenght) / ( 1f / mom.magnitude);
- OnNewDot(ball);
- OnNewCamera(ball,inter);
- }
- #endregion
- static public Vector3 toUnityVector (Vector3 tabVector,float multiple = 1)
- {
- Vector3 newPos = Vector3.zero;
- newPos.x = tabVector.x * multiple;
- newPos.y = tabVector.y * multiple;
- newPos.z = tabVector.z * multiple;
- return newPos;
- }
- float scaleProduct(Vector3 a, Vector3 b){
- return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement