原创 GCC转换为类成员的指针的另一方法(TestCode)

2008-2-10 01:38 3858 5 5 分类: MCU/ 嵌入式

原帖出处: http://www.21icbbs.com/club/bbs/ShowAnnounce.asp?v=&ID=1718828



GCC转换为类成员的指针的另一方法
mood.gif testcode 发表于 2005-7-1 05:47 AVR 单片机 ←返回版面 按此察看该网友的资料 按此把文章加入收藏夹 按此编辑本帖


GCC转换为类成员的指针问题:
今天找到另一种方法,使用memcpy_P而不用pgm_read_word,将其转换为类成员的指针,测试通过。

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <compat/ina90.h>

class KeyObj ;

typedef prog_void (KeyObj::* PFV)(void);

#define KeyCP     (1)
#define KeyCLK    (2)
#define KeyData   (3)

class KeyObj {
private:
  unsigned char KeyVal;  
  unsigned int KeyTest[8];
public:
    unsigned char KeyCount;
  KeyObj(void);
  void KeyInit(void);
  void SetKeyVal(unsigned char keyval);
  void SetKeyCount(unsigned char count);
  unsigned char GetKeyVal(void);//
  void KeyCommandExec(unsigned char);
  static const PFV KeyCommandTab[3][5];
  void Exec(void);
  void Key00(void);
...
  void Key24(void);
};

inline unsigned char KeyObj::GetKeyVal(void)
{
unsigned char KeyVal = 0;
  PORTC &= ~(1 << KeyCP);
  _NOP();//??
  PORTC |=  (1 << KeyCP);
  for(int i = 8; i > 0; i --)
  {
    KeyVal <<= 1;
    PORTC &= ~(1 << KeyCLK);
    _NOP();//??
    if (!(PINC & (1 << KeyData))) KeyVal ++;
    PORTC |=  (1 << KeyCLK);
  }
  return KeyVal;//?????0
}

inline void KeyObj::Exec(void)
{
static const unsigned char KeyTab[8] PROGMEM = {
  0b10000000,
  0b01000000,
  0b00100000,
  0b00010000,
  0b00001000,
  0b00000100,
  0b00000010,
  0b00000001
};
  KeyCount &= 0x07;
  KeyVal = GetKeyVal();
  if (KeyVal & pgm_read_byte(&KeyTab[KeyCount])) {
    if (KeyTest[KeyCount] < 1200) {
      KeyTest[KeyCount] ++;
      if (KeyTest[KeyCount] == 2) {
        KeyCommandExec(1);
      }
    }
    else {
      KeyTest[KeyCount] = 2;
      KeyCommandExec(2);
    }
  }
  else {
    if (KeyTest[KeyCount] >= 2) {
      KeyCommandExec(0);
    }
    else {
    }
    KeyTest[KeyCount] = 0;
  }
  KeyCount ++;
  KeyCount &= 0x07;
}

void KeyObj::SetKeyVal(unsigned char keyval)
{
    KeyVal = keyval;
}

void KeyObj::SetKeyCount(unsigned char count)
{
    KeyCount = count;
}

const PFV KeyObj::KeyCommandTab[3][5] PROGMEM=
{
      {&KeyObj::Key00, &KeyObj::Key01, &KeyObj::Key02, &KeyObj::Key03, &KeyObj::Key04}, //???????
      {&KeyObj::Key10, &KeyObj::Key11, &KeyObj::Key12, &KeyObj::Key13, &KeyObj::Key14}, //??????
      {&KeyObj::Key20, &KeyObj::Key21, &KeyObj::Key22, &KeyObj::Key23, &KeyObj::Key24}  //???????
};
    
void KeyObj::KeyCommandExec(unsigned char mode)
{
    
  KeyCount &= 0x07;
  if (KeyCount <= 4)
  {
    PFV f;
    memcpy_P(&f,  &(KeyObj::KeyCommandTab[(int)mode][(int)KeyCount]), sizeof(PFV));
    (this->*f)();
  }
}

void KeyObj::Key00(void)
{
}
...

void KeyObj::Key24(void)
{
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
5
关闭 站长推荐上一条 /3 下一条