Beispiel #1
0
void Keys_t::ITask() {
    chThdSleepMilliseconds(KEYS_POLL_PERIOD_MS);
    if(App.PThd == nullptr) return;
    // Check keys
    for(uint8_t i=0; i<KEYS_CNT; i++) {
        bool PressedNow = !PinIsSet(KeyData[i].PGpio, KeyData[i].Pin);
        // Check if just pressed
        if(PressedNow and !Key[i].IsPressed) {
            chSysLock();
            Key[i].IsPressed = true;
            Key[i].IsLongPress = false;
            Key[i].IsRepeating = false;
            chEvtSignalI(App.PThd, KeyData[i].EvtMskPress);
            // Reset timers
            RepeatTimer = chTimeNow();
            LongPressTimer = chTimeNow();
            chSysUnlock();
        }
        // Check if just released
        else if(!PressedNow and Key[i].IsPressed) {
            Key[i].IsPressed = false;
            if(!Key[i].IsLongPress) {
                chSysLock();
                chEvtSignalI(App.PThd, KeyData[i].EvtMskRelease);
                chSysUnlock();
            }
        }
        // Check if still pressed
        else if(PressedNow and Key[i].IsPressed) {
            // Check if long press
            if(!Key[i].IsLongPress) {
                if(TimeElapsed(&LongPressTimer, KEY_LONGPRESS_DELAY_MS)) {
                    Key[i].IsLongPress = true;
                    chSysLock();
                    chEvtSignalI(App.PThd, KeyData[i].EvtMskLongPress);
                    chSysUnlock();
                }
            }
            // Check if repeat
            if(!Key[i].IsRepeating) {
                if(TimeElapsed(&RepeatTimer, KEYS_KEY_BEFORE_REPEAT_DELAY_MS)) {
                    Key[i].IsRepeating = true;
                    chSysLock();
                    chEvtSignalI(App.PThd, KeyData[i].EvtMskRepeat);
                    chSysUnlock();
                }
            }
            else {
                if(TimeElapsed(&RepeatTimer, KEY_REPEAT_PERIOD_MS)) {
                    chSysLock();
                    chEvtSignalI(App.PThd, KeyData[i].EvtMskRepeat);
                    chSysUnlock();
                }
            }
        } // if still pressed
    } // for
}
void BStar::VerifyInitNode(int ColorRoot,TreeNode *parent)
{
	TreeNode *aux;

	if(!(parent->MoveCount)) return;

	int ParamJob = DepthEval;
	for(aux = parent->MoveTo(FIRSTCHILD);
		aux; aux = aux->MoveTo(NEXTSIBBLING))
	{
		if(!FixedDepth)
		if((TimeElapsed()-ini) >= TimeLimit )
			break;

		if(aux->OptVal == UNDEFINED)
		{
			SmpWorkers.DoWork(aux,true,ParamJob);
		}
	}
// wait for all workers end his job
	while(!SmpWorkers.AllIdle())SmpWorkers.Sleep();

	for(aux = parent->MoveTo(FIRSTCHILD);
		aux; aux = aux->MoveTo(NEXTSIBBLING))
	{
		TotalNodes += aux->HNCount;
		CalcVerifPrb(aux);
		VerifyInitNode(ColorRoot,aux);
	}
}
Beispiel #3
0
//********************************************************************************
bool CExplosionEffect::Draw(SDL_Surface* pDestSurface)
//Draw the effect.
//
//Returns:
//True if effect should continue, or false if effect is done.
{
	const UINT dwTimeElapsed = TimeElapsed();
	if (dwTimeElapsed >= this->dwDuration)
		return false; //Effect is done.

	if (!pDestSurface) pDestSurface = GetDestSurface();

	//Draw shrinking explosion.
	const float fPercent = (this->dwDuration - dwTimeElapsed) / (float)this->dwDuration;
	ASSERT(fPercent >= 0.0);
	ASSERT(fPercent <= 1.0);
	UINT wTile = TI_EXPLOSION_1;
	if (fPercent < 0.30)
		wTile = TI_EXPLOSION_4;
	else if (fPercent < 0.55)
		wTile = TI_EXPLOSION_3;
	else if (fPercent < 0.80)
		wTile = TI_EXPLOSION_2;

	DrawTile(wTile, pDestSurface);

	//Continue effect.
	return true;
}
Beispiel #4
0
long double 
Bench::TimeRepetitions(unsigned iterations_){
    fIterations = iterations_;
    fMeanTime = 0;
    for(unsigned i = 0; i < iterations_; i++){
        double timeElapsed = TimeElapsed();
        fMeanTime += timeElapsed;

        if(!i || timeElapsed < fMinTime)
            fMinTime = timeElapsed;                
    }

    fMeanTime = (double)(fMeanTime)/iterations_;
    return fMeanTime;
}
void BStar::Run(char *fen)
{
	int TiempoLimiteOld;
	DumpTree *dt;
	ini = TimeElapsed();

	Running = 1;
	PrintPVV = true;
	if(DumpData)
	{
		dt = new DumpTree();
	}
	if(FixedDepth)
	{
		TiempoLimiteOld = TimeLimit = 0;
	}
	else
	{
		TiempoLimiteOld = TimeLimit;
	}
	ExpandCount = 0;
	SelectNodes = 72; //72; 
	VerifyNodes = 40; //40; 
	TotalExpand = 0;
	TotalProbes = 0;
	TotalNodes = 0;

	TimeExpand = 0;
	TimeProbe = 0;

	TargetVal = -UNDEFINED;
	BestMoveAtRoot = NULL;
	RealValBest = 0;
	OptVal2ndBest = 0;
	ColorRoot = White;
	ExpandCount = 0;
	TotalExpand = 0;
	if(TiempoLimiteOld == 0)
		TiempoLimiteOld = 60000;
	
	TimeLimit = TiempoLimiteOld-15;
	if(TimeLimit <= 0)
	{
		TimeLimit = TiempoLimiteOld; //60000-15;
	}
	// antes primero a ver si ya tenemos en memoria el nodo a expandir
	TreeNode *raiz = GetNodeOfFEN(TreeNode::GetRootNode(),fen);
	if(raiz && raiz->SubtreeSize && raiz->MoveTo(FIRSTCHILD))
	{
		if(DumpData)
			Print("info string position in Cache saving %d nodes\n",raiz->SubtreeSize);
		TreeNode *OldRoot = TreeNode::GetRootNode();
		TreeNode::SetRootNode(raiz);
		if(OldRoot && OldRoot != raiz)
			OldRoot->Delete();
	}
	else
	{
		if(DumpData)
			Print("info string Position %s not found\n",fen);
		// limpiar el arbol
		raiz = TreeNode::GetRootNode();
		if(raiz)
			raiz->Delete();
		// establecer el nodo raiz
		raiz = TreeNode::GetFree();
		TreeNode::SetRootNode(raiz);
		strcpy(raiz->fen,fen);

		// expand del nodo raiz
		if(Cancel) {Running = 0;return;}
		Expand(raiz,PLAYER);
	}

	if(Cancel) {Running = 0;return;}
//	if(TimeElapsed()-ini < TimeLimit)
		Search(); 

	if(DumpData)
		Print("info string TransPos %d\n",TransPos);
	if(DumpData)
	{
		dt->Print("Probe depth %d, Select Nodes Limit %d, Verify Nodes limit %d\n",
			DepthEval, SelectNodes, VerifyNodes	);
		dt->Print("Time used %d Nodes Expanded %d\n",TimeElapsed()-ini,TotalExpand);
		int TargetVal = ((BestMoveAtRoot->RealVal) + OptVal2Best())/2;
		dt->Print("Target %d\n",TargetVal);

		dt->DumpRoot();
		dt->Write();
	}
	// Print the best move...
	int BestMove;
	_Board.LoadFen(TreeNode::GetRootNode()->fen); // para movetoAlgebra
	BestMove = TreeNode::GetRootNode()->SelectBestReal()->Move;
	int ColorMove = _Board.MoveToAlgebra(BestMove,BestMoveStr);
	Print("bestmove %s\n",BestMoveStr);

	Running = 0;
	if(DumpData)
	{
		delete dt;
	}
}
void BStar::Search()
{
	int Cancelar = 0;
	int i,NExpandFD = 0;
	int LastIteration = -1;
	Cancelar = 0;
	DumpTree *dt;
	TreeNode *CurNode = NULL;		// last move expanded
	TreeNode *CurBestNode = NULL;	// last best
	if(DumpData)
	{
		dt = new DumpTree();
	}

	TreeNode *a = NULL;
	TreeNode *SelectedNode = NULL;
	ColorRoot = TreeNode::GetRootNode()->Color;

	NExpandFD = 18;
	for(i=1;i < FixedDepth;i++)
	{
		NExpandFD *= 3; // Branch factor 3
	}
	if(FixedDepth)
	{
		SelectNodes = (NExpandFD * 6)/10;
		VerifyNodes = NExpandFD - SelectNodes;
	}

	for(;;)
	{
		if(LastIteration == TotalExpand)
			break;						// avoid cicle without expands.
		LastIteration = TotalExpand;
		if(Cancel||Cancelar)
			break;
//		printf("SELECT Step %d\n",TotalExpand);
		while (GetRealValBestAtRoot() <= OptValAnyOtherMoveAtRoot())
		{
			if(BestMoveAtRoot != CurBestNode)
			{
				if(CurBestNode != NULL)
					PrintPV();
				CurBestNode = BestMoveAtRoot;
				LastChange = TotalExpand;
				PrintPV();
			}
			if(TreeNode::GetRootNode()->RealVal > (MATE-50)
				|| TreeNode::GetRootNode()->RealVal < (50-MATE)
				)
			{
				Cancelar= 1;
				// switch to verify
				break;
			}


			TargetVal = ((BestMoveAtRoot->RealVal) + OptVal2ndBest)/2;
			
			// it is unlikely that any other move can achieve as good a RealVal ?
			if( SecondRootOptPrb() < MinAct)
			{
				if(GetRealValBestAtRoot() >= (RealVal2ndBstMoveAtRoot() + 1))
//					goto salida;
				break;
			}
			if(DumpData )
			{
				dt->Print("SELECT STEP Color %d Best %d Target Value %d\n",ColorRoot,BestMoveAtRoot->RealVal,TargetVal);
				dt->Write();
			}
//			Select Root Node with greatest OptPrb;
			a = GetBestOptPrb(TreeNode::GetRootNode());
			if(!a)
				break;
			if(a != CurNode)
			{
				CurNode = a;
			}
//			Trace down the child subtree selecting
//			For Player-to-Move nodes, child with largest OptPrb
//			For Opponent-to-Move nodes, child with best RealVal
//			Until arriving at a leaf node;
			SelectedNode = a->TraceDownNotStopper(true);
			if(DumpData)
			{
				dt->Print("Selected node:");
				dt->DumpPath(SelectedNode,TreeNode::GetRootNode());
				dt->Print("Color root %d\n",TreeNode::GetRootNode()->Color);
				dt->DumpRoot();
			}
			if(SelectedNode->MoveCount != 0) // already expanded, mate pos?
			{
				break;
			}
//			Get RealVal for each Child Node of this leaf;
//			If it is a Player-to-Move node get OptVals for each Child;
			if(Cancelar||Cancel)
				break;
			Expand(SelectedNode,PLAYER);
			// search path down to new expanded
			SelectedNode = a->TraceDown(true);
			a = SelectedNode;

			if(DumpData)
			{
				dt->Print("Backup Node :");
				dt->DumpPath(a,TreeNode::GetRootNode());
			}

//			Back up Values;
			if(
				SelectedNode->RealVal == 0 
				&& SelectedNode->OptVal == 0
				&& SelectedNode->MoveCount == -1
				)
				Backup(PLAYER,a->MoveTo(PARENT));
			else
				Backup(PLAYER,a);

			// best move at root can change after a Backup
			GetRealValBestAtRoot();
			TargetVal = ((BestMoveAtRoot->RealVal) + OptVal2Best())/2;
			
			BackupPrb(a);

			if(TotalExpand > MaxExpand)
			{
				Cancelar = 1;
				break;
			}
			if(FixedDepth)
			{
				if(TotalExpand > NExpandFD ) //18*(3^FixedDepth))
				{
					Cancelar = 1;
					break; // EffortLimitsExceeded
				}
			}
			else
			if((TimeElapsed()-ini) >= TimeLimit )
			{
				Cancelar = 1;
				break; // EffortLimitsExceeded
			}
			if ((ExpandCount > SelectNodes)) 
			{
				ExpandCount = 0;
				break; // EffortLimitsExceeded
			}

		}
		if(Cancel||Cancelar)
			break;


		TargetVal = RealVal2ndBstMoveAtRoot() + 1;

		if(TargetVal == (-UNDEFINED+1))
			goto salida;
//		printf("VERIFY  Step %d\n",TotalExpand);

		if(DumpData)
		{
			dt->Print("VERIFY Step\n");
			dt->Print("Target Value %d\n",TargetVal);
		}
		
		if(!FixedDepth && (TimeElapsed()-ini) >= TimeLimit )
		{
			break;
		}
		if(FixedDepth)
		{
			if(TotalExpand > NExpandFD) //18*(3^FixedDepth))
			{
				break; // EffortLimitsExceeded
			}
		}

		VerifyInit();
		if(!FixedDepth && (TimeElapsed()-ini) >= TimeLimit )
			break;		
		GetRealValBestAtRoot() ;
		if(BestMoveAtRoot->RealVal < TargetVal && GetRealValBestAtRoot() > OptValAnyOtherMoveAtRoot())
			break; 
		
		if(BestMoveAtRoot->RealVal >= MATE-50 || BestMoveAtRoot->RealVal <= 50-MATE)
			break;

		ExpandCount = 0;
		while(BestMoveAtRoot->RealVal >= TargetVal)
		{
			if(BestMoveAtRoot != CurBestNode)
			{
				CurBestNode = BestMoveAtRoot;
				LastChange = TotalExpand;
				// print info
				PrintPV();
			}

			if(DumpData)
			{
				dt->Print("Verify 1:");
			}

//			Select reply Node with Greatest RealVal
			a = BestMoveAtRoot;
			if(DumpData)
				dt->Print("BestMove Real %d Opt %d Pess %d\n",a->RealVal,a->OptVal,a->PessVal);
//			Trace down the child subtree selecting
//			For Opponent-to-Move nodes, child with largest OptPrb
//			For Player-to-Move nodes, child with best RealVal
//			Until arriving at a leaf node;
			SelectedNode = TraceDownVerify(a);
			if(DumpData)
			{
				dt->Print("Verify Selected node:");
				dt->DumpPath(SelectedNode,TreeNode::GetRootNode());
			}
			if(DumpData)
			{
				dt->Print("Verify 2:");
			}
// TODO
//			if(IsOver(a))
//				return;	// Solved
			if(SelectedNode->RealVal == MATE || SelectedNode->RealVal == -MATE)
				break;
//				goto salida; 
//			Get RealVal for each Child Node of this leaf;
//			If it is an Opponent-to-Move node get OptVals for each Child;
			Expand(SelectedNode,OPPONENT);
			if(SelectedNode->MoveCount > 0)
			{
				SelectedNode = TraceDownVerify(SelectedNode); // Search leaf node 
				Backup(OPPONENT,SelectedNode);			    // and Back up Values;
				if(DumpData)
				{
					dt->Print("Verify Selected node:");
					dt->DumpPath(SelectedNode,TreeNode::GetRootNode());
					dt->DumpRoot();
				}
			}
			else
			{
				if(
					SelectedNode->RealVal == 0 
					&& SelectedNode->OptVal == 0
					&& SelectedNode->MoveCount == -1
					)
					break;
			}
			if(DumpData)
			{
				dt->Print("Verify 3:");
			}
			if(TotalExpand > MaxExpand)
			{
				Cancelar = 1;
				break;
			}
			if(FixedDepth)
			{
				if(TotalExpand > NExpandFD) // 18*(3^FixedDepth))
				{
					goto salida;
					break; // EffortLimitsExceeded
				}
			}
			else
			if (((TimeElapsed()-ini) >= TimeLimit && ExpandCount > 3 )|| (ExpandCount > VerifyNodes))
			{
				ExpandCount = 0;
				if((TimeElapsed()-ini) >= TimeLimit )
				{
					goto salida;
				}
				break;
			}
		}
		if(DumpData)
		{
			int TargetVal = ((BestMoveAtRoot->RealVal) + OptVal2Best())/2;
			dt->Print("Target %d\n",TargetVal);
			dt->Write();
		}
	}
salida:
	PrintPV();
	if(DumpData)
	{
		delete dt;
	}

}
void BStar::PrintPV()
{
	if(!PrintPVV)return;
	int timeUsed;
	char Path[1024];
	int SelDepth = 0;
	TreeNode *aux;
	Path[0] = '\0';
	for(aux = TreeNode::GetRootNode()->SelectBestReal();aux; aux = aux->SelectBestReal())
	{
		strcat(Path,aux->MoveStr);
		strcat(Path," ");
		SelDepth++;
		if(SelDepth > 20) break; // avoid buffer overrun on Path
	}

	timeUsed = TimeElapsed()-ini;
	if(timeUsed <=0)
		timeUsed = 1;
	int nps;
	nps = (TotalExpand * 1000)/ timeUsed;
	int mate;

	int value =  BestMoveAtRoot->RealVal;

	mate = 0;
	if(value >= MATE -50)
	{
		mate = MATE -value;
	}
	if(value <= -MATE +50)
	{
		mate = MATE +value;
		mate = -mate;
	}
	if(!mate)
	{
		Print("info depth %d seldepth %d pv %s score cp %d nodes %d nps %d hashfull %d time %ld\n",
			SelDepth,
			SelDepth,
			Path,
			value,
			TotalExpand ,
			//TotalNodes, //TotalExpand ,
			nps,
			0,	
			timeUsed);
	}
	else
	{
		Print("info depth %d seldepth %d pv %s score mate %d nodes %d nps %d hashfull %d time %ld\n",
			SelDepth,
			SelDepth,
			Path,
			mate/2,
			TotalExpand ,
			//TotalNodes, //TotalExpand ,
			nps,
			0,	
			timeUsed);
	}
}
Beispiel #8
0
// ========================= Postprocessor for PinSns ==========================
void ProcessButtons(PinSnsState_t *BtnState, uint32_t Len) {
//    Uart.Printf("\r%A", p, Len, ' ');
    for(uint8_t i=0; i<BUTTONS_CNT; i++) {
        // ==== Button Press ====
        if(BtnState[i] == BTN_PRESS_STATE) {
#if BTN_LONGPRESS
            IsLongPress[i] = false;
#endif
#if BTN_REPEAT
            IsRepeating[i] = false;
#endif
#if BTN_COMBO // Check if combo
            BtnEvtInfo_t IEvt;
            IEvt.BtnCnt = 0;
            for(uint8_t j=0; j<BUTTONS_CNT; j++) {
                if(BtnState[j] == BTN_HOLDDOWN_STATE or BtnState[j] == BTN_PRESS_STATE) {
                    IEvt.BtnID[IEvt.BtnCnt] = j;
                    IEvt.BtnCnt++;
                    if((j != i) and !IsCombo) {
                        IsCombo = true;
                        AddEvtToQueue(beCancel, j);
                    }
                }
            } // for j
            if(IEvt.BtnCnt > 1) { // Combo
                IEvt.Type = beCombo;
                AddEvtToQueue(IEvt);
                continue;
            }
            else IsCombo = false;
#endif // combo
            // Single key pressed, no combo
            AddEvtToQueue(bePress, i);  // Add single keypress
#if BTN_LONGPRESS
            LongPressTimer = chTimeNow();
#endif
#if BTN_REPEAT
            RepeatTimer = chTimeNow();
#endif
        } // if press

        // ==== Button Release ====
#if BTN_COMBO || BTN_RELEASE
        else if(BtnState[i] == BTN_RELEASE_STATE) {
#if BTN_COMBO // Check if combo completely released
            if(IsCombo) {
                IsCombo = false;
                for(uint8_t j=0; j<BUTTONS_CNT; j++) {
                    if(BtnState[j] == BTN_HOLDDOWN_STATE) {
                        IsCombo = true;
                        break;
                    }
                }
                continue; // do not send release evt (if enabled)
            } // if combo
#endif
#if BTN_RELEASE // Send evt if not combo
            AddEvtToQueue(beRelease, i);
#endif
        }
#endif // if combo or release

        // ==== Long Press ====
#if BTN_LONGPRESS || BTN_REPEAT
        else if(BtnState[i] == BTN_HOLDDOWN_STATE
#if BTN_COMBO
                and !IsCombo
#endif
                ) {
#if BTN_LONGPRESS // Check if long press
            if(!IsLongPress[i]) {
                if(TimeElapsed(&LongPressTimer, BTN_LONGPRESS_DELAY_MS)) {
                    IsLongPress[i] = true;
                    AddEvtToQueue(beLongPress, i);
                }
            }
#endif
#if BTN_REPEAT // Check if repeat
            if(!IsRepeating[i]) {
                if(TimeElapsed(&RepeatTimer, BTN_DELAY_BEFORE_REPEAT_MS)) {
                    IsRepeating[i] = true;
                    AddEvtToQueue(beRepeat, i);
                }
            }
            else {
                if(TimeElapsed(&RepeatTimer, BTN_REPEAT_PERIOD_MS)) {
                    AddEvtToQueue(beRepeat, i);
                }
            }
#endif
        } // if still pressed
#endif
    } // for i
}
Beispiel #9
0
void StartClock() {
  TimeElapsed();
}
Beispiel #10
0
void StopClock() {
  fTotalTime = TimeElapsed();
}
Beispiel #11
0
int32 TimeHandler::TimeElapsedInMilliseconds(TimeCanal canal)
{
	return TimeElapsed(canal).GetMilliseconds();
}
Beispiel #12
0
int32 TimeHandler::TimeElapsedInMinutes(TimeCanal canal)
{
	return TimeElapsed(canal).GetMinutes();
}
Beispiel #13
0
void Keys_t::ProcessKeysState(bool *PCurrentState) {
//    Uart.Printf("\r%A", PCurrentState, KEYS_CNT, ' ');
    // Iterate keys
    for(uint8_t i=0; i<KEYS_CNT; i++) {
        bool PressedNow = KeyIsPressed(PCurrentState[i]);

        // ==== Key Press ====
        if(PressedNow and !Key[i].IsPressed) {
            Key[i].IsPressed = true;
#if KEY_LONGPRESS
            Key[i].IsLongPress = false;
            Key[i].IsRepeating = false;
#endif
            KeyEvtInfo_t IEvt;
            IEvt.KeysCnt = 0;
            // Check if combo
            for(uint8_t j=0; j<KEYS_CNT; j++) {
                if(Key[j].IsPressed) {
                    IEvt.KeyID[IEvt.KeysCnt] = j;
                    IEvt.KeysCnt++;
                    if((j != i) and !IsCombo) {
                        IsCombo = true;
                        AddEvtToQueue(keCancel, j);
                    }
                }
            } // for j
            if(IEvt.KeysCnt == 1) {   // Single key pressed, no combo
                IsCombo = false;
                IEvt.Type = kePress;
#if KEY_LONGPRESS   // Reset timers
                RepeatTimer = chTimeNow();
                LongPressTimer = chTimeNow();
#endif
            }
            else IEvt.Type = keCombo;
            AddEvtToQueue(IEvt);
        } // if became pressed

        // ==== Key Release ====
        else if(!PressedNow and Key[i].IsPressed) {
            Key[i].IsPressed = false;
            // Check if combo completely released
            if(IsCombo) {
                IsCombo = false;
                for(uint8_t j=0; j<KEYS_CNT; j++) {
                    if(Key[j].IsPressed) {
                        IsCombo = true;
                        break;
                    }
                }
            } // if combo
#if KEY_RELEASE // Send evt if not combo
            else AddEvtToQueue(keRelease, i);
#endif
        }

#if KEY_LONGPRESS // ==== Long Press ====
        else if(PressedNow and Key[i].IsPressed and !IsCombo) {
            // Check if long press
            if(!Key[i].IsLongPress) {
                if(TimeElapsed(&LongPressTimer, KEY_LONGPRESS_DELAY_MS)) {
                    Key[i].IsLongPress = true;
                    AddEvtToQueue(keLongPress, i);
                }
            }
            // Check if repeat
            if(!Key[i].IsRepeating) {
                if(TimeElapsed(&RepeatTimer, KEYS_DELAY_BEFORE_REPEAT_MS)) {
                    Key[i].IsRepeating = true;
                    AddEvtToQueue(keRepeat, i);
                }
            }
            else {
                if(TimeElapsed(&RepeatTimer, KEY_REPEAT_PERIOD_MS)) {
                    AddEvtToQueue(keRepeat, i);
                }
            }
        } // if still pressed
#endif
    } // for i
}