aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Control/DriverWithCallbackExample.c
blob: 881034e40eacb675e2bfd5e6d36d84ea781da0cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * DriverWithCallbackExample.c
 *
 *  Created on: 11 march 2018
 *      Author: shlomo
 */
#include "include.h"
#include "control.h"


uint32_t KeepParameter = 0;
callback_fptr ModuleCallback = 0;
bool isValid (uint32_t deviceID);
uint32_t ControlCallBackFunction(uint32_t deviceID, uint32_t ReadValue);



uint32_t DriverActionWithCallback (uint32_t deviceId, uint32_t parameter, callback_fptr callback)
{
    assert (callback);
    assert (isValid(deviceId));

    //call driver action to device id with the parameter
    //SetMotorSpeed (deviceId, parameter);
    KeepParameter = parameter;
    ModuleCallback = callback;
    //start control:

    uint32_t ControlId = AddControlCallback( callback,  eOneMillisecond, NULL, (IfTypeNone*0x100+deviceId),deviceId, parameter );
    return ControlId;

}

uint32_t ControlCallBackFunction(uint32_t deviceId, uint32_t ReadValue)
{
       if (ReadValue == KeepParameter)
       {
           //stop this control loop
           RemoveControlCallback(deviceId, ControlCallBackFunction );
           //possibly: start regular control (speed etc)
           //uint32_t ControlId = AddControlCallback(ControlCBFunction Callback,  eOneMillisecond, NULL, deviceId, Parameter );

           //call the module callback
           ModuleCallback(deviceId,ReadValue);

       }
   return OK;
}

bool isValid (uint32_t deviceID)
{
    return true;
}
0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Emulations.Emulators;
using Tango.Logging;
using Tango.SharedUI;
using Tango.Core.Commands;
using Tango.Stubs;
using Tango.Transport.Adapters;
using Tango.Transport.Servers;
using Tango.Transport.Transporters;
using Tango.Core;

namespace Tango.MachineEM.UI.ViewModels
{
    public class MainViewVM : ExtendedObject
    {
        private TcpServer TcpServer;
        private bool _running;
        private LogManager logManager = LogManager.Default;

        #region Properties

        private MachineEmulator _emulator;
        /// <summary>
        /// Gets or sets the machine emulator.
        /// </summary>
        public MachineEmulator Emulator
        {
            get { return _emulator; }
            set { _emulator = value; RaisePropertyChanged(nameof(Emulator)); }
        }

        private String _log;
        /// <summary>
        /// Gets or sets the log.
        /// </summary>
        public String Log
        {
            get { return _log; }
            set { _log = value; RaisePropertyChanged(nameof(Log)); }
        }

        /// <summary>
        /// Gets or sets the available stubs.
        /// </summary>
        public List<AvailableStub> AvailableStubs { get; set; }

        private AvailableStub _selectedAvailableStub;
        /// <summary>
        /// Gets or sets the selected available stub.
        /// </summary>
        public AvailableStub SelectedAvailableStub
        {
            get { return _selectedAvailableStub; }
            set
            {
                _selectedAvailableStub = value; RaisePropertyChanged(nameof(SelectedAvailableStub));
                OnStubSelected(value);
            }
        }

        private StubBase _selectedStub;
        /// <summary>
        /// Gets or sets the selected stub.
        /// </summary>
        public StubBase SelectedStub
        {
            get { return _selectedStub; }
            set { _selectedStub = value; RaisePropertyChanged(nameof(SelectedStub)); }
        }

        private List<String> _ports;
        /// <summary>
        /// Gets or sets the ports.
        /// </summary>
        public List<String> Ports
        {
            get { return _ports; }
            set { _ports = value; RaisePropertyChanged(nameof(Ports)); }
        }

        private String _selectedPort;
        /// <summary>
        /// Gets or sets the selected port.
        /// </summary>
        public String SelectedPort
        {
            get { return _selectedPort; }
            set { _selectedPort = value; RaisePropertyChanged(nameof(SelectedPort)); }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Called when available stub is selected.
        /// </summary>
        /// <param name="availableStub">The available stub.</param>
        private void OnStubSelected(AvailableStub availableStub)
        {
            if (availableStub != null)
            {
                SelectedStub = availableStub.CreateInstance(Emulator.Transporter);
            }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Gets or sets the start command.
        /// </summary>
        public RelayCommand StartCommand { get; set; }

        /// <summary>
        /// Gets or sets the stop command.
        /// </summary>
        public RelayCommand StopCommand { get; set; }

        /// <summary>
        /// Gets or sets the run command.
        /// </summary>
        public RelayCommand RunCommand { get; set; }

        /// <summary>
        /// Gets or sets the cancel command.
        /// </summary>
        public RelayCommand CancelCommand { get; set; }

        /// <summary>
        /// Gets or sets the clear command.
        /// </summary>
        public RelayCommand ClearCommand { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewVM"/> class.
        /// </summary>
        public MainViewVM()
        {
            SimpleStringLogger logger = new SimpleStringLogger(LogCategory.Critical, LogCategory.Error, LogCategory.Info, LogCategory.Warning);
            logger.LogReceived += (sedner, log) =>
            {
                Log += log.ToString() + Environment.NewLine;
            };

            logManager.RegisterLogger(logger);

            logManager.Log("Embedded Emulator Started...");


            Emulator = new MachineEmulator(new BasicTransporter());

            StartCommand = new RelayCommand(Start, (x) => !Emulator.IsStarted);
            StopCommand = new RelayCommand(Stop, (x) => Emulator.IsStarted);
            RunCommand = new RelayCommand(RunSelectedStub, (x) => !_running);
            CancelCommand = new RelayCommand(Cancel, (x) => _running);
            ClearCommand = new RelayCommand(() => Log = String.Empty);

            AvailableStubs = StubBase.GetAvailableStubs(StubDirection.ToMobile);

            Ports = new List<string>()
            {
                "TCP",
                "COM1",
                "COM2",
                "COM3",
                "COM4",
                "COM5",
                "COM6",
                "COM7",
                "COM8",
                "COM9",
            };

            SelectedPort = Ports.First();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the ClientConnected event of the TcpServer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ClientConnectedEventArgs"/> instance containing the event data.</param>
        private void TcpServer_ClientConnected(object sender, ClientConnectedEventArgs e)
        {
            Emulator.Transporter.Adapter = new TcpTransportAdapter(e.Socket);
        }

        #endregion

        #region Command Handlers

        /// <summary>
        /// Stops the TCP server and emulator.
        /// </summary>
        private async void Start()
        {
            if (SelectedPort == Ports.First())
            {
                TcpServer = new TcpServer(9999);
                TcpServer.ClientConnected += TcpServer_ClientConnected;
                TcpServer.Start();
            }
            else
            {
                Emulator.Transporter.Adapter = new UsbTransportAdapter(SelectedPort);
            }
            await Emulator.Start();
            InvalidateRelayCommands();
        }

        /// <summary>
        /// Starts the TCP server/USB and emulator.
        /// </summary>
        private async void Stop()
        {
            if (TcpServer != null)
            {
                TcpServer.Stop();
            }
            await Emulator.Stop();
            InvalidateRelayCommands();
        }

        /// <summary>
        /// Runs the selected stub.
        /// </summary>
        private async void RunSelectedStub()
        {
            _running = true;
            InvalidateRelayCommands();
            var result = await SelectedStub.Run((response) =>
            {
                _running = false;
                Log += response + Environment.NewLine;
                InvalidateRelayCommands();

            });

            Log += result + Environment.NewLine;

            _running = false;
            InvalidateRelayCommands();
        }

        private void Cancel()
        {

        }

        #endregion
    }
}