Skip to content

Instantly share code, notes, and snippets.

@shazin
Created September 24, 2016 07:28
Show Gist options
  • Save shazin/c59c3b9de54021225d473a1ee9f3e9c0 to your computer and use it in GitHub Desktop.
Save shazin/c59c3b9de54021225d473a1ee9f3e9c0 to your computer and use it in GitHub Desktop.
// Example to demonstrate thread definition, semaphores, and thread sleep.
#include <ChibiOS_AVR.h>
#include <LiquidCrystal.h>
#include <dht.h>
#define DHT11_PIN 8
int humidity = 0;
int temperature = 0;
dht DHT;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Declare a semaphore with an inital counter value of zero.
SEMAPHORE_DECL(sem, 0);
//------------------------------------------------------------------------------
// Thread 1, Input Thread
// 64 byte stack beyond task switch and interrupt needs
static THD_WORKING_AREA(waThread1, 64);
static THD_FUNCTION(Thread1, arg) {
while (1) {
Serial.print("In Input Thread, \t");
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK\n");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
temperature = (int) DHT.temperature;
humidity = (int) DHT.humidity;
chThdSleepMilliseconds(200);
chSemSignal(&sem);
}
}
//------------------------------------------------------------------------------
// Thread 2, Output Thread
// 64 byte stack beyond task switch and interrupt needs
static THD_WORKING_AREA(waThread2, 64);
static THD_FUNCTION(Thread2, arg) {
while (!chThdShouldTerminateX()) {
// Wait for signal from thread 2.
chSemWait(&sem);
Serial.print("In Output Thread : ");
Serial.print(temperature);
Serial.print(", ");
Serial.print(humidity);
Serial.print("\n");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Tem:");
lcd.print((int) temperature);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Hum:");
lcd.print((int) humidity);
lcd.print("%");
chThdSleepMilliseconds(1000);
chSemReset(&sem, 0);
}
}
//------------------------------------------------------------------------------
void setup() {
chBegin(chSetup);
// chBegin never returns, main thread continues with mainThread()
while(1) {
}
}
//------------------------------------------------------------------------------
// main thread runs at NORMALPRIO
void chSetup() {
Serial.begin(9600);
lcd.begin(8, 2);
lcd.clear();
// Start Input Thread
chThdCreateStatic(waThread1, sizeof(waThread1),
NORMALPRIO + 2, Thread1, NULL);
// Start Output Thread
chThdCreateStatic(waThread2, sizeof(waThread2),
NORMALPRIO + 1, Thread2, NULL);
}
//------------------------------------------------------------------------------
void loop() {
// not used
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment