0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

arduinoでusb host その4

Posted at

概要

arduino unoにusb hostつないで、usbバーコードリーダーつないでみた。
リーダーは、NEC製PK-UP004Eです。

写真

MVC-007S.JPG

ログ

bar.JPG

サンプルコード

# include <hidboot.h>
# include <usbhub.h>
# ifdef dobogusinclude
# include <spi4teensy3.h>
# include <SPI.h>
# endif

class KbdRptParser : public KeyboardReportParser
{
	void PrintKey(uint8_t mod, uint8_t key);
	protected:
	void OnControlKeysChanged(uint8_t before, uint8_t after);
	void OnKeyDown(uint8_t mod, uint8_t key);
	void OnKeyUp(uint8_t mod, uint8_t key);
	void OnKeyPressed(uint8_t key);
};
void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
{
	MODIFIERKEYS mod;
	* ((uint8_t *) &mod) = m;
	Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
	Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
	Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
	Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
	Serial.print(" >");
	PrintHex<uint8_t>(key, 0x80);
	Serial.print("< ");
	Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
	Serial.print((mod.bmRightShift == 1) ? "S" : " ");
	Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
	Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
};
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
	Serial.print("DN ");
	PrintKey(mod, key);
	uint8_t c = OemToAscii(mod, key);
	if (c) OnKeyPressed(c);
}
void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after)
{
	MODIFIERKEYS beforeMod;
	* ((uint8_t *) &beforeMod) = before;
	MODIFIERKEYS afterMod;
	* ((uint8_t *) &afterMod) = after;
	if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl)
	{
		Serial.println("LeftCtrl changed");
	}
	if (beforeMod.bmLeftShift != afterMod.bmLeftShift)
	{
		Serial.println("LeftShift changed");
	}
	if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt)
	{
		Serial.println("LeftAlt changed");
	}
	if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI)
	{
		Serial.println("LeftGUI changed");
	}
	if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl)
	{
		Serial.println("RightCtrl changed");
	}
	if (beforeMod.bmRightShift != afterMod.bmRightShift)
	{
		Serial.println("RightShift changed");
	}
	if (beforeMod.bmRightAlt != afterMod.bmRightAlt)
	{
		Serial.println("RightAlt changed");
	}
	if (beforeMod.bmRightGUI != afterMod.bmRightGUI)
	{
		Serial.println("RightGUI changed");
	}
}
void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
{
	Serial.print("UP ");
	PrintKey(mod, key);
}
void KbdRptParser::OnKeyPressed(uint8_t key)
{
	Serial.print("ASCII: ");
	Serial.println((char) key);
	if (key == 49) digitalWrite(2, HIGH);
	if (key == 48) digitalWrite(2, LOW);
};
USB Usb;
HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
KbdRptParser Prs;
void setup()
{
	pinMode(2, OUTPUT);
	Serial.begin(115200);
	Serial.println("Start");
	if (Usb.Init() == -1) Serial.println("OSC did not start.");
	HidKeyboard.SetReportParser(0, &Prs);
}
void loop()
{
	Usb.Task();
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?