//================================================================================================ // Questo esempio mostra come accedere agli slot del sistema anche con il linguaggio CSharp // Con il sistema theremino si usa normalmente il VbNet ma per chi è abituato ad Arduino // il linguaggio CSharp é molto più comodo (quasi identico al C di Arduino) //================================================================================================ //------------------------------------------------------------------------------------------------ // Nella funzione "EseguiConPause" si puo' eseguire ogni genere di operazione lenta // Si possono fare PAUSE lunghe a piacere, cicli LOOP, FOR, WHILE etc... //------------------------------------------------------------------------------------------------ // Se si arriva alla fine della funzione allora la funzione ricomincia immediatamente da capo //------------------------------------------------------------------------------------------------ static void EseguiConPause() { float n = ReadSlot(1); if (n >= 0 & n < 250) { WriteSlot(2, 1000); WriteSlot(3, 0); WriteSlot(4, 0); WriteSlot(5, 0); } if (n >= 250 & n < 500) { WriteSlot(2, 0); WriteSlot(3, 1000); WriteSlot(4, 0); WriteSlot(5, 0); } if (n >= 500 & n < 750) { WriteSlot(2, 0); WriteSlot(3, 0); WriteSlot(4, 1000); WriteSlot(5, 0); } if (n >= 750 & n < 1000) { WriteSlot(2, 0); WriteSlot(3, 0); WriteSlot(4, 0); WriteSlot(5, 1000); } //Label1.Text = "Slot1 = " + n.ToString(); } // ------------------------------------------------------------------------------------------------ // Questa funzione viene chiamata 20 volte al secondo // Tutte le visualizzazioni dei dati nei controlli della finestra principale devono avvenire qui // Se si devono leggere o scrivere i valori degli slot con grande frequenza e' bene farlo qui // Niente PAUSE qui dentro, solo esecuzioni piu' rapide possibile // ------------------------------------------------------------------------------------------------ static void EseguiVentiVolteAlSecondo() { } // ################################################################ // LA GESTIONE SEMPLIFICATA FINISCE QUI // MODIFICARE LA PARTE SEGUENTE CON ATTENZIONE // ################################################################ static Form Form1; static MemoryMappedFile MMF1; static Timer Timer1; static System.Threading.Thread objThread; static void Main(String[] pars) { if (Form1 != null) return; Form1 = new Form(); Application.EnableVisualStyles(); Form1.SuspendLayout(); // ------------------------------------------------------------------ Form Form1.Text = "Theremino Script - Led Meter CSharp"; Form1.ClientSize = new Size(400, 80); Form1.MinimumSize = Form1.Size; Form1.StartPosition = FormStartPosition.CenterScreen; // ------------------------------------------------------------------ Form1.ResumeLayout(); Form1.PerformLayout(); Form1.Show(); MMF1 = new MemoryMappedFile("Theremino1", 4080); Timer1 = new Timer(); Timer1.Tick += new EventHandler(Timer1_Tick); Timer1.Interval = 50; Timer1.Start(); StartThread(); while(Form1.Visible) { Application.DoEvents(); System.Threading.Thread.Sleep(10); } } static void WriteSlot(Int32 SlotNumber, float Value) { MMF1.WriteSingle(SlotNumber * 4, Value); } static float ReadSlot(Int32 SlotNumber) { float v = MMF1.ReadSingle(SlotNumber * 4); if (Single.IsNaN(v)) v = 0; return v; } static void StartThread() { // --------------------------------------------------------- asynchronously start the Thread objThread = new System.Threading.Thread(ExecuteThread); // --------------------------------------------------------- make the thread as background thread. objThread.IsBackground = true; // --------------------------------------------------------- set the Priority of the thread. objThread.Priority = System.Threading.ThreadPriority.AboveNormal; // --------------------------------------------------------- start the thread. objThread.Start(); } static void ExecuteThread() { while (true) { EseguiConPause(); PausaMS(1); } } static void PausaMS(Int32 mS) { System.Threading.Thread.Sleep(mS); } static void Beep() { System.Media.SystemSounds.Beep.Play(); } static void Timer1_Tick(Object myObject,EventArgs myEventArgs) { EseguiVentiVolteAlSecondo(); }