Go Down

Topic: Libreria PS2Keyboard - lettere accentate (Read 3561 times) previous topic - next topic

lesto

scusa ho sbagliato, corretto è

Code: [Select]
if (i >= BUFFER_SIZE) i = 0;
c = buffer[i];
Serial.println(c, HEX);
tail = i;
return c;


cmq leo ha visto giusto, bisogna solo capire cos'è che fa dividere in 2 quel valore
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

Francesco_MIRM

Mettendo il println nella get_scan_code e nel ps2interrupt nel terminale ricevo:
Code: [Select]

1C
1C                //   a
a
F0
1C
F0
1C
---------------------
32
32               //   b
b
F0
32
F0
32
-------------------
52
52               //   à

=
`
F0
52
F0
52
------------------
54
54              //   è
=
X
F0
54
F0
54
-----------------

Secondo me i codici hex vengono letti bene e anche l'array è fatto giusto, facendo vari calcoli con le posizioni mi trovoo le lettere nel posto esatto.

Francesco_MIRM

#32
Dec 17, 2011, 04:20 am Last Edit: Dec 17, 2011, 04:23 am by Francesco_MIRM Reason: 1
Allora alla fine ho risolto così, ho assegnato ai "caratteri speciali" dei numeri < 127 , ho cercato di usare numeri non usati da caratteri stampabili o tasti funzione:
Code: [Select]

#define PS2_POUND_SIGN         28  //163 // £
#define PS2_a_GRAVE         1   //224 // à
#define PS2_e_GRAVE         2   //232 // è
#define PS2_i_GRAVE         3   //236 // ì
#define PS2_o_GRAVE         4   //242 // ò
#define PS2_u_GRAVE         5   //249 // ù
#define PS2_DEGREE_SIGN         6   //176 // °
#define PS2_e_ACUTE         7   //233 // é
#define PS2_c_CEDILLA         22  //231 // ç
#define PS2_SECTION_SIGN         16  //167 // §

Questo le sketch:
Code: [Select]

#include <PS2Keyboard.h>

const int DataPin = 4;
const int IRQpin =  3;

PS2Keyboard keyboard;

void setup() {
 keyboard.begin(DataPin, IRQpin);
 Serial.begin(9600);
 Serial.println("Test:");
}

void loop() {
 if (keyboard.available()) {
   char c = keyboard.read();
   // Controllo tasti speciali
   if (c == PS2_ENTER) {
     Serial.println("\n");  
   } else if (c == PS2_TAB) {
     Serial.print("\t");  
   } else if (c == PS2_ESC) {
     Serial.print("[ESC]");
   } else if (c == PS2_INSERT) {
     Serial.print("[Ins]");
   } else if (c == PS2_DELETE) {
     Serial.print("[Del]");
   } else if (c == PS2_HOME) {
     Serial.print("[Home]");  
   } else if (c == PS2_END) {
     Serial.print("[End]");    
   } else if (c == PS2_PAGEDOWN) {
     Serial.print("[PagGiu]");
   } else if (c == PS2_PAGEUP) {
     Serial.print("[PagSu]");
   //Frecce direzionali.
   } else if (c == PS2_UPARROW) {
     Serial.print("[Su]");
   } else if (c == PS2_DOWNARROW) {
     Serial.print("[Giu]");
   } else if (c == PS2_LEFTARROW) {
     Serial.print("[Sinistra]");
   } else if (c == PS2_RIGHTARROW) {
     Serial.print("[Destra]");
   // Lettere accentate.
   } else if (c == PS2_a_GRAVE) {
     Serial.print(char(224));// à
   } else if (c == PS2_e_GRAVE) {
     Serial.print(char(232));// è
   } else if (c == PS2_i_GRAVE) {
     Serial.print(char(236));// ì
   } else if (c == PS2_o_GRAVE) {
     Serial.print(char(242));// ò
   } else if (c == PS2_u_GRAVE) {
     Serial.print(char(249));// u
   // Con Shift.
   } else if (c == PS2_DEGREE_SIGN) {
     Serial.print(char(176));// °
   } else if (c == PS2_e_ACUTE) {
     Serial.print(char(233));// é
   } else if (c == PS2_c_CEDILLA) {
     Serial.print(char(231));// ç
   } else if (c == PS2_SECTION_SIGN) {
     Serial.print(char(167));// §
   } else if (c == PS2_POUND_SIGN) {
     Serial.print(char(163));// £
   }    
   else {    
     Serial.print(c); // Tutti gli altri caratteri.
   }
 }
}

Il risultato:
Code: [Select]

Test:

SENZA SHIFT
\1234567890'ìqwertyuiopè+asdfghjklòàù<zxcvbnm,.-

CON SHIFT
|!"£$%&/()=?^QWERTYUIOPé*ASDFGHJKLç°§>ZXCVBNM;:_

CON ALTGR
[]@#

TASTI FUNZIONE
[ESC][Ins][Del][End][Home][PagSu][PagGiu][Enter][TAB]

TASTIERINO NUMERICO
123456789/*-+.0

Possono essere aggiunti anche i tasti [Fn].

Questo è quello che sono riuscito a fare, lo sketch compilato sono 4318 bytes, dalla libreria classica:
http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html
ho tolto una parte sul controllo della board e sull'inizializzazione dei pin, (adesso è compatibile con Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc..).
Tolti i #define dei caratteri speciali non usati, rimosso il layout tedesco e modificato il nome di quello americano PS2Keymap_US in PS2Keymap_Italiano e reso il layout predefinito.
Tolti alcuni commenti nei file .h e .cpp.
Se qualcuno vuole proporre qualche soluzione migliore mi fa piacere , ciao!

Nel prossimo post riporto i file .h e .cpp da me modificati.

Francesco_MIRM

file PS2Keyboard.h

Code: [Select]

//PS2Keyboard.h  * Author:  Christian Weichel

#ifndef PS2Keyboard_h
#define PS2Keyboard_h
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "WProgram.h"
// interrupt pins per Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc.
#define CORE_INT0_PIN  2
#define CORE_INT1_PIN  3

#define PS2_TAB 9
#define PS2_ENTER 13
#define PS2_BACKSPACE 127
#define PS2_ESC 27
#define PS2_INSERT 11  //82
#define PS2_DELETE 127
#define PS2_HOME 12  //71
#define PS2_END 23  //79
#define PS2_PAGEUP 25
#define PS2_PAGEDOWN 26
#define PS2_UPARROW 11
#define PS2_LEFTARROW 8
#define PS2_DOWNARROW 10
#define PS2_RIGHTARROW 21
#define PS2_F1 0   //59
#define PS2_F2 0   //60
#define PS2_F3 0   //61
#define PS2_F4 0   //62
#define PS2_F5 0   //63
#define PS2_F6 0   //64
#define PS2_F7 0   //65
#define PS2_F8 0   //66
#define PS2_F9 0   //67
#define PS2_F10 0   //68
#define PS2_F11 0   //133
#define PS2_F12 0   //134
#define PS2_SCROLL 0
#define PS2_POUND_SIGN      28  //163 // £
#define PS2_a_GRAVE          1   //224 // à
#define PS2_e_GRAVE          2   //232 // è
#define PS2_i_GRAVE          3   //236 // ì
#define PS2_o_GRAVE          4   //242 // ò
#define PS2_u_GRAVE          5   //249 // ù
#define PS2_DEGREE_SIGN      6   //176 // °
#define PS2_e_ACUTE          7   //233 // é
#define PS2_c_CEDILLA      22  //231 // ç
#define PS2_SECTION_SIGN      16  //167 // §


#define PS2_KEYMAP_SIZE 136

typedef struct {
uint8_t noshift[PS2_KEYMAP_SIZE];
uint8_t shift[PS2_KEYMAP_SIZE];
uint8_t uses_altgr;
uint8_t altgr[PS2_KEYMAP_SIZE];
} PS2Keymap_t;

extern const PROGMEM PS2Keymap_t PS2Keymap_Italiano;


class PS2Keyboard {
  public:

    PS2Keyboard();
   
    static void begin(uint8_t dataPin, uint8_t irq_pin, const PS2Keymap_t &map = PS2Keymap_Italiano);
   
    static bool available();
   
    static int read();
};

#endif


file PS2Keyboard.cpp

Code: [Select]

// PS2Keyboard.cpp   * Author:  Christian Weichel

#include "PS2Keyboard.h"
#define BUFFER_SIZE 45
static volatile uint8_t buffer[BUFFER_SIZE];
static volatile uint8_t head, tail;
static uint8_t DataPin;
static uint8_t CharBuffer=0;
static uint8_t UTF8next=0;
static const PS2Keymap_t *keymap=NULL;

// The ISR for the external interrupt
void ps2interrupt(void)
{
static uint8_t bitcount=0;
static uint8_t incoming=0;
static uint32_t prev_ms=0;
uint32_t now_ms;
uint8_t n, val;

val = digitalRead(DataPin);
now_ms = millis();
if (now_ms - prev_ms > 250) {
bitcount = 0;
incoming = 0;
}
prev_ms = now_ms;
n = bitcount - 1;
if (n <= 7) {
incoming |= (val << n);
}
bitcount++;
if (bitcount == 11) {
uint8_t i = head + 1;
if (i >= BUFFER_SIZE) i = 0;
if (i != tail) {
buffer[i] = incoming;
//Serial.println(incoming, HEX);//funzionante
head = i;
}
bitcount = 0;
incoming = 0;
}
}


static inline uint8_t get_scan_code(void)   
{
uint8_t c, i;                         

i = tail;
if (i == head) return 0;
i++;
if (i >= BUFFER_SIZE) i = 0;
c = buffer[i];
//Serial.println(c, HEX);//funzionante
tail = i;
return c;
}


const PROGMEM PS2Keymap_t PS2Keymap_Italiano = {
  // senza shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '\\', 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '1', 0,
0, 0, 'z', 's', 'a', 'w', '2', 0,
0, 'c', 'x', 'd', 'e', '4', '3', 0,
0, ' ', 'v', 'f', 't', 'r', '5', 0,
0, 'n', 'b', 'h', 'g', 'y', '6', 0,
0, 0, 'm', 'j', 'u', '7', '8', 0,
0, ',', 'k', 'i', 'o', '0', '9', 0,
0, '.', '-', 'l', PS2_o_GRAVE, 'p', '\'', 0,
0, 0, PS2_a_GRAVE, 0, PS2_e_GRAVE, PS2_i_GRAVE, 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '+', 0, PS2_u_GRAVE, 0, 0,
0, '<', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
  // con shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '|', 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'Q', '!', 0,
0, 0, 'Z', 'S', 'A', 'W', '"', 0,
0, 'C', 'X', 'D', 'E', '$', PS2_POUND_SIGN, 0,
0, ' ', 'V', 'F', 'T', 'R', '%', 0,
0, 'N', 'B', 'H', 'G', 'Y', '&', 0,
0, 0, 'M', 'J', 'U', '/', '(', 0,
0, ';', 'K', 'I', 'O', '=', ')', 0,
0, ':', '_', 'L', PS2_c_CEDILLA, 'P', '?', 0,
0, 0, PS2_DEGREE_SIGN, 0, PS2_e_ACUTE, '^', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '*', 0, PS2_SECTION_SIGN, 0, 0,
0, '>', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
0,
  // con altgr
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, 0, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0 /*ALTGR+E*/, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, '@', 0, 0, 0,
0, 0, '#', 0, '[', 0, 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, ']', 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 }
};


#define BREAK     0x01
#define MODIFIER  0x02
#define SHIFT_L   0x04
#define SHIFT_R   0x08
#define ALTGR     0x10

static char get_iso8859_code(void)
{
static uint8_t state=0;
uint8_t s;                       
int8_t c;                         

while (1) {
s = get_scan_code();
if (!s) return 0;
if (s == 0xF0) {
state |= BREAK;
} else if (s == 0xE0) {
state |= MODIFIER;
} else {
if (state & BREAK) {
if (s == 0x12) {
state &= ~SHIFT_L;
} else if (s == 0x59) {
state &= ~SHIFT_R;
} else if (s == 0x11 && (state & MODIFIER)) {
state &= ~ALTGR;
}
// CTRL, ALT & WIN keys could be added
state &= ~(BREAK | MODIFIER);
continue;
}
if (s == 0x12) {
state |= SHIFT_L;
continue;
} else if (s == 0x59) {
state |= SHIFT_R;
continue;
} else if (s == 0x11 && (state & MODIFIER)) {
state |= ALTGR;
}
c = 0;
if (state & MODIFIER) {
switch (s) {
  case 0x70: c = PS2_INSERT;      break;
  case 0x6C: c = PS2_HOME;        break;
  case 0x7D: c = PS2_PAGEUP;      break;
  case 0x71: c = PS2_DELETE;      break;
  case 0x69: c = PS2_END;         break;
  case 0x7A: c = PS2_PAGEDOWN;    break;
  case 0x75: c = PS2_UPARROW;     break;
  case 0x6B: c = PS2_LEFTARROW;   break;
  case 0x72: c = PS2_DOWNARROW;   break;
  case 0x74: c = PS2_RIGHTARROW;  break;
  case 0x4A: c = '/';             break;
  case 0x5A: c = PS2_ENTER;       break;
  default: break;
}
} else if ((state & ALTGR) && keymap->uses_altgr) {
if (s < PS2_KEYMAP_SIZE)
c = pgm_read_byte(keymap->altgr + s);
} else if (state & (SHIFT_L | SHIFT_R)) {
if (s < PS2_KEYMAP_SIZE)
c = pgm_read_byte(keymap->shift + s);
} else {
if (s < PS2_KEYMAP_SIZE)
c = pgm_read_byte(keymap->noshift + s);
}
state &= ~(BREAK | MODIFIER);
if (c) return c;
}
}
}


bool PS2Keyboard::available() {
if (CharBuffer || UTF8next) return true;
CharBuffer = get_iso8859_code();
if (CharBuffer) return true;
return false;
}


int PS2Keyboard::read() {
uint8_t result;

result = UTF8next;
if (result) {
UTF8next = 0;
} else {
result = CharBuffer;
if (result) {
CharBuffer = 0;
} else {
result = get_iso8859_code();
}
if (result >= 128) {
UTF8next = (result & 0x3F) | 0x80;
result = ((result >> 6) & 0x1F) | 0xC0;
}
}
if (!result) return -1;
return result;
}


PS2Keyboard::PS2Keyboard() {}


void PS2Keyboard::begin(uint8_t data_pin, uint8_t irq_pin, const PS2Keymap_t &map) {
  uint8_t irq_num=0;

  DataPin = data_pin;
  keymap = &map;

  // initialize the pins
#ifdef INPUT_PULLUP
  pinMode(irq_pin, INPUT_PULLUP);
  pinMode(data_pin, INPUT_PULLUP);
#else
  pinMode(irq_pin, INPUT);
  digitalWrite(irq_pin, HIGH);
  pinMode(data_pin, INPUT);
  digitalWrite(data_pin, HIGH);
#endif
 
  switch(irq_pin) {
    #ifdef CORE_INT0_PIN
    case CORE_INT0_PIN:
      irq_num = 0;
      break;
    #endif
    #ifdef CORE_INT1_PIN
    case CORE_INT1_PIN:
      irq_num = 1;
      break;
    #endif
    default:
      irq_num = 0;
      break;
  }
  head = 0;
  tail = 0;
  attachInterrupt(irq_num, ps2interrupt, FALLING);
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy