1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Updated USBHost HID examples

This commit is contained in:
Cristian Maglie 2012-06-30 02:04:52 +02:00
parent 7ae42f7e3a
commit 00f8cdb931
2 changed files with 72 additions and 60 deletions

View File

@ -1,55 +1,57 @@
#include "variant.h"
#include <stdio.h>
#include <hidboot.h>
class KbdRptParser : public KeyboardReportParser
class KbdRptParser :
public KeyboardReportParser
{
void PrintKey(uint8_t mod, uint8_t key);
void PrintKey(uint8_t mod, uint8_t key);
protected:
virtual void OnKeyDown (uint8_t mod, uint8_t key);
virtual void OnKeyUp (uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
virtual void OnKeyDown(uint8_t mod, uint8_t key);
virtual void OnKeyUp(uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
};
void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
{
MODIFIERKEYS mod;
MODIFIERKEYS mod;
*((uint8_t*)&mod) = m;
printf((mod.bmLeftCtrl == 1) ? "C" : " ");
printf((mod.bmLeftShift == 1) ? "S" : " ");
printf((mod.bmLeftAlt == 1) ? "A" : " ");
printf((mod.bmLeftGUI == 1) ? "G" : " ");
*((uint8_t*)&mod) = m;
Serial1.print((mod.bmLeftCtrl == 1) ? "C" : " ");
Serial1.print((mod.bmLeftShift == 1) ? "S" : " ");
Serial1.print((mod.bmLeftAlt == 1) ? "A" : " ");
Serial1.print((mod.bmLeftGUI == 1) ? "G" : " ");
printf("<%c>", key);
Serial1.print("<");
Serial1.print(key);
Serial1.print(">");
printf((mod.bmRightCtrl == 1) ? "C" : " ");
printf((mod.bmRightShift == 1) ? "S" : " ");
printf((mod.bmRightAlt == 1) ? "A" : " ");
printf((mod.bmRightGUI == 1) ? "G" : " ");
};
Serial1.print((mod.bmRightCtrl == 1) ? "C" : " ");
Serial1.print((mod.bmRightShift == 1) ? "S" : " ");
Serial1.print((mod.bmRightAlt == 1) ? "A" : " ");
Serial1.print((mod.bmRightGUI == 1) ? "G" : " ");
}
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
printf("DN ");
PrintKey(mod, key);
uint8_t c = OemToAscii(mod, key);
Serial1.print("DOWN ");
PrintKey(mod, key);
uint8_t c = OemToAscii(mod, key);
if (c)
OnKeyPressed(c);
if (c)
OnKeyPressed(c);
}
void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
{
printf("UP ");
PrintKey(mod, key);
Serial1.print("UP ");
PrintKey(mod, key);
}
void KbdRptParser::OnKeyPressed(uint8_t key)
{
printf("ASCII: %c", key);
};
Serial1.print("ASCII: ");
Serial1.println(key);
}
USBHost Usb;
HIDBoot<HID_PROTOCOL_KEYBOARD> Kbd(&Usb);
@ -57,14 +59,15 @@ KbdRptParser Prs;
void setup()
{
cpu_irq_enable();
printf("\r\nProgram started:\r\n");
delay(200);
Serial1.begin(115200);
Serial1.println("Program started!");
delay(200);
Kbd.SetReportParser(0, (HIDReportParser*)&Prs);
Kbd.SetReportParser(0, &Prs);
}
void loop()
{
Usb.Task();
Usb.Task();
}

View File

@ -1,45 +1,54 @@
#include "variant.h"
#include <stdio.h>
#include <hidboot.h>
class MouseRptParser : public MouseReportParser
{
protected:
virtual void OnMouseMove (MOUSEINFO *mi);
virtual void OnLeftButtonUp (MOUSEINFO *mi);
virtual void OnLeftButtonDown (MOUSEINFO *mi);
virtual void OnRightButtonUp (MOUSEINFO *mi);
virtual void OnRightButtonDown (MOUSEINFO *mi);
virtual void OnMiddleButtonUp (MOUSEINFO *mi);
virtual void OnMiddleButtonDown (MOUSEINFO *mi);
virtual void OnMouseMove(MOUSEINFO *mi);
virtual void OnLeftButtonUp(MOUSEINFO *mi);
virtual void OnLeftButtonDown(MOUSEINFO *mi);
virtual void OnRightButtonUp(MOUSEINFO *mi);
virtual void OnRightButtonDown(MOUSEINFO *mi);
virtual void OnMiddleButtonUp(MOUSEINFO *mi);
virtual void OnMiddleButtonDown(MOUSEINFO *mi);
};
void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
{
printf("Pos={%d,%d}\r\n", mi->dX, mi->dY);
Serial1.print("Moving { ");
Serial1.print(mi->dX);
Serial1.print(", ");
Serial1.print(mi->dY);
Serial1.println(" }");
};
void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
void MouseRptParser::OnLeftButtonUp(MOUSEINFO *mi)
{
printf("L Butt Up\r\n");
Serial1.println("Left Button UP");
};
void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
void MouseRptParser::OnLeftButtonDown(MOUSEINFO *mi)
{
printf("L Butt Dn\r\n");
Serial1.println("Left Button DOWN");
};
void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)
void MouseRptParser::OnRightButtonUp(MOUSEINFO *mi)
{
printf("R Butt Up\r\n");
Serial1.println("Right Button UP");
};
void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)
void MouseRptParser::OnRightButtonDown(MOUSEINFO *mi)
{
printf("R Butt Dn\r\n");
Serial1.println("Right Button DOWN");
};
void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi)
void MouseRptParser::OnMiddleButtonUp(MOUSEINFO *mi)
{
printf("M Butt Up\r\n");
Serial1.println("Middle Button UP");
};
void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
void MouseRptParser::OnMiddleButtonDown(MOUSEINFO *mi)
{
printf("M Butt Dn\r\n");
Serial1.println("Middle Button DOWN");
};
USBHost Usb;
@ -48,14 +57,14 @@ MouseRptParser Prs;
void setup()
{
cpu_irq_enable();
printf("\r\nProgram started:\r\n");
delay(200);
Serial1.begin(115200);
Serial1.println("Program started:");
delay(200);
HostMouse.SetReportParser(0,(HIDReportParser*)&Prs);
HostMouse.SetReportParser(0, &Prs);
}
void loop()
{
Usb.Task();
Usb.Task();
}