State Machines with C Callbacks

State Machine with Function Pointers

Now how do we benefit from function pointers in the context of a state machine? Simple, just wrap the processing for a given state as a function, and call that:

#include <avr/io.h>
#define F_CPU 20000000UL
#include <util/delay.h>

// States
void led_on();
void led_off();

// State pointer
void (*statefunc)() = led_on;

void led_on() {
  PORTB
|= 1;
  statefunc
= led_off;
}

void led_off() {
  PORTB
&= ~1;
  statefunc
= led_on;
}

int main() {
  DDRB
|= 1;

 
while(1) {
   
(*statefunc)();
    _delay_ms
(1000); // sleep for a second
 
}

 
return 1; // should not get here
}

We can make one further addition: Instead of setting a global function pointer, let’s just have the state function to return a pointer to the next state function. This will make the code a bit cleaner with maybe a few clock cycle overhead. We’ll use a typedef and some type casting to make the code a little easier to read:

#include <avr/io.h>
#define F_CPU 20000000UL
#include <util/delay.h>

typedef void *(*StateFunc)();

// States
void *led_on();
void *led_off();

void *led_on() {
  PORTB
|= 1;
 
return led_off; // next state
}

void *led_off() {
  PORTB
&= ~1;
 
return led_on; // next state
}

int main() {
 
StateFunc statefunc = led_on;

  DDRB
|= 1;

 
while(1) {
    statefunc
= (StateFunc)(*statefunc)();
    _delay_ms
(1000); // sleep for a second
 
}

 
return 1; // should not get here
}

Anyone who can write the code below without a void pointer and a type cast, please give your solution in comments, I’d be interested to see the exact line to do it (extra points if you can typedef the StateFunc as a function pointer that returns a function pointer)!

对状态机的转换函数可以写得抽象写,利用状态机转换表的形式。比如:

/*
		Stat1	Stat2	Stat3	Stat4
Stat1	  0       1       1	      0
Stat2	  0       0       1	      0
Stat3	  1       1       0	      0
Stat4	  0       0       1	      0
*/

bool stateTable[4][4]={false};

typedef void *(*StateFunc)();

// States
void *S1();
void *S2();
void *S3();
void *S4();

void *S1() {
	if(stateTable[0][1]==true)
		return S2;
	else if (stateTable[0][2]==true)
		return S3;
	else
		return S1;
}

void *S2() {
	if(stateTable[0][2]==true)
		return S3;
	else
		return S2;
}

void *S3() {
	if(stateTable[0][0]==true)
		return S1;
	else if (stateTable[0][1]==true)
		return S2;
	else
		return S3;
}

void *S4() {
	if(stateTable[0][3]==true)
		return S3;
	else
		return S4;
}


int main() {
  StateFunc statefunc = S1;

  while(1) {
	modifystateTable();//modify stateTable according to situations
    statefunc = (StateFunc)(*statefunc)();
    _delay_ms(1000); // sleep for a second
  }

  return 1; // should not get here
}



在C语言中,回调函数也是一种常见的编程概念。C语言中的回调函数是通过函数指针来实现的,允许将一个函数作为参数传递给另一个函数,并在特定条件下调用该函数。 以下是一个简单的示例,演示了如何在C语言中使用回调函数: ```c #include <stdio.h> // 声明回调函数类型(函数指针) typedef void (*CallbackFunc)(int); // 接受回调函数作为参数的函数 void performOperation(int value, CallbackFunc callback) { printf("执行操作:%d\n", value); callback(value); // 调用传递的回调函数 } // 回调函数的具体实现 void callbackFunction(int value) { printf("回调函数被调用,传递的值为:%d\n", value); } int main() { int value = 42; performOperation(value, callbackFunction); // 将回调函数作为参数传递给 performOperation 函数 return 0; } ``` 在上面的示例中,我们首先通过 `typedef` 定义了一个函数指针类型 `CallbackFunc`。然后,我们定义了 `performOperation` 函数,它接受一个整数值和一个回调函数作为参数。在 `performOperation` 函数内部,它执行某些操作,并在操作完成后调用传递的回调函数。 在 `main` 函数中,我们定义了一个整数值 `value`,然后将 `callbackFunction` 作为回调函数传递给 `performOperation` 函数。当 `performOperation` 函数完成操作后,它会调用传递的回调函数。 请注意,回调函数的具体实现方式可能因使用的库或框架而有所不同。以上示例仅展示了C语言中的基本回调函数概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值