コード例 #1
0
ファイル: KowhaiUtils.cs プロジェクト: othane/kowhai
 private static int _CreateSymbolPath(ref Kowhai.kowhai_tree_t tree, IntPtr targetLocation, out Kowhai.kowhai_symbol_t[] symbolPath)
 {
     int result, symbolPathLength = 5;
     do
     {
         symbolPath = new Kowhai.kowhai_symbol_t[symbolPathLength];
         GCHandle h = GCHandle.Alloc(symbolPath, GCHandleType.Pinned);
         result = kowhai_create_symbol_path2(ref tree, targetLocation, h.AddrOfPinnedObject(), ref symbolPathLength);
         h.Free();
         if (result == Kowhai.STATUS_OK)
             Array.Resize<Kowhai.kowhai_symbol_t>(ref symbolPath, symbolPathLength);
         symbolPathLength *= 2;
     }
     while (result == Kowhai.STATUS_TARGET_BUFFER_TOO_SMALL);
     return result;
 }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: othane/kowhai
        private void ProcessPacket(byte[] buffer)
        {
            KowhaiProtocol.kowhai_protocol_t prot;
            Kowhai.kowhai_symbol_t[] symbols;
            int result = KowhaiProtocol.Parse(buffer, buffer.Length, out prot, out symbols);
            if (result == Kowhai.STATUS_OK)
            {
                switch (prot.header.command)
                {
                    case KowhaiProtocol.CMD_GET_VERSION_ACK:
                        Text = initialFormText + string.Format(" - kowhai server version: {0}", prot.payload.spec.version);
                        CallGetSymbolList();
                        break;
                    case KowhaiProtocol.CMD_GET_TREE_LIST_ACK:
                    case KowhaiProtocol.CMD_GET_TREE_LIST_ACK_END:
                    {
                        if (trees == null || trees.Length != prot.payload.spec.id_list.list_count)
                            trees = new KowhaiProtocol.kowhai_protocol_id_list_item_t[prot.payload.spec.id_list.list_count];
                        KowhaiProtocol.CopyIdList(trees, prot.payload);
                        InitTreeList(trees);

                        buffer = new byte[PACKET_SIZE];
                        int bytesRequired;
                        prot.header.command = KowhaiProtocol.CMD_GET_FUNCTION_LIST;
                        if (KowhaiProtocol.CreateBasicPacket(buffer, PACKET_SIZE, ref prot,
                            out bytesRequired) == Kowhai.STATUS_OK)
                            comms.Send(buffer, bytesRequired);
                        break;
                    }
                    case KowhaiProtocol.CMD_READ_DATA_ACK:
                    case KowhaiProtocol.CMD_WRITE_DATA_ACK:
                    case KowhaiProtocol.CMD_READ_DATA_ACK_END:
                    {
                        byte[] data = KowhaiProtocol.GetBuffer(prot);
                        int nodeOffset;
                        Kowhai.kowhai_node_t node;
                        Kowhai.kowhai_node_t[] descriptor = descriptors[prot.header.id];
                        if (Kowhai.GetNode(descriptor, symbols, out nodeOffset, out node) == Kowhai.STATUS_OK)
                        {
                            KowhaiTree tree = kowhaiTreeMain;
                            tree.UpdateData(data, nodeOffset + prot.payload.spec.data.memory.offset);
                            if (descriptor[0].symbol == (ushort)KowhaiSymbols.Symbols.Constants.Scope)
                            {
                                Kowhai.kowhai_symbol_t[] symbolPath = new Kowhai.kowhai_symbol_t[] {
                                    new Kowhai.kowhai_symbol_t((uint)KowhaiSymbols.Symbols.Constants.Scope),
                                    new Kowhai.kowhai_symbol_t((uint)KowhaiSymbols.Symbols.Constants.Pixels)
                                };
                                if (Kowhai.GetNode(tree.GetDescriptor(), symbolPath, out nodeOffset, out node) == Kowhai.STATUS_OK)
                                {
                                    // update the scope points the the new data
                                    if (ScopePointData == null || ScopePointData.Length != node.count * 2)
                                        Array.Resize(ref ScopePointData, node.count * 2);
                                    int arrayIndex = 0;
                                    if (symbols.Length == 2)
                                        arrayIndex = symbols[1].parts.array_index;
                                    Array.Copy(data, 0, ScopePointData, arrayIndex * 2 + prot.payload.spec.data.memory.offset, data.Length);
                                    for (int i = 0; i < ScopePointData.Length / 2; i++)
                                    {
                                        UInt16 value = BitConverter.ToUInt16(ScopePointData, i * 2);
                                        if (value > ScopeMaxVal)
                                            ScopeMaxVal = value;
                                        if (value < ScopeMinVal)
                                            ScopeMinVal = value;
                                        if (ScopePoints.Count > i)
                                            ScopePoints[i] = value;
                                        else
                                            ScopePoints.Add(value);
                                    }
                                    pnlScope.Invalidate();
                                }
                            }
                        }
                        break;
                    }
                    case KowhaiProtocol.CMD_READ_DESCRIPTOR_ACK:
                    case KowhaiProtocol.CMD_READ_DESCRIPTOR_ACK_END:
                    {
                        if (!descriptors.ContainsKey(prot.header.id))
                            descriptors.Add(prot.header.id, null);
                        Kowhai.kowhai_node_t[] descriptor = descriptors[prot.header.id];
                        if (descriptor == null || descriptor.Length < prot.payload.spec.descriptor.node_count)
                        {
                            Array.Resize<Kowhai.kowhai_node_t>(ref descriptor, prot.payload.spec.descriptor.node_count);
                            descriptors[prot.header.id] = descriptor;
                        }
                        KowhaiProtocol.CopyDescriptor(descriptor, prot.payload);

                        if (prot.header.command == KowhaiProtocol.CMD_READ_DESCRIPTOR_ACK_END)
                        {
                            if (functionCallForm != null)
                            {
                                if (prot.header.id == functionCallForm.FunctionDetails.tree_in_id)
                                {
                                    functionCallForm.TreeInDescriptor = descriptor;
                                    if (functionCallForm.FunctionDetails.tree_out_id != Kowhai.KOW_UNDEFINED_SYMBOL)
                                    {
                                        if (functionCallForm.FunctionDetails.tree_out_id == functionCallForm.FunctionDetails.tree_in_id)
                                        {
                                            functionCallForm.TreeOutDescriptor = descriptor;
                                            functionCallForm.ShowDialog();
                                        }
                                        else
                                            CallGetTreeDescriptor(functionCallForm.FunctionDetails.tree_out_id);
                                    }
                                    else
                                        functionCallForm.ShowDialog();
                                }
                                else if (prot.header.id == functionCallForm.FunctionDetails.tree_out_id)
                                {
                                    functionCallForm.TreeOutDescriptor = descriptor;
                                    functionCallForm.ShowDialog();
                                }
                                else
                                {
                                    MessageBox.Show("Why am i here?");
                                }
                            }
                            else
                            {
                                kowhaiTreeMain.UpdateDescriptor(descriptor, SymbolStrings, null);
                                CallGetTreeData(prot.header.id, descriptor[0].symbol);
                            }
                        }
                        break;
                    }
                    case KowhaiProtocol.CMD_GET_FUNCTION_LIST_ACK:
                    case KowhaiProtocol.CMD_GET_FUNCTION_LIST_ACK_END:
                        if (funcs == null || funcs.Length != prot.payload.spec.id_list.list_count)
                            funcs = new KowhaiProtocol.kowhai_protocol_id_list_item_t[prot.payload.spec.id_list.list_count];
                        KowhaiProtocol.CopyIdList(funcs, prot.payload);
                        InitFunctionList(funcs);
                        break;
                    case KowhaiProtocol.CMD_GET_FUNCTION_DETAILS_ACK:
                        // setup function call form
                        functionCallForm = new FunctionCallForm();
                        functionCallForm.SymbolStrings = SymbolStrings;
                        functionCallForm.FunctionName = new SymbolName(prot.header.id, SymbolStrings[prot.header.id], 0);
                        functionCallForm.FunctionDetails = prot.payload.spec.function_details;
                        functionCallForm.FormClosed += new FormClosedEventHandler(delegate (object sender, FormClosedEventArgs e)
                            { functionCallForm = null; });
                        functionCallForm.CallFunction += new FunctionCallForm.CallFunctionEventHandler(delegate (object sender, FunctionCallForm.CallFunctionEventArgs e)
                            { CallFunction(e.FunctionId, e.Buffer); });
                        // get descriptors for function trees
                        if (prot.payload.spec.function_details.tree_in_id != Kowhai.KOW_UNDEFINED_SYMBOL ||
                            prot.payload.spec.function_details.tree_out_id != Kowhai.KOW_UNDEFINED_SYMBOL)
                        {
                            if (prot.payload.spec.function_details.tree_in_id != Kowhai.KOW_UNDEFINED_SYMBOL)
                                // get input tree descriptor
                                CallGetTreeDescriptor(prot.payload.spec.function_details.tree_in_id);
                            else
                                // get input tree descriptor
                                CallGetTreeDescriptor(prot.payload.spec.function_details.tree_out_id);
                        }
                        else
                            functionCallForm.ShowDialog();
                        break;
                    case KowhaiProtocol.CMD_CALL_FUNCTION_ACK:
                        // do nothing
                        break;
                    case KowhaiProtocol.CMD_CALL_FUNCTION_RESULT:
                    case KowhaiProtocol.CMD_CALL_FUNCTION_RESULT_END:
                        if (functionCallForm != null)
                        {
                            byte[] buf = KowhaiProtocol.GetBuffer(prot);
                            functionCallForm.SetFunctionOutData(buf, prot.payload.spec.function_call.offset);
                            if (prot.header.command == KowhaiProtocol.CMD_CALL_FUNCTION_RESULT_END)
                                ShowToast("Function Call Succeded", 800);
                        }
                        else
                            ShowToast("Why am I here?");
                        break;
                    case KowhaiProtocol.CMD_CALL_FUNCTION_FAILED:
                        ShowToast(string.Format("Error - ProcessPacket(): Function call failed ({0})", prot.header.command));
                        break;
                    case KowhaiProtocol.CMD_GET_SYMBOL_LIST_ACK:
                    case KowhaiProtocol.CMD_GET_SYMBOL_LIST_ACK_END:
                    {
                        // copy buffer to symbolListRaw
                        byte[] buf = KowhaiProtocol.GetBuffer(prot);
                        if (symbolListRaw == null)
                            symbolListRaw = new byte[prot.payload.spec.string_list.list_total_size];
                        Array.Copy(buf, 0, symbolListRaw, prot.payload.spec.string_list.offset, buf.Length);
                        // create new symbols array
                        if (prot.header.command == KowhaiProtocol.CMD_GET_SYMBOL_LIST_ACK_END)
                        {
                            symbolStrings = new List<string>();
                            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                            int index = 0;
                            int count = 0;
                            while (index + count < symbolListRaw.Length)
                            {
                                if (symbolListRaw[index + count] == 0)
                                {
                                    symbolStrings.Add(encoding.GetString(symbolListRaw, index, count));
                                    index += count + 1;
                                    count = 0;
                                }
                                count++;
                            }
                        }
                        break;
                    }
                    case KowhaiProtocol.CMD_EVENT:
                    case KowhaiProtocol.CMD_EVENT_END:
                        if (GetTreeId() == prot.header.id)
                            kowhaiTreeMain.UpdateData(KowhaiProtocol.GetBuffer(prot), prot.payload.spec.event_.offset);
                        else
                            ShowToast(string.Format("Event: {0}", getSymbolName(null, prot.header.id)));
                        break;
                    case KowhaiProtocol.CMD_ERROR_NO_DATA:
                        ShowToast("No tree data");
                        break;
                    default:
                        MessageBox.Show(string.Format("ProcessPacket(): Unknown command ({0})", prot.header.command),
                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                }
            }
            else
            {
                MessageBox.Show(string.Format("ProcessPacket(): KowhaiProtocol.Parse() = {0}", result),
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: Ehsan70/beaverbot
        private void trackBar_Scroll(object sender, EventArgs e)
        {
            byte[] buffer = new byte[PACKET_SIZE];
            int bytesRequired;
            KowhaiProtocol.kowhai_protocol_t prot = new KowhaiProtocol.kowhai_protocol_t();
            prot.header.tree_id = TREE_ID_TEENSY;
            prot.header.command = KowhaiProtocol.CMD_WRITE_DATA;

            Kowhai.kowhai_symbol_t[] symbols;
            byte[] data;

            symbols = new Kowhai.kowhai_symbol_t[] { new Kowhai.kowhai_symbol_t((UInt32)KowhaiSymbols.Symbols.Constants.Teensy),
                                                     new Kowhai.kowhai_symbol_t((UInt16)KowhaiSymbols.Symbols.Constants.GPIO, 5),
                                                     new Kowhai.kowhai_symbol_t((UInt32)KowhaiSymbols.Symbols.Constants.PORT) };
            data = new byte[] { 0 };
            if (trackBar1.Value > trackBar1.Maximum / 2)
                data[0] = 9;
            else if (trackBar1.Value < trackBar1.Maximum / 2)
                data[0] = 18;
            KowhaiProtocol.Create(buffer, PACKET_SIZE, ref prot, symbols, data, 0, out bytesRequired);
            SendData(buffer, bytesRequired);

            symbols = new Kowhai.kowhai_symbol_t[] { new Kowhai.kowhai_symbol_t((UInt32)KowhaiSymbols.Symbols.Constants.Teensy),
                                                     new Kowhai.kowhai_symbol_t((UInt32)KowhaiSymbols.Symbols.Constants.TIMER2),
                                                     new Kowhai.kowhai_symbol_t((UInt32)KowhaiSymbols.Symbols.Constants.OCR2A) };
            data = new byte[] { 0 };
            if (trackBar1.Value > trackBar1.Maximum / 2)
                data[0] = (byte)((trackBar1.Value - trackBar1.Maximum / 2) * 10);
            else if (trackBar1.Value < trackBar1.Maximum / 2)
                data[0] = (byte)((Math.Abs(trackBar1.Value - trackBar1.Maximum / 2)) * 10);
            if (trackBar2.Value > trackBar2.Maximum / 2)
                data[0] = (byte)Math.Max(0, data[0] - ((trackBar2.Value - trackBar2.Maximum / 2) * 10));
            KowhaiProtocol.Create(buffer, PACKET_SIZE, ref prot, symbols, data, 0, out bytesRequired);
            SendData(buffer, bytesRequired);

            symbols = new Kowhai.kowhai_symbol_t[] { new Kowhai.kowhai_symbol_t((UInt32)KowhaiSymbols.Symbols.Constants.Teensy),
                                                     new Kowhai.kowhai_symbol_t((UInt32)KowhaiSymbols.Symbols.Constants.TIMER2),
                                                     new Kowhai.kowhai_symbol_t((UInt32)KowhaiSymbols.Symbols.Constants.OCR2B) };
            data = new byte[] { 0 };
            if (trackBar1.Value > trackBar1.Maximum / 2)
                data[0] = (byte)((trackBar1.Value - trackBar1.Maximum / 2) * 10);
            else if (trackBar1.Value < trackBar1.Maximum / 2)
                data[0] = (byte)((Math.Abs(trackBar1.Value - trackBar1.Maximum / 2)) * 10);
            if (trackBar2.Value < trackBar2.Maximum / 2)
                data[0] = (byte)Math.Max(0, data[0] + ((trackBar2.Value - trackBar2.Maximum / 2) * 10));
            KowhaiProtocol.Create(buffer, PACKET_SIZE, ref prot, symbols, data, 0, out bytesRequired);
            SendData(buffer, bytesRequired);
        }
コード例 #4
0
ファイル: MainForm.cs プロジェクト: othane/kowhai
 private void CallGetTreeData(ushort treeId, ushort baseSymbol)
 {
     KowhaiProtocol.kowhai_protocol_t prot = new KowhaiProtocol.kowhai_protocol_t();
     byte[] buffer = new byte[PACKET_SIZE];
     int bytesRequired;
     prot.header.command = KowhaiProtocol.CMD_READ_DATA;
     prot.header.id = treeId;
     Kowhai.kowhai_symbol_t[] syms = new Kowhai.kowhai_symbol_t[] { new Kowhai.kowhai_symbol_t(baseSymbol, 0) };
     if (KowhaiProtocol.CreateReadDataPacket(buffer, PACKET_SIZE, ref prot,
         syms,
         out bytesRequired) == Kowhai.STATUS_OK)
         comms.Send(buffer, bytesRequired);
 }
コード例 #5
0
ファイル: KowhaiProtocol.cs プロジェクト: Ehsan70/beaverbot
 public static int Parse(byte[] protoPacket, int packetSize, out kowhai_protocol_t protocol, out Kowhai.kowhai_symbol_t[] symbols)
 {
     GCHandle h = GCHandle.Alloc(protoPacket, GCHandleType.Pinned);
     int result = kowhai_protocol_parse(h.AddrOfPinnedObject(), packetSize, out protocol);
     h.Free();
     if (result == Kowhai.STATUS_OK && !IsDescriptorCommand(protocol.header.command))
     {
         symbols = new Kowhai.kowhai_symbol_t[protocol.payload.spec.data.symbols.count];
         h = GCHandle.Alloc(symbols, GCHandleType.Pinned);
         CopyIntPtrs(h.AddrOfPinnedObject(), protocol.payload.spec.data.symbols.array_, Marshal.SizeOf(typeof(Kowhai.kowhai_symbol_t)) * symbols.Length);
         h.Free();
     }
     else
         symbols = null;
     return result;
 }