// LCD1602 to Arduino Uno connection example

// Includes

#include "proto_activities.h"

#include <dht.h>
#include <LiquidCrystal.h>

// Globals

dht dht;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

// Activities

pa_activity (EnvSensor, pa_ctx_tm(), float& hum, float& temp) {
  // Read the sensor once every second
  pa_every_s (1) {
    int res = dht.read22(5);
    if (res == DHTLIB_OK) {
      hum = dht.humidity;
      temp = dht.temperature;
    }
  } pa_every_end
} pa_end

pa_activity (HumUI, pa_ctx(), float hum) {
  lcd.clear();
  lcd.print("Humid. ");
  lcd.print(hum);
  lcd.print(" %RH");
  pa_halt; // Dont end this activity - let it be aborted by caller instead
} pa_end

pa_activity (TempUI, pa_ctx(), float temp) {
  lcd.clear();
  lcd.print("Temp. ");
  lcd.print(temp);
  lcd.print(" DegC");
  pa_halt; // Dont end this activity - let it be aborted by caller instead
} pa_end

pa_activity (UI, pa_ctx_tm(pa_use(HumUI); pa_use(TempUI)), float hum, float temp) {
  // Setup lcd
  lcd.begin(16, 2);

  // Alternate between Humidity UI and Temperature UI - abort each after 3 seconds
  pa_repeat {
    pa_after_s_abort (3, HumUI, hum);
    pa_after_s_abort (3, TempUI, temp);
  }
} pa_end

pa_activity (Main, pa_ctx(pa_co_res(2); pa_use(EnvSensor); pa_use(UI); float hum; float temp)) {  
  // Concurrently run env sensor with UI
  pa_co(2) {
    pa_with (EnvSensor, pa_self.hum, pa_self.temp);
    pa_with (UI, pa_self.hum, pa_self.temp);
  } pa_co_end
} pa_end

pa_use(Main);

// Setup and loop

void setup() {}

void loop() {
  pa_tick(Main);
}