, , , C, __attribute__ (avr-libc , ).
, https://github.com/ndim/handler-function-table.
:
#ifndef HANDLER_TABLE_H
#define HANDLER_TABLE_H
#include <avr/pgmspace.h>
#define HANDLER_MAX 2
typedef void (*handler_func)(void);
extern const handler_func handler_table_P[HANDLER_MAX] PROGMEM;
#endif
:
#include "handler_table.h"
void handler_foo(void) __attribute__((weak, alias("__handler_default")));
void handler_bar(void) __attribute__((weak, alias("__handler_default")));
const handler_func handler_talbe_P[HANDLER_MAX] PROGMEM = {
handler_foo,
handler_bar
};
__attribute__((weak))
void handler_default(void)
{
}
void __handler_default(void)
{
handler_default();
}
:
#include "handler-table.h"
int main()
{
for (unsigned int i=0; i<HANDLER_MAX; ++i) {
const uint16_t func_addr = pgm_read_word_near(handler_table_P[i]);
const handler_func func = (handler_func) addr;
func();
}
}
Testcase 2 :
void handler_default(void)
{ ... }
void handler_foo(void)
{ ... }
:
:
, .
, ,
, ,
.
.
- ,
,
, .
AVR MCU, .
, PROGMEM
pgm_read_word_near.
, ,
.
,
.
-, ,
, , ,
.
On the other hand ... in some cases, you probably do not need a function table in the first place and several weak functions potentially overridden by non-weak functions, a problem without a table.
source
share