Skip to content

Commit

Permalink
Merged from main
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoBattilana committed Dec 21, 2021
2 parents 56122ca + b4d0bbe commit 4afb4b8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 27 deletions.
39 changes: 34 additions & 5 deletions peripherals/PersonalActuators/I2CDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
}

Expand All @@ -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();
}

Expand Down
61 changes: 50 additions & 11 deletions peripherals/PersonalActuators/IRSensor.cs
Original file line number Diff line number Diff line change
@@ -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<KeyValuePair<int, int>> 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<KeyValuePair<int, int>>();
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<int, int> 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<int, int>(nTimes, interval));
this.Log(LogLevel.Debug, "Set " + nTimes + " triggers every " + interval + " ms");
semaphore.Release();
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/i2c_lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ");
}
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ int main(void) {
COMM_Init(&hcomm);
COMM_StartListen();

update_interface();

while (1) {
HAL_Delay(1000);
HAL_Delay(100);
}

}
Expand Down

0 comments on commit 4afb4b8

Please sign in to comment.