/***************************************************************************************************
ExploreEmbedded Copyright Notice
****************************************************************************************************
File: 01-TaskSwitching
Version: 15.0
Author: ExploreEmbedded
Website: http://www.exploreembedded.com/wiki
Description: File contains the free rtos example to demonstarte the task switching.
This code has been developed and tested on ExploreEmbedded boards.
We strongly believe that the library works on any of development boards for respective controllers.
Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT.
ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider
buying the ExploreEmbedded boards.
The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license).
See also: http://www.opensource.org/licenses/bsd-license.php
EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR
INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION
RELATED TO UPDATES.
Permission to use, copy, modify, and distribute this software and its documentation for any purpose
and without fee is hereby granted, provided that this copyright notices appear in all copies
and that both those copyright notices and this permission notice appear in supporting documentation.
**************************************************************************************************/
#include <Arduino_FreeRTOS.h>
#include <time.h>
#include "task.h" /* RTOS task related API prototypes. */
#include "queue.h" /* RTOS queue related API prototypes. */
#include "timers.h" /* Software timer related API prototypes. */
#include "semphr.h"
#include <ncurses.h>
#include <stdlib.h>
#define CLK 13
#define DIN 10
#define CS 11
#define mainKEYBOARD_TASK_PRIORITY (tskIDLE_PRIORITY + 0)
static void demoTaskFunktion(void *pvParameters);
static void vKeyHitTask(void *pvParameters);
static void threadSafePrintw(int x, int y, char *text);
static void printPalken(void *pvParameters);
static TimerHandle_t xTimer = NULL;
SemaphoreHandle_t xSemaphore;
bool quit = false;
#define N_TASKS 10
int TaskData[N_TASKS];
void setup()
{
Serial.begin(9600);
Serial.println(F("In Setup function"));
Serial.begin(115200);
pinMode(CLK, OUTPUT);
pinMode(DIN, OUTPUT);
pinMode(CS, OUTPUT);
/* Create two tasks with priorities 1 and 2. An idle task is also created,
which will be run when there are no tasks in RUN state */
xSemaphore = xSemaphoreCreateBinary();
for (int i = 0; i < N_TASKS; i++)
{
TaskData[i] = i + 1;
xTaskCreate(printPalken, /* The function that implements the task. */
"Demo Task", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */
(void *)&TaskData[i], /* The parameter passed to the task - not used in this simple case. */
mainKEYBOARD_TASK_PRIORITY + i, /* The priority assigned to the task. */
NULL);
}
xTaskCreate(vKeyHitTask, "Keyboard", configMINIMAL_STACK_SIZE, NULL, mainKEYBOARD_TASK_PRIORITY, NULL);
vTaskStartScheduler();
shiftAll(0x0f, 0x00); //display test register - test mode off
shiftAll(0x0b, 0x07); //scan limit register - display digits 0 thru 7
shiftAll(0x0c, 0x01); //shutdown register - normal operation
shiftAll(0x0a, 0x0f); //intensity register - max brightness
shiftAll(0x09, 0x00); //decode mode register - No decode
}
static void threadSafePrintw(int x, int y, char *text)
{
xSemaphoreTake(xSemaphore, 1);
mvprintw(y, x, ((char *)text));
refresh();
xSemaphoreGive(xSemaphore);
}
static void printPalken(void *pvParameters) {
int taskID = *((int *)pvParameters);
int x = taskID * 6;
int max = 10;
int y = max;
int m = 1;
int delay = taskID + 35;
char *text = "#";
char *empty = " ";
for (;;) {
if (m == 1)
{
threadSafePrintw(x, y, text);
y--;
}
else {
threadSafePrintw(x, y, empty);
y++;
}
if (y > max)
{
m = 1;
y = max;
}
if (y < 0)
{
m = 0;
y = 0;
}
vTaskDelay(delay);
}
void shiftAll(byte send_to_address, byte send_this_data)
{
digitalWrite(CS, LOW);
for (int i = 0; i < NUM_SEGMENTS; i++) {
shiftOut(DIN, CLK, MSBFIRST, send_to_address);
shiftOut(DIN, CLK, MSBFIRST, send_this_data);
}
digitalWrite(CS, HIGH);
}
}