Re-bonjour. Après de nombreuses recherches, nous avons réussi à trouver un programme qui fonctionne normalement avec notre douchette. Ce programme provient de ce post et nous lui avons ajouté une fonction compare afin de comparer le code lu avec ceux dans notre tableau en mémoire.
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#include <SPI.h>
#include <hiduniversal.h>
#include <stdio.h>
#include <string.h>
#define MAXQRCODE 50 // NEEDS TO BE LONG ENOUGH TO FIT YOUR LONGEST BARCODE SCAN
class KbdRptParser : public KeyboardReportParser
{
public:
KbdRptParser();
void resetString();
bool newCar;
bool newString;
uint8_t currentCursor;
char QRCodeBuffer[MAXQRCODE + 1];
protected:
virtual void OnKeyDown (uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
};
KbdRptParser::KbdRptParser(): KeyboardReportParser()
{
resetString();
}
void KbdRptParser::resetString()
{
newCar = false;
newString = false;
currentCursor = 0;
QRCodeBuffer[0] = '\0';
}
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
uint8_t c = OemToAscii(mod, key);
if (c) OnKeyPressed(c);
}
/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)
{
QRCodeBuffer[currentCursor++] = key;
QRCodeBuffer[currentCursor] = '\0';
if (currentCursor >= MAXQRCODE) currentCursor = MAXQRCODE - 1; // not enough space, next character will overwrite last one
newCar = true;
newString = true;
}
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);
KbdRptParser Prs;
const unsigned long timeOutDuration = 200ul;
boolean premiereFois = false;
void setup()
{
Serial.begin(115200);
Serial.println("Start");
if (Usb.Init() == -1) {
Serial.println("OSC did not start. Stop here.");
while (1); // No need to continue.
}
Hid.SetReportParser(0, (HIDReportParser*)&Prs);
}
void loop()
{
static uint32_t lastKeyTime = 0; //watchdog
static bool timedOut = false;
Usb.Task(); // this gives a chance to read from the barcode scanner
if (Prs.newCar) { // we just got a new char; you'll see the string building up
Serial.print("["); Serial.print(Prs.QRCodeBuffer); Serial.println("]");
Prs.newCar = false; // new char has been handled
lastKeyTime = millis(); // memorize the last char entry time
}
// here we check if there has been no activity from the barcode reader for a while
if (Prs.newString && (millis() - lastKeyTime >= timeOutDuration)) { //timeout, we just finsihed entering a new string
Serial.print("Final string = ["); Serial.print(Prs.QRCodeBuffer); Serial.println("]");
compare();
Prs.resetString(); // tell the Prs instance that the new string has been handled.
}
}
void compare()
{
const char * Str1 = "01";
//if (!strcmp(Prs.QRCodeBuffer, Str1)) {
//if (!strcmp(Prs.QRCodeBuffer, "01") ==1) {
if (Prs.QRCodeBuffer == "01"){
Serial.print("code bon");
}
else {
Serial.print("mauvais code");
}
}
Mais nous avons 2 petits problèmes:
-Lorsque l'on scan un code avec des lettres, le programme nous ajoute des "+" non voulu
Start
Start
[+]
[+C]
[+C+]
[+C+P]
[+C+P]
Final string = [+C+P]
mauvais code
-Lorsque l'on scan le code barre '01' qui correspond à notre code en mémoire ou tout autre code, la fonction compare renvoie toujours mauvais code et ce avec les 3 différentes façon de le faire (les 3 façon sont dans le void compare).
Nous aimerions donc savoir d'où viennent ces problèmes et comment les résoudre. Merci