diff --git a/peripherals/PersonalActuators/I2CDisplay.cs b/peripherals/PersonalActuators/I2CDisplay.cs index cce15cf..d6aadc4 100644 --- a/peripherals/PersonalActuators/I2CDisplay.cs +++ b/peripherals/PersonalActuators/I2CDisplay.cs @@ -21,16 +21,47 @@ namespace Antmicro.Renode.Peripherals public class I2CDisplay : II2CPeripheral, IExternal { DisplayForm display; + Thread thread1, thread2; + private bool _disposedValue; public I2CDisplay() { active = true; this.Log(LogLevel.Debug, "Display i2c connected"); - Thread thread1 = new Thread(StartForm); + + thread1 = new Thread(StartForm) {IsBackground = true}; thread1.Start(); + + thread2 = new Thread(UpdateFrom) {IsBackground = true}; + thread2.Start(); Reset(); } + ~I2CDisplay() => Dispose(false); + + // Public implementation of Dispose pattern callable by consumers. + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + // Protected implementation of Dispose pattern. + protected virtual void Dispose(bool disposing) + { + if (!_disposedValue) + { + if (disposing) + { + display.Close(); + thread2.Abort(); + thread1.Abort(); + } + + _disposedValue = true; + } + } + /** * Method used to invalidate the view of the form and redraw all the pixels (character) * This has been implemented as a while lopp in order to aviod that the display.Invalidate() @@ -39,10 +70,10 @@ public I2CDisplay() */ public void UpdateFrom(){ while(true){ - if(display.needsToBeUpdate()){ + if(display != null && display.needsToBeUpdate()){ display.Invalidate(); - Thread.Sleep(100); } + Thread.Sleep(30); } } @@ -52,8 +83,6 @@ public void UpdateFrom(){ public void StartForm(){ display = new DisplayForm(this); display.Text = "Display 2x" + DisplayForm.numberOfCharPerRow; - Thread thread1 = new Thread(UpdateFrom); - thread1.Start(); display.ShowDialog(); } diff --git a/peripherals/PersonalActuators/IRSensor.cs b/peripherals/PersonalActuators/IRSensor.cs index 63904b3..2112e25 100644 --- a/peripherals/PersonalActuators/IRSensor.cs +++ b/peripherals/PersonalActuators/IRSensor.cs @@ -1,35 +1,74 @@ using System; using System.Threading; +using System.IO; +using Antmicro.Renode.Logging; using Antmicro.Renode.Peripherals.Miscellaneous; +using System.Collections.Generic; namespace Antmicro.Renode.Peripherals { - public class IRSensor : Button + public class IRSensor : Button, IDisposable { - private static readonly Random getrandom = new Random(); - + private Stream _resource; + private bool _disposed; + private bool _disposedValue; + private Queue> queue; + private Semaphore semaphore; private bool active; + Thread thread1; public IRSensor() { + semaphore = new Semaphore(0, 10); active = true; - Thread thread1 = new Thread(TriggerThread); + queue = new Queue>(); + thread1 = new Thread(TriggerThread); thread1.Start(); } - public static int GetRandomNumber(int min, int max) + ~IRSensor() => Dispose(false); + + // Public implementation of Dispose pattern callable by consumers. + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + // Protected implementation of Dispose pattern. + protected virtual void Dispose(bool disposing) { - lock(getrandom) // synchronize + if (!_disposedValue) { - return getrandom.Next(min, max); + if (disposing) + { + thread1.Abort(); + } + + _disposedValue = true; } } // Triggers random press in times between 700 and 3000 ms - public void TriggerThread(){ + private void TriggerThread(){ while(true){ - Thread.Sleep(GetRandomNumber(700, 3000)); - if(active) - PressAndRelease(); + semaphore.WaitOne(); + if(active){ + KeyValuePair element = queue.Dequeue(); + this.Log(LogLevel.Debug, "Run " + element.Key + " triggers every " + element.Value + " ms"); + for(int i = 0; i < element.Key; i++){ + Thread.Sleep(element.Value); + PressAndRelease(); + } + } + } + } + + public void TriggerSensorNTimes(int nTimes, int interval = 450) + { + if(active){ + queue.Enqueue(new KeyValuePair(nTimes, interval)); + this.Log(LogLevel.Debug, "Set " + nTimes + " triggers every " + interval + " ms"); + semaphore.Release(); } } diff --git a/src/i2c_lcd.c b/src/i2c_lcd.c index e8c2c9f..58ab5f9 100644 --- a/src/i2c_lcd.c +++ b/src/i2c_lcd.c @@ -128,21 +128,22 @@ void lcd_send_two_string (char *str1, char * str2) { lcd_send_string(str2); } -void lcd_set_text_downloading() { - lcd_clear(); - lcd_go_home(); - lcd_send_string("Downloading..."); +void lcd_set_text_downloading(){ + lcd_go_home(); + lcd_send_string("Downloading... "); + lcd_put_cur(1,0); + lcd_send_string(" "); } -void lcd_set_number_people(int n_people) { - lcd_clear(); - char num_char[15]; - sprintf(num_char, "%d", n_people); +void lcd_set_number_people(int n_people){ + char num_char[15]; + sprintf(num_char, "%d", n_people); lcd_go_home(); lcd_send_string("Number of people"); lcd_put_cur(1,0); - lcd_send_custom_char(1); + lcd_send_custom_char(1); lcd_send_string(" "); - lcd_send_string(num_char); + lcd_send_string(num_char); + lcd_send_string(" "); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 23bdbd0..c064e90 100644 --- a/src/main.c +++ b/src/main.c @@ -63,8 +63,10 @@ int main(void) { COMM_Init(&hcomm); COMM_StartListen(); + update_interface(); + while (1) { - HAL_Delay(1000); + HAL_Delay(100); } }