public MainStationDevice(MainStation _MainStation, string _Name, ProductDataBase.Device _Device, ushort _ID)
 {
     mainstation = _MainStation;
     Name = _Name;
     device = _Device;
     ID = _ID;
 }
Example #2
0
 public GetOutputResult(byte[] _code, MainStation.MainStationCompiler.RegisterEntry _Register)
 {
     Code = _code; Register = _Register;
 }
        public static byte[] Compile(MainStation _MainStation)
        {
            TimerCount = 0;
            byte[] Buffer = new byte[0xffff];//2^16

            _MainStation.Sequence.SortDependencies();

            //global header
            //first 8 bits indicate the amount of timers

            //Event header
            List<MainStationDevice> devices = new List<MainStationDevice>();
            devices.Add(new MainStationDevice(_MainStation, "", null, 0));
            foreach (MainStationDevice d in _MainStation.Devices)
            {
                devices.Add(d);
            }
            List<BaseBlockEvent.Event> events = new List<BaseBlockEvent.Event>();
            foreach (CodeBlock c in _MainStation.Sequence.CodeBlocks)
            {
                if (c is MainStationCodeBlock)
                {
                    MainStationCodeBlock msc = (MainStationCodeBlock)c;
                    if (msc is BaseBlockEvent)
                    {
                        foreach (BaseBlockEvent.Event evnt in ((BaseBlockEvent)msc).Events)
                        {
                            //if (evnt.Output.Connected.Count > 0)//only add the event to the list when there is code attached to it
                            {
                                if (msc is BlockDelay)
                                    TimerCount++;
                                events.Add(evnt);
                            }
                        }
                    }
                }
                else throw new Exception("normal codeblock in sequence for mainstation found, whut !?");
            }
            //add empty events for RTC stuff, will be implemented later
            events.Add(new BaseBlockEvent.Event(0, 2, null));
            events.Add(new BaseBlockEvent.Event(0, 3, null));
            events.Add(new BaseBlockEvent.Event(0, 4, null));
            events.Add(new BaseBlockEvent.Event(0, 5, null));

            //Device Header
            int deviceheadersize = devices.Count * 4 + 2; // including the two blanks
            int eventheadersize = events.Count * 3 + 2;//2 blanks
            int currenteventaddr = HEADERSIZE + deviceheadersize + eventheadersize;
            int didx = 0;
            int eidx = 0;
            foreach (MainStationDevice d in devices)
            {
                byte[] did = Utilities.Utilities.FromShort(d.ID);
                byte[] eaddr = Utilities.Utilities.FromShort((ushort)(HEADERSIZE + deviceheadersize + eidx * 3));
                Buffer[HEADERSIZE + didx * 4 + 0] = did[0];
                Buffer[HEADERSIZE + didx * 4 + 1] = did[1];
                Buffer[HEADERSIZE + didx * 4 + 2] = eaddr[0];
                Buffer[HEADERSIZE + didx * 4 + 3] = eaddr[1];
                foreach (BaseBlockEvent.Event e in events)
                {
                    if (e.DeviceID != d.ID) continue;
                    int addr = HEADERSIZE + deviceheadersize + eidx * 3;
                    eaddr = Utilities.Utilities.FromShort((ushort)currenteventaddr);
                    Buffer[addr + 0] = e.EventID;
                    Buffer[addr + 1] = eaddr[0];
                    Buffer[addr + 2] = eaddr[1];
                    //clear the compiler before we use it
                    UsedRegisters.Clear();
                    BaseBlockEvent block = null;
                    foreach (CodeBlock c in _MainStation.Sequence.CodeBlocks)
                    {
                        if ((MainStationCodeBlock)c is BaseBlockEvent)
                        {
                            foreach (BaseBlockEvent.Event evnt in ((BaseBlockEvent)c).Events)
                            {
                                if (evnt.DeviceID == e.DeviceID && evnt.EventID == e.EventID)
                                {
                                    block = ((BaseBlockEvent)c);
                                }
                            }
                        }
                    }
                    byte[] blob = new byte[] { };
                    if (e.Output != null)
                        blob = block.CompileEvent(e);
                    for (int i = 0; i < blob.Length; i++)
                    {
                        Buffer[currenteventaddr++] = blob[i];
                    }
                    Buffer[currenteventaddr++] = 0;//zero instruction, indicates the end of the event
                    eidx++;
                }
                didx++;
            }
            //fill in header
            // for (int i = 0; i < 32; i++) Buffer[i] = (byte)i;
            Buffer[0] = (byte)TimerCount;

            //fill in the blanks
            Buffer[HEADERSIZE + deviceheadersize - 1] = 0xff;//tag the end of each header with 0xffff
            Buffer[HEADERSIZE + deviceheadersize - 2] = 0xff;
            Buffer[HEADERSIZE + deviceheadersize + eventheadersize - 1] = 0xff;//tag the end each header with 0xffff
            Buffer[HEADERSIZE + deviceheadersize + eventheadersize - 2] = 0xff;
            //mark end of file ( doesnt do anything )
            Buffer[currenteventaddr + 0] = (byte)'E';
            Buffer[currenteventaddr + 1] = (byte)'O';
            Buffer[currenteventaddr + 2] = (byte)'F';

            MemoryStream stream = new MemoryStream();
            stream.Write(Buffer, 0, currenteventaddr + 3);//+3 for EOF ( temporary )

            return stream.ToArray();
        }
        public System.Windows.Forms.TreeNode GetTreeNode(MainStation.MainStationDevice _Device)
        {
            int icon = (int)(_Device.Found ? MainWindow.Icons.Device : MainWindow.Icons.DeviceError);
            var node = new System.Windows.Forms.TreeNode(_Device.Name + "(" + _Device.ID + ")", icon, icon);
            node.Tag = _Device;

            foreach (ProductDataBase.Device.Event e in _Device.device.events)
            {
                int eicon = (int)MainWindow.Icons.EventLocal;
                TreeNode enode = new TreeNode(e.Name, eicon, eicon);
                enode.Tag = e;
                node.Nodes.Add(enode);
            }

            foreach (ProductDataBase.Device.RemoteEvent e in _Device.device.remoteevents)
            {
                int eicon = (int)MainWindow.Icons.EventRemote;
                TreeNode enode = new TreeNode(e.Name,eicon,eicon);
                enode.Tag = e;
                node.Nodes.Add(enode);
            }

            return node;
        }