How to count 'on event' in CANoe Capl? - timer

I'd like to count on event for 200ms.
I tried with this code in CANoe Capl but it's not working well.
I don't know what the problem is.
help me plz.
MainActivity.Capl
variables
{
int timerConditionChecker = 0;
int lockStatusMonitor = 0;
mstimer conutCheckTimer;
}
on timer conutCheckTimer
{
//do nothing
}
on sysvar_update sysvar::Frame2
{
if(timerConditionChecker == 0)
{
lockStatusMonitor++;
timerConditionChecker = 1;
setTimer(conutCheckTimer, 500);
}
else if(timerConditionChecker == 1)
{
if(timeToElapse(conutCheckTimer) > 200)
{
timerConditionChecker = 2;
}
else
{
lockStatusMonitor++;
}
}
else if(timerConditionChecker == 2)
{
timerConditionChecker = 3;
Write("lockStatusMonitorCount = %d",lockStatusMonitor);
}
else{}
}

What about this (I mostly used your variable names):
variables
{
int lockStatusMonitor = 0;
mstimer conutCheckTimer;
}
on timer conutCheckTimer
{
// Is called after 200ms and will output how often the sysvar was updated within these 200ms
Write("lockStatusMonitorCount = %d",lockStatusMonitor);
}
on sysvar_update sysvar::Frame2
{
if(isTimerActive(conutCheckTimer))
{
// In case the 200ms have already started, just count
lockStatusMonitor++;
}
else {
// Timer is not yet active, so start counting for the next 200ms now
lockStatusMonitor = 0; // or = 1 depending on whether your use case
setTimer(conutCheckTimer, 200);
}
}
Apart from that, using the CAPL debugger should help you to solve these kind of problems.

Related

STM32 PWM DMA only works properly if I re-init every time I transfer, otherwise drops first few pulses

As the title says, if I don't include the call to HAL_DMA_Init(&hdma_tim2_ch1) in WS2812_DMA_Stop, my first transfer works fine but all subsequent transfers are missing the first (1-4, usually 3) pulses.
DMA settings:
hdma_tim2_ch1.Instance = DMA1_Channel5;
hdma_tim2_ch1.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tim2_ch1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tim2_ch1.Init.MemInc = DMA_MINC_ENABLE;
hdma_tim2_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_tim2_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tim2_ch1.Init.Mode = DMA_CIRCULAR;
hdma_tim2_ch1.Init.Priority = DMA_PRIORITY_HIGH;
Full code attached below:
void WS2812_DMA_Stop() {
__HAL_TIM_DISABLE_DMA(&htim2, TIM_DMA_CC1);
TIM_CCxChannelCmd(htim2.Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
__HAL_TIM_DISABLE(&htim2);
HAL_DMA_Init(&hdma_tim2_ch1);
ws2812_busy = 0;
}
void WS2812_DMA_ISR(uint8_t tc_flag) {
if (ws2812_current_led < WS2812_NUM_LEDS) {
if (tc_flag) {
WS2812_fill_buffer(ws2812_current_led, &ws2812_dma_buffer[WS2812_DMA_LED_SIZE]);
} else {
WS2812_fill_buffer(ws2812_current_led, &ws2812_dma_buffer[0]);
}
} else if (ws2812_current_led < (WS2812_NUM_LEDS + WS2812_RESET_PULSE_LEN)) {
if (tc_flag) {
memset(&ws2812_dma_buffer[WS2812_DMA_LED_SIZE], 0, sizeof(ws2812_dma_buffer[0]) * WS2812_DMA_LED_SIZE);
} else {
memset(&ws2812_dma_buffer[0], 0, sizeof(ws2812_dma_buffer[0]) * WS2812_DMA_LED_SIZE);
}
} else {
WS2812_DMA_Stop();
}
ws2812_current_led++;
}
void WS2812_DMA_HT(DMA_HandleTypeDef *hdma) {
WS2812_DMA_ISR(0);
}
void WS2812_DMA_TC(DMA_HandleTypeDef *hdma) {
WS2812_DMA_ISR(1);
}
void WS2812_DMA_Error(DMA_HandleTypeDef *hdma) {
_Error_Handler(__FILE__, __LINE__);
}
void WS2812_DMA_Start() {
ws2812_busy = 1;
for (ws2812_current_led = 0; ws2812_current_led < WS2812_DMA_NUM_LEDS; ws2812_current_led++) {
WS2812_fill_buffer(ws2812_current_led, &ws2812_dma_buffer[ws2812_current_led * WS2812_DMA_LED_SIZE]);
}
htim2.hdma[TIM_DMA_ID_CC1]->XferCpltCallback = WS2812_DMA_TC;
htim2.hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = WS2812_DMA_HT;
htim2.hdma[TIM_DMA_ID_CC1]->XferErrorCallback = WS2812_DMA_Error;
HAL_DMA_Start_IT(htim2.hdma[TIM_DMA_ID_CC1], (uint32_t)(&ws2812_dma_buffer[0]), (uint32_t)(&(htim2.Instance->CCR1)), WS2812_DMA_BUF_LEN);
__HAL_TIM_ENABLE_DMA(&htim2, TIM_DMA_CC1);
TIM_CCxChannelCmd(htim2.Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
__HAL_TIM_ENABLE(&htim2);
}
Well, still not 100% sure why, but it looks like the crucial line from HAL_DMA_Init is hdma->State = HAL_DMA_STATE_READY;. Replacing the call to Init with just that line also solves the problem.

Double buffering and printf

I am having trouble with getting my other basic functions to work with double buffer.
for example in the code below, it runs and I can press either down or up to move my cursor, and when I press enter I am supposed to get a printf saying either making a new char, load, or goodbye.
It shows up for a split second but then it immediately disappears. In these situations rewind(stdin) and getchar(); solves this issue but for this code, nothing seems to work.
Please help.
#define _CRT_SECURE_NO_WARNINGS
#include "conioex.h"
#include "DblBuffer.h"
enum //
{
NEW_GAME = 20,
LOAD,
EXIT,
MAX_NUM
};
void main (void)
{
DblBuffer db;
int Cursor_X, Cursor_Y; // cursorlocation
bool Key_flag = false; // pressandtrue
int type = NEW_GAME; // type
Cursor_X = 20;
Cursor_Y = 1;
int flag = 1;
while (flag)
{
for (int i = 1; i <= 3; i++)
{
db.setCursorPos(20,i);
db.write(" ");
}
db.setCursorPos(25,1);
db.write("New Game\n");
db.setCursorPos(25,2);
db.write("Load\n");
db.setCursorPos(25,3);
db.write("Exit\n");
if (inport(PK_DOWN))
{
if (Key_flag == false)
{
Cursor_Y = Cursor_Y + 1;
type = type + 1;
Key_flag = true;
}
}
else if (inport(PK_UP))
{
if (Key_flag == false)
{
Cursor_Y = Cursor_Y - 1;
type = type - 1;
Key_flag = true;
}
}
else if (inport(PK_ENTER))
{
flag = 0;
break;
}
else
{
Key_flag = false;
}
if (Cursor_Y < 1)
{
Cursor_Y = 1;
}
if (Cursor_Y > 3)
{
Cursor_Y = 3;
}
if (type < NEW_GAME)
{
type = NEW_GAME;
}
if (type >= MAX_NUM)
{
type = MAX_NUM - 1;
}
db.setCursorPos(Cursor_X, Cursor_Y);
db.write("→");
db.swap();
}
if(type == NEW_GAME)
{
printf("making a new game");
}
if (type == LOAD)
{
printf("will load");
}
if (type == EXIT)
{
printf("goodbye");
}
rewind(stdin);
getchar();
}
As for your problem, by "then it immediately disappears" I assume that the console window disappears quickly?
That's because the program exits.
You need to flush the input buffer connected to stdin to remove all key presses you made (by reading from stdin) and then call getchar one extra time to get a a kind of confirmation that the user wants to exit.

messagebox and timer in visual studio

Need help.
so i was making a game with my friend for college using visual studio 2017 and there's something weird about the program.
we're set it so that when the life is zero, a message box would show up and chose whether to retry the game or not, but when we tried the game, the message box show's up at least at 20 - 30 second after playing the game and the timer for the game is still going even though we add "timergame.enable = false;".
where's seems to be the problem?
private void timerGame_Tick(object sender, EventArgs e)
{
for (int i = 0; i < listOfSardine.Count; i++)
{
listOfSardine[i].Top += (int)listOfSardine[i].Tag;
if (listOfSardine[i].Bounds.IntersectsWith(pictureBoxGrass.Bounds))
{
userLives--;
labelLives.Text = "Lives: " + userLives;
listOfSardine[i].Dispose();
listOfSardine.RemoveAt(i);
if (userLives == 0)
{
highScore = userScore;
timerBonusSpeed.Enabled = false;
timerGame.Enabled = false;
timerHealth.Enabled = false;
timerMatatabi.Enabled = false;
timerSardine.Enabled = false;
DialogResult dialogResultLose = MessageBox.Show
("Sorry.... you have lost, continue?", "Continue??", MessageBoxButtons.YesNo);
if (dialogResultLose == DialogResult.Yes)
{
for (int j = 0; j < listOfHealth.Count; j++)
{
listOfHealth[j].Dispose();
}
listOfHealth.Clear();
for (int q = 0; q < listOfSardine.Count; q++)
{
listOfSardine[q].Dispose();
}
listOfSardine.Clear();
for (int k = 0; k < listOfMatatabi.Count; k++)
{
listOfMatatabi[k].Dispose();
}
listOfMatatabi.Clear();
userLives = USER_LIVES;
userScore = USER_SCORE;
timerBonusSpeed.Enabled = true;
timerGame.Enabled = true;
timerHealth.Enabled = true;
timerMatatabi.Enabled = true;
timerSardine.Enabled = true;
}
else
{
this.Visible = false;
FormMainMenu formMainMenu = new FormMainMenu();
formMainMenu.Owner = this;
formMainMenu.ShowDialog();
}
}
}
else if (listOfSardine[i].Bounds.IntersectsWith(pictureBoxMainCharacter.Bounds))
{
listOfSardine[i].Dispose();
listOfSardine.RemoveAt(i);
userScore += 1;
labelScore.Text = "Score: " + userScore;
if (userScore % 100 == 0)
{
listOfSardine[i].Top += (int)listOfSardine[i].Tag * 4;
}
if(userScore == 1000)
{
timerBonusSpeed.Enabled = false;
timerGame.Enabled = false;
timerHealth.Enabled = false;
timerMatatabi.Enabled = false;
timerSardine.Enabled = false;
highScore = userScore;
}
}
else
{
listOfSardine[i].Refresh();
}
}
for (int i = 0; i < listOfHealth.Count; i++)
{
listOfHealth[i].Top += (int)listOfHealth[i].Tag;
if (listOfHealth[i].Bounds.IntersectsWith(pictureBoxGrass.Bounds))
{
listOfHealth[i].Dispose();
listOfHealth.RemoveAt(i);
}
else if (listOfHealth[i].Bounds.IntersectsWith(pictureBoxMainCharacter.Bounds))
{
listOfHealth[i].Dispose();
listOfHealth.RemoveAt(i);
userLives++;
labelLives.Text = "Lives: " + userLives;
}
else
{
listOfHealth[i].Refresh();
}
Not enough clear what you did. However, I think you have a counter for life, and which are decreasing as per your game's event. On timer event just check life counter is equal or less than Zero or not. If so, just stop timer, give user confirmation for retry. If user want to retry, then reset life counter and start timer.
Ahh already found the answer
our problem is about the ownership of the form, we just incorrectly insert the ownership for each form
thanks for the people who have answered my question earlier

game maker - how to navigate a map with 2d array

I have to make a navigable map
the starting point is in the center
there are three worlds + the final stage
pressing up I have to navigate from the base to the first level of the first world and go next level in second and third
pressing down I have do go from the third to the second and from the second to the first level and from the first level of the world to the base
pressing left and right I have to change the world
now:
I already made a lot of menus using different methods, but alway using a 1d array
obj_menu:
create event:
///menu
menu[0] = "new";
menu[1] = "load";
menu[2] = "exit";
space = 55;
mpos = 0;
step event:
///move
if(inputs) {
var move = 0;
move -= max(keyboard_check_pressed(vk_up),0);
move += max(keyboard_check_pressed(vk_down),0);
if(move != 0) {
mpos += move;
if(mpos < 0) {
mpos = array_length_1d(saveload) -1;
}
if(mpos > array_length_1d(saveload) -1) {
mpos = 0;
}
}
//push
if(keyboard_check_pressed(vk_enter)) {
scr_menu();
}
}
scr_menu();
switch(mpos) {
case 0: { scr_new_game(); break; } //new
case 1: { scr_load_game(); break; } //load
case 2: { game_end(); break; } //exit
default: { break; }
}
This time I have to navigate in a 2d array
I did this:
obj_map:
create event:
///navigation setup
if(crash_to_base) { lvl[0,0] = true }
if(base_to_ruins_1) { lvl[1,0] = true }
if(ruins_1_to_ruins_2) { lvl[1,1] = true }
if(ruins_2_to_ruins_3) { lvl[1,2] = true }
if(base_to_city_1) { lvl[2,0] = true }
if(city_1_to_city_2) { lvl[2,1] = true }
if(city_2_to_city_3) { lvl[2,2] = true }
if(base_to_lab_1) { lvl[3,0] = true }
if(lab_1_to_lab_2) { lvl[3,1] = true }
if(lab_2_to_lab_3) { lvl[3,2] = true }
if(base_to_castle) { lvl[4,0] = true }
//posizione del menu
mposh = 0;
mposv = 0;
mpos[mposv,mposh] = 0;
step event:
///map navigation
if(inputs) {
moveh -= max(keyboard_check_pressed(vk_left),0);
moveh += max(keyboard_check_pressed(vk_right),0);
movev -= max(keyboard_check_pressed(vk_up),0);
movev += max(keyboard_check_pressed(vk_down),0);
if(moveh != 0) {
//mposh += move;
}
if(movev != 0) {
//mposv += move;
}
push = keyboard_check_pressed(vk_enter);
if(push) {
scr_map();
}
}
how to translate the first method to de sencond need??
Not quite sure what your having difficulty with, perhaps you could elaborate on what exactly the problem is? On a side note however your 1D menu navigation code can be greatly simplified to:
mpos += keyboard_check_pressed(vk_up) - keyboard_check_pressed(vk_down);
var len = array_length_1d(saveload);
if (mpos < 0) { mpos = len - 1; }
if (mpos > len - 1) { mpos = 0; }
and in terms of a 2d map navigation system, it might not be beneficial to use 2d arrays and instead you could use a ds_map which allows you to store all information on each location in one data structure. For instance
var lvlMap = ds_map_create()
lvlMap[?"base-title"] = "Base"
lvlMap[?"base-travel-right"] = "crash"
lvlMap[?"base-travel-left"] = "fortress"
then when you try to move right/left:
var next_location = lvlMap[?current_location+"-travel-"+direction]
current_location = next_location

8051 microcontroller lcd countdown timer

Currently i faced some problem on my lcd screen. I'm try to do a countdown timer but when i set Hour = 0, Min = 1, the sec hit 0 and my hour turn to some unknown character and min = 59, sec = 59. I'm i missing out something?
void Timer1(void) interrupt 3
{
TF1 = 0;
TH1 = 0xB1;
TL1 = 0XE0;
cd_msec--;
if(cd_msec == 0)
{
cd_msec = 99;
cd_sec--;
}
if(cd_sec == 0)
{
cd_sec = 59;
cd_min--;
}
if(cd_min == 0)
{
cd_min = 59;
cd_hour--;
}
if(cd_hour == 0)
{
cd_hour = 0;
}
if(cd_hour == 0 && cd_min == 0)
{
cd_hour = 0;
cd_min = 0;
}
if(cd_hour == 0 && cd_min == 0 && cd_sec == 0)
{
cd_hour = 0;
cd_min = 0;
cd_sec = 0;
cd_msec = 0;
}
}
I agree with #nielsen that the logic is wrong. You may consider following approach to update all the variables properly at every millisecond tick.
Also, I have assigned milli_sec to 999 considering that you will manage to get a 16 bit variable for it.
if (milli_sec > 0)
{
milli_sec--
} else {
if (second > 0) {
milli_sec = 999;
second--;
} else {
if (minute > 0) {
milli_sec = 999;
second = 59
minute--
} else {
if (hour > 0) {
milli_sec = 999;
second = 59;
minute = 59;
hour--
}
else {
//COUNTDOWN REACHED TO 0
//hour,minute,second,milli_second is zero
}
}
}
}
Your logic is pretty wrong.
Assume the pre-condition:
cd_msec=1; cd_sec=1; cd_min=5;
When the code executes, you'll get:
cd_msec=99; cd_sec=59; cd_min=4;
So a single tick changed the countdown more than 1 sec.
Remember that zero is a valid value! I'll suggest that you rewrite the code so that you check for zero before decrementing.
Something like:
if (cd_msec > 0) {
cd_msec--;
}
else
{
if (cd_sec > 0) {
cd_sec--;
cd_msec = 99; // Assummes 10ms ticks
}
else
{
// Handle case with both cd_msec and cd_sec being zero
// ...
// ....
}
}
You have 0H : 1M : 0S.
You check seconds, seconds is zero. You set minutes = 0.
You check minutes, it is now 0. So you subtract 1 from hours. Hours is already zero. So it wraps around to maybe ~65k.
IMHO it would be better to have only msecs and convert to hours:minutes:seconds only when you update the display (if you need).

Resources