#include <iostream>

#include "rtk32.h"
#include "rttextio.h"
#include "clock.h"
#include "rtkeybrd.h"
#include "rttarget.h"

#include "processer.h"
#include "KnapTaeller.h"
#include "UrData.h"
#include "interrupt.h"

using namespace std;

#define MB_Tryk_Vis_LEN 4

void main() {

	RTKTaskHandle
		Hnd_KnapTrykTaeller,
		Hnd_VisKnapTryk,
		Hnd_Timer,
		Hnd_Ur,
		Hnd_Operator;

	/* DEFAULT */
	RTKernelInit(3); // proces main får prioritet 3
	RTKPreemptionsON(); // preemptiv skedulering
	CLKSetTimerIntVal(10000); // systemtidsenhed == 10 msek
	KBInit(); // initiering af keyboard
	RTInitMouse(-1, 12, 0, 5, 5); // initiering af mus
	WClearScreen(0x7);
	/* END DEFAULT */

	// SEMAFORER
	RTKSemaphore sem_stop = RTKCreateSemaphore(ST_COUNTING, 0, "mainStop");
	RTKSemaphore sem_knap = RTKCreateSemaphore(ST_COUNTING, 0, "IntKnap");
	RTKSemaphore sem_visur = RTKCreateSemaphore(ST_COUNTING, 0, "UrVis");
	RTKSemaphore sem_trykvis = RTKCreateSemaphore(ST_COUNTING, 0, "TrykVist");
	
	//RTKMailbox mb_Tryk_Vis = RTKCreateMailbox(sizeof(int), MB_Tryk_Vis_LEN, "mb_Tryk_Vis");
	KnapTaeller kt;


	#define COLOR_MAIN 0xA
	WWindow *winMain = WNewWindow(5, 19, 74, 23, 500, COLOR_MAIN,"Proces Main");
	Wprintf(winMain, "Proces Main startet\r\n"); 

	// Opretter porcesser objekter
	Proc_Monitor Proc_m;
	RTKRTLCreateThread(Thread::threadMapper, 4, 0, 0, &Proc_m, "Proc_Monitor");

	Proc_Operator Proc_o( &sem_stop , &kt);
	Hnd_Operator = RTKRTLCreateThread(Thread::threadMapper, 4, 0, 0, &Proc_o, "Proc_operator");

	Proc_VisKnapTryk Proc_VKT( &sem_trykvis , &kt);
	Hnd_VisKnapTryk = RTKRTLCreateThread(Thread::threadMapper, 4, 0, 0, &Proc_VKT, "Proc_KnapTrykTaeller");

	Proc_KnapTrykTaeller Proc_KTT(&sem_knap, &kt );
	Hnd_KnapTrykTaeller = RTKRTLCreateThread(Thread::threadMapper, 4, 0, 0, &Proc_KTT, "Proc_KnapTrykTaeller");
	
	Proc_Ur Proc_ur( &sem_visur );
	Hnd_Ur = RTKRTLCreateThread(Thread::threadMapper, 4, 0, 0, &Proc_ur, "Proc_Ur");

	Proc_Timer Proc_t( &sem_visur );
	Hnd_Timer = RTKRTLCreateThread(Thread::threadMapper, 4, 0, 0, &Proc_t, "Proc_Timer");

	IntHandler ih(5,sem_knap);

	RTKWait(sem_stop);
	cout << "Done.." << endl;
}
