Ejemplo n.º 1
0
        public static void Main()
        {
            sid  = new MOS6581(Pins.GPIO_PIN_D10, Pins.GPIO_PIN_D9);
            _usb = new UsbPort("COM4", 250000, _ProcessPacket);
            //Timer usbstatusreport = new Timer(new TimerCallback(_usb.SendPacket), null, 1000, 700);

            sid.reset();
            sid.volume(15);               // set volume to the maximum, 15.

            sid.setMode(0, SID_TRIANGLE); //set voice 0 to a ramp waveform
            sid.setADEnvelope(0, 0, 0);   //Set voice 0's Attack and Decay envelope
            sid.setSREnvelope(0, 15, 0);  //Set voice 0's Sustain and Release envelope

            sid.setFrequency(0, A[4]);

            sid.setVoice(0, true); //Set voice 0 to 'on'.
            //  sid.setVoice(2, true);
            //  sid.setVoice(3, true);

            while (true)
            {
                Thread.Sleep(900);
                _usb.SendPacket();
            }
        }
Ejemplo n.º 2
0
        static void _ProcessPacket(object sender, SerialDataReceivedEventArgs a)
        {
            //long startTime = Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks;
            byte[] inBuf = null;
            int    available;

            do
            {
                available = _usb._serial.BytesToRead;
            } while (available < 14);                       //wait for all bytes before operating on data

            inBuf = new byte[14];
            _usb._serial.Read(inBuf, 0, inBuf.Length);

            string Packet = new string(System.Text.Encoding.UTF8.GetChars(inBuf));
            // Debug.Print(Packet);
            int    RemoteChecksum = Int16.Parse(Packet.Substring(11, 3)); //extract sender's checksum
            int    LocalChecksum  = 0;
            string data           = Packet.Substring(3, 8);

            for (int i = 3; i < 11; i++)
            {
                LocalChecksum += Packet[i];
            }
            //compute our checksum
            LocalChecksum %= 1000;

            if (RemoteChecksum != LocalChecksum)
            {
                return;                                     //stop packet processing if checksums don't match
            }
            string packetType = Packet.Substring(0, 3);

            if (packetType == "$$$")                        //do SID Protocol
            {
                //Debug.Print(Packet);
                string command = data.Substring(0, 3);
                string value   = data.Substring(3);

                switch (command)
                {
                case "ENV":        //set voice envelope
                {
                    int voice   = GetHex(value[0]);
                    int attack  = GetHex(value[1]);
                    int decay   = GetHex(value[2]);
                    int sustain = GetHex(value[3]);
                    int release = GetHex(value[4]);
                    sid.setADEnvelope(voice, attack, decay);
                    sid.setSREnvelope(voice, sustain, release);
                    break;
                }

                case "VFQ":        //voice frequency
                {
                    int voice = GetHex(value[0]);
                    int freq  = Convert.ToInt32(value.Substring(1), 16);
                    sid.setFrequency(voice, freq);
                    break;
                }

                case "VPW":        //voice puslewidth
                {
                    int voice = GetHex(value[0]);
                    int pw    = Convert.ToInt32(value.Substring(1), 16);
                    sid.setPulseWidth(voice, pw);
                    break;
                }

                case "FFQ":        //filter frequency
                {
                    int freq = Convert.ToInt32(value, 16);
                    sid.filterFrequency(freq);
                    break;
                }

                case "FRS":        //filter resonance
                {
                    int res = Convert.ToInt32(value, 16);
                    break;
                }

                case "VFL":        //voice filter mode
                {
                    int  voice = GetHex(value[0]);
                    bool on    = value[4] == '1' ? true : false;
                    sid.setFilterVoice(voice, on);
                    break;
                }

                case "TRG":        //voice enable: on/off
                {
                    int  voice = GetHex(value[0]);
                    bool on    = value[4] == '1' ? true : false;
                    sid.setVoice(voice, on);
                    break;
                }

                case "VWF":        //voice waveform
                {
                    int voice     = GetHex(value[0]);
                    int _waveform = GetHex(value[4]);
                    int waveform  = 1 << (4 + _waveform);
                    sid.setMode(voice, waveform);
                    break;
                }

                case "AMP":        //set volume
                {
                    int volume = GetHex(value[4]);
                    sid.volume(volume);
                    break;
                }

                case "FMO":        //filter mode (bp, hp, lp)
                {
                    int mode = GetHex(value[4]);
                    sid.setFilterMode(1 << mode);
                    break;
                }
                }
            }
            #region boring
            else if (packetType == "###")                        //do Mayes Protocol
            {
                _usb.D0.Write(data[0] == '1' ? true : false);
                _usb.D1.Write(data[1] == '1' ? true : false);
                _usb.D2.Write(data[2] == '1' ? true : false);
                _usb.D3.Write(data[3] == '1' ? true : false);
                _usb.D4.Write(data[4] == '1' ? true : false);
                _usb.D5.Write(data[5] == '1' ? true : false);
                _usb.D6.Write(data[6] == '1' ? true : false);
                _usb.D7.Write(data[7] == '1' ? true : false);
            }
            #endregion
            // long endTime = Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks;
            //Debug.Print((endTime - startTime).ToString());
        }