Exemplo n.º 1
0
        public static void Main()
        {
            int DacValue1 = 10;
            int DacValue2 = 10;

            Debug.Print(Resources.GetString(Resources.StringResources.String1));

            // ADC
            AnalogInput ADC0 = new AnalogInput(ADC.PA1);
            AnalogInput ADC1 = new AnalogInput(ADC.PA2);
            AnalogInput ADC2 = new AnalogInput(ADC.PA3);
            AnalogInput ADC3 = new AnalogInput(ADC.PB0); // PB0 = PA0 ???

            //DAC
            AnalogOutput DAC0 = new AnalogOutput(Cpu.AnalogOutputChannel.ANALOG_OUTPUT_0);
            AnalogOutput DAC1 = new AnalogOutput(Cpu.AnalogOutputChannel.ANALOG_OUTPUT_1);

            DAC0.Scale = 1;
            DAC0.Write(0.8);

            DAC1.Scale = 1;
            DAC1.Write(0.1);

            /* Initialize LEDs */
            LED.LEDInit();
            LED.GreenLedToggle();
            while (true)
            {
                /* Display the ADC converted value */
                //int AdcValue = (ADC0.ReadRaw() * 1);
                //string str = AdcValue.ToString();
                Debug.Print("ADC0 (pin " + ADC0.Pin + ") = " + (ADC0.ReadRaw()));
                Debug.Print("ADC1 (pin " + ADC1.Pin + ") = " + (ADC1.ReadRaw()));
                Debug.Print("ADC2 (pin " + ADC2.Pin + ") = " + (ADC2.ReadRaw()));
                Debug.Print("ADC3 (pin " + ADC3.Pin + ") = " + (ADC3.ReadRaw()));

                Debug.Print("\r\n--------------------------------\r\n");

                /* Wait for 1s */
                Thread.Sleep(250);

                /* Toggle Green LED */
                LED.GreenLedToggle();
                LED.RedLedToggle();

                DacValue1 += 100;
                if (DacValue1 > 4000)
                {
                    DacValue1 = 0;
                }
                DAC0.WriteRaw(DacValue1);

                DacValue2 += 100;
                if (DacValue2 > 4000)
                {
                    DacValue2 = 0;
                }
                DAC1.WriteRaw(DacValue2);
            }
        }
Exemplo n.º 2
0
        public static void Main()
        {
            //one heartbeat 272.72ms
            #region ECG
            int[] ecgValues = new int[] { 238, 238, 238, 238, 238, 238, 238, 248, 258, 269, 280, 294, 308, 313, 318, 313, 308, 301, 294, 287, 280, 273, 266, 259,
                                          252, 245, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 227, 217, 199, 182, 255, 329, 444, 560, 672, 784, 871,
                                          959, 773, 588, 301, 014, 059, 105, 161, 217, 227, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238,
                                          238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 248, 259, 266, 273, 280, 287, 294, 301, 308, 315, 322,
                                          329, 336, 343, 346, 350, 353, 357, 360, 364, 360, 357, 353, 350, 346, 343, 336, 329, 322, 315, 304, 294, 280, 266, 252,
                                          238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 248, 259, 262, 266, 259, 252, 245, 238, 238, 238, 238, 238, 238, 238 };
            #endregion

            #region Constants
            const long TIME_ONE_MS_IN_TICK          = 10000;
            const int  TIME_ONE_MIN_IN_MS           = 60000;
            const int  STATE_DYNAMIC                = 0;
            const int  STATE_STATIC                 = 1;
            const long LENGTH_ONE_HEARTBEAT_IN_TICK = 2727272;
            const int  AVG_ECG_VALUE                = 238;
            const int  BPM_HEART_MAX                = 220;
            const int  BPM_HEART_MIN                = 40;
            #endregion

            #region Var
            Button      btnJoy = new Button(true, new InputPort(GHICard.Socket10.Pin3, false, Port.ResistorMode.Disabled), 100);
            AnalogInput joyX   = new AnalogInput(GHICard.Socket10.AnalogInput4);
            joyX.Scale  = BPM_HEART_MAX - BPM_HEART_MIN;
            joyX.Offset = BPM_HEART_MIN;
            AnalogOutput analogOut         = new AnalogOutput(GHICard.Socket9.AnalogOutput);
            int          state             = STATE_STATIC;
            long         timeToWait        = 10000000; //in tick
            int          index             = 0;
            long         ticksAtLastChange = 0;
            long         ticksAtLastWait   = 0;
            bool         isBeating         = true;
            #endregion

            //Main Loop
            while (true)
            {
                //Inputs management
                if (btnJoy.isPressed())
                {
                    if (state == STATE_DYNAMIC)
                    {
                        state = STATE_STATIC;
                    }
                    else if (state == STATE_STATIC)
                    {
                        state = STATE_DYNAMIC;
                    }
                }

                switch (state)
                {
                case STATE_DYNAMIC:
                    int  bpm = joyX.ReadRaw();
                    long timeTotalHeartbeat = bpm * LENGTH_ONE_HEARTBEAT_IN_TICK;
                    timeToWait = (TIME_ONE_MIN_IN_MS * TIME_ONE_MS_IN_TICK) - timeTotalHeartbeat;
                    break;

                case STATE_STATIC:

                default:
                    break;
                }

                //Outputs management
                long ticks = Utility.GetMachineTime().Ticks;
                if (isBeating)
                {
                    long elapsedTimeHeartbeat = ticks - ticksAtLastChange;
                    if (elapsedTimeHeartbeat >= LENGTH_ONE_HEARTBEAT_IN_TICK / ecgValues.Length)
                    {
                        if (index == ecgValues.Length - 1)
                        {
                            index           = 0;
                            isBeating       = false;
                            ticksAtLastWait = ticks;
                        }
                        else
                        {
                            index++;
                        }

                        ticksAtLastChange = ticks;
                    }
                }
                else
                {
                    long elapsedTimeWait = ticks - ticksAtLastWait;
                    if (elapsedTimeWait >= timeToWait)
                    {
                        isBeating         = true;
                        ticksAtLastChange = ticks;
                    }
                }

                //View
                if (isBeating)
                {
                    analogOut.WriteRaw(ecgValues[index]);
                }
                else
                {
                    analogOut.WriteRaw(AVG_ECG_VALUE);
                }
            }//End while
        }