Esempio n. 1
0
static uint8_t m2_get_rot_enc_get_key(void)
{
  uint8_t new_m2_rot_enc_state;
  
  /* calculate the next state */
  new_m2_rot_enc_state = m2_rot_enc_next_state(m2_rot_enc_state, m2_rot_enc_get_input());

  if ( new_m2_rot_enc_state != m2_rot_enc_state )
  {
    /* if state has changed, then reset the debounce counter */
    m2_rot_enc_debounce_cnt = 0;
    /* ... and store new state */
    m2_rot_enc_state = new_m2_rot_enc_state;
    
    delay(M2_ROTARY_ENCODER_STATE_CHANGE_DELAY);
  }
  else
  {
    /* only if the direction is known */
    if ( new_m2_rot_enc_state < 8 ) 
    {
      /* increment the debounce counter */
      if ( m2_rot_enc_debounce_cnt < ROT_ENC_DEBOUNCE_VAL )
      { 
        m2_rot_enc_debounce_cnt++;
        if ( m2_rot_enc_debounce_cnt >= ROT_ENC_DEBOUNCE_VAL )
        {
	  if ( new_m2_rot_enc_state & 4 )
	  {
	    if ( m2_rot_enc_div_cnt > ROT_ENC_EVENT_DIVISION )
	    {
	      m2_rot_enc_div_cnt = 0;
	      return M2_KEY_EVENT(M2_KEY_NEXT);		/* disable debounce by M2tk algoritm */	      
	    }
	    else
	    {
	      m2_rot_enc_div_cnt++;
	    }
	  }
	  else
	  {
	    if ( m2_rot_enc_div_cnt < -ROT_ENC_EVENT_DIVISION )
	    {
	      m2_rot_enc_div_cnt = 0;
	      return M2_KEY_EVENT(M2_KEY_PREV);		/* disable debounce by M2tk algoritm */
	    }
	    else
	    {
	      m2_rot_enc_div_cnt--;
	    }
	  }
        }
      }
    }
  }
  
  return M2_KEY_NONE;
}
Esempio n. 2
0
uint8_t m2_es_arduino_ir(m2_p ep, uint8_t msg) 
{
  switch(msg)
  {
    case M2_ES_MSG_GET_KEY:
      if (irrecv.decode(&results)) 
      {
        irrecv.resume(); // Receive the next value
        switch(results.value) 
        {
          case 0xFFC23D:
            return M2_KEY_EVENT(M2_KEY_SELECT);         /* the M2_KEY_EVENT() macro will  */
          case 0xFFB04F:                                                /* disable the debounce algorithm */
            return M2_KEY_EVENT(M2_KEY_EXIT);
          case 0xFF02FD:
            return M2_KEY_EVENT(M2_KEY_NEXT);
          case 0xFF22DD:
            return M2_KEY_EVENT(M2_KEY_PREV);
          case 0xFF0000:
            return M2_KEY_EVENT(M2_KEY_DATA_UP);
          case 0xFF0001:
            return M2_KEY_EVENT(M2_KEY_DATA_DOWN);
          default:
            return M2_KEY_NONE;
         }
      }
    default:
      return 0;
  }
} 
Esempio n. 3
0
File: main.c Progetto: likon/m2tklib
/* 
  event source for SDL
  this has been copied from m2ghsdl.c
  u8glib only replaces the graphics part, but not the event handling for sdl
  it is assumed, that SDL has been setup
*/
uint8_t m2_es_sdl(m2_p ep, uint8_t msg)
{
  switch(msg)
  {
    case M2_ES_MSG_GET_KEY:
    {
    	SDL_Event event;
	    /* http://www.libsdl.org/cgi/docwiki.cgi/SDL_PollEvent */
	    if ( SDL_PollEvent(&event) != 0 )
	    {
	      switch (event.type) 
	      {
	        case SDL_QUIT:
		        exit(0);
		        break;
	        case SDL_KEYDOWN:
		        switch( event.key.keysym.sym )
		        {
		          case SDLK_s:
		            return M2_KEY_EVENT(M2_KEY_SELECT);
		          case SDLK_x:
		            puts("SDLK_x");
		            return M2_KEY_EVENT(M2_KEY_EXIT);
		          case SDLK_n:
		            puts("SDLK_n");
		            return M2_KEY_EVENT(M2_KEY_NEXT);
		          case SDLK_p:
		            puts("SDLK_p");
		            return M2_KEY_EVENT(M2_KEY_PREV);
		          case SDLK_u:
		            puts("SDLK_u");
		            return M2_KEY_EVENT(M2_KEY_DATA_UP);
		          case SDLK_d:
		            puts("SDLK_d");
		            return M2_KEY_EVENT(M2_KEY_DATA_DOWN);
		          case SDLK_o:                  // screenshot
		            puts("SDLK_o (screenshOt)");
                            screenshot();
                            //screenshot100();
                            break;
		          case SDLK_q:
		            exit(0);
		            break;
		          default:
		            break;
		        }
		        break;
	        }
      	}
      }
      return M2_KEY_NONE;
    case M2_ES_MSG_INIT:
      break;
  }
  return 0;
}
Esempio n. 4
0
extern "C" uint8_t m2_es_arduino_serial(m2_p ep, uint8_t msg)
{
  uint8_t c, r;
  
  switch(msg)
  {
    case M2_ES_MSG_GET_KEY:
      if ( m2_es_arduino_serial_is_init == 0 )
      {
	Serial.begin(9600);        // init serial
	Serial.println("m2tklib serial (press 'h' for help)");
	Serial.println("");
	m2_es_arduino_serial_is_init = 1;
      }
      r = M2_KEY_NONE;
      if ( Serial.available() )
      {
	c = Serial.read();
	if ( c == 'h' )
	{
	  m2_serial_show_help();
	}
	else
	{
	  r = m2_serial_char_to_key(c);
	  if ( r != M2_KEY_NONE )
	  {
	    Serial.println((char)c);
	    r = M2_KEY_EVENT(r);
	  }
	}
      }
      return r;
    case M2_ES_MSG_INIT:
      return 0;
  }
  return 0;
}
Esempio n. 5
0
void m2_CheckKeyM2(m2_p m2)
{
  uint8_t key;
  /* step 1: get raw key */
  
  /* check if a key should be forced */
  /* obsolete
  key = m2->forced_key;
  if ( key != M2_KEY_NONE )
  {
    m2->forced_key = M2_KEY_NONE;
    m2_PutKeyIntoQueue(m2, key);
  }
  else
  */
  {
    /* request key information from the event source */
    if ( m2->es != NULL )
    {
      key = m2->es(m2, M2_ES_MSG_GET_KEY);
      /*
      if ( key == 0 )
	printf(".");
      else
	printf("key: %d   is_last_key_touch_screen_press:%d\n", key, m2->is_last_key_touch_screen_press);
      */
      
      if ( key == M2_KEY_EVENT(M2_KEY_TOUCH_PRESS) )
      {
	/* event without debounce */
	m2->is_last_key_touch_screen_press = 1;
      }
      /* the case where key == M2_KEY_TOUCH_PRESS is handled inside the deboucne algorithm */      
      if ( m2->is_last_key_touch_screen_press != 0 && key == M2_KEY_NONE )
      {
	m2->is_last_key_touch_screen_press = 0;
	key = M2_KEY_EVENT(M2_KEY_TOUCH_RELEASE);
      }
      
#ifdef OBSOLTE
      
      /* this code automatically generates the M2_KEY_TOUCH_RELEASE message once */
      if ( key == M2_KEY_TOUCH_PRESS || key == M2_KEY_EVENT(M2_KEY_TOUCH_PRESS) )
      {
	m2->is_last_key_touch_screen_press = 1;
	//puts("key == M2_KEY_TOUCH_PRESS");
      }
      else if ( key == M2_KEY_TOUCH_RELEASE || key == M2_KEY_EVENT(M2_KEY_TOUCH_RELEASE) )
      {
	//puts("key == M2_KEY_TOUCH_RELEASE || key == M2_KEY_EVENT(M2_KEY_TOUCH_RELEASE)");
	m2->is_last_key_touch_screen_press = 0;
	key = M2_KEY_EVENT(M2_KEY_TOUCH_RELEASE);
      }
      else if ( m2->is_last_key_touch_screen_press != 0 && key == M2_KEY_NONE )
      {
	//puts("m2->is_last_key_touch_screen_press != 0 && key == M2_KEY_NONE");
	m2->is_last_key_touch_screen_press = 0;
	key = M2_KEY_EVENT(M2_KEY_TOUCH_RELEASE);
      }
#endif
	
      /* store the key in the queue */
      m2_SetDetectedKey(m2, key, m2->arg1, m2->arg2);
    }
    else
      key = M2_KEY_NONE;
  }
}