Pages: 1 [2]   Go Down
Author Topic: USB Host shield accessing variables  (Read 2127 times)
0 Members and 1 Guest are viewing this topic.
ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mr.Paul,

Please see the codes below:

USBHIDjoystick code:

Code:
#include <avr/pgmspace.h>

#include <avrpins.h>
#include <max3421e.h>
#include <usbhost.h>
#include <usb_ch9.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <address.h>
#include <hid.h>
#include <hiduniversal.h>

#include "hidjoystickrptparser.h"

#include <printhex.h>
#include <message.h>
#include <hexdump.h>
#include <parsetools.h>

USB                                             Usb;
USBHub                                          Hub(&Usb);
HIDUniversal                                    Hid(&Usb);
JoystickEvents                                  JoyEvents;
JoystickReportParser                            Joy(&JoyEvents);

void setup()
{
  Serial.begin( 9600 );
  Serial.println("Start");

  if (Usb.Init() == -1)
      Serial.println("OSC did not start.");
     
  delay( 200 );

  if (!Hid.SetReportParser(0, &Joy))
      ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1  );
}

void loop()
{
    Usb.Task();
}

The  hidjoystickrptparser.cpp file ,This is the file that is executed to show the values returned by the USB descriptors after successful connection of USB joystick is made with USB host shield and various buttons are pressed, ALL works fine but when i add the LiquidCrystal.h library in this file i get the following error:


hidjoystickrptparser.cpp:1:27: error: LiquidCrystal.h: No such file or directory

Code:
#include <LiquidCrystal.h>

#include "hidjoystickrptparser.h"

JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
joyEvents(evt),
oldHat(0xDE),
oldButtons(0)
{
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
oldPad[i] = 0xD;
}

void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
{
bool match = true;

// Checking if there are changes in report since the method was last called
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
if (buf[i] != oldPad[i])
{
match = false;
break;
}

// Calling Game Pad event handler
if (!match && joyEvents)
{
joyEvents->OnGamePadChanged((const GamePadEventData*)buf);

for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];
}

uint8_t hat = (buf[5] & 0xF);

// Calling Hat Switch event handler
if (hat != oldHat && joyEvents)
{
joyEvents->OnHatSwitch(hat);
oldHat = hat;
}

uint16_t buttons = (0x0000 | buf[6]);
buttons <<= 4;
buttons |= (buf[5] >> 4);
uint16_t changes = (buttons ^ oldButtons);

// Calling Button Event Handler for every button changed
if (changes)
{
for (uint8_t i=0; i<0x0C; i++)
{
uint16_t mask = (0x0001 << i);

if (((mask & changes) > 0) && joyEvents)
if ((buttons & mask) > 0)
joyEvents->OnButtonDn(i+1);
else
joyEvents->OnButtonUp(i+1);
}
oldButtons = buttons;
}
}

void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)
{
Serial.print("X: ");
PrintHex<uint8_t>(evt->X);
Serial.print("\tY: ");
PrintHex<uint8_t>(evt->Y);
Serial.print("\tZ: ");
PrintHex<uint8_t>(evt->Z1);
Serial.print("\tZ: ");
PrintHex<uint8_t>(evt->Z2);
Serial.print("\tRz: ");
PrintHex<uint8_t>(evt->Rz);
Serial.println("");
}

void JoystickEvents::OnHatSwitch(uint8_t hat)
{
Serial.print("Hat Switch: ");
PrintHex<uint8_t>(hat);
Serial.println("");
}

void JoystickEvents::OnButtonUp(uint8_t but_id)
{
Serial.print("Up: ");
Serial.println(but_id, DEC);
}

void JoystickEvents::OnButtonDn(uint8_t but_id)
{
Serial.print("Dn: ");
Serial.println(but_id, DEC);
}

no changes to the hidjoystickrptparser.h file below as no changes are needed:

Code:
#if !defined(__HIDJOYSTICKRPTPARSER_H__)
#define __HIDJOYSTICKRPTPARSER_H__

#include <inttypes.h>
#include <avr/pgmspace.h>
#include "avrpins.h"
#include "max3421e.h"
#include "usbhost.h"
#include "usb_ch9.h"
#include "Usb.h"

#if defined(ARDUINO) && ARDUINO >=100
#include "Arduino.h"
#else
#include <WProgram.h>
#endif

#include "printhex.h"
#include "hexdump.h"
#include "message.h"
#include "confdescparser.h"
#include "hid.h"

struct GamePadEventData
{
uint8_t X, Y, Z1, Z2, Rz;
};

class JoystickEvents
{
public:
virtual void OnGamePadChanged(const GamePadEventData *evt);
virtual void OnHatSwitch(uint8_t hat);
virtual void OnButtonUp(uint8_t but_id);
virtual void OnButtonDn(uint8_t but_id);
};

#define RPT_GEMEPAD_LEN 5

class JoystickReportParser : public HIDReportParser
{
JoystickEvents *joyEvents;

uint8_t oldPad[RPT_GEMEPAD_LEN];
uint8_t oldHat;
uint16_t oldButtons;

public:
JoystickReportParser(JoystickEvents *evt);

virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
};

#endif // __HIDJOYSTICKRPTPARSER_H__

Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The sketch posted in the first box does not include LiquidCrystal.h. It needs to.
Logged

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Mr.Paul it now compiles but only till i do not use the library as soon as i put in lcd.print(); in the .cpp code it shows me the following error:

USBHIDJoystick.cpp.o:(.bss.lcd+0x0): multiple definition of `lcd'
hidjoystickrptparser.cpp.o:(.bss.lcd+0x0): first defined here

it shows im defining it multiple times which i understand and remove the LiquidCrystal lcd(6,9,8,5,4,3,2); from the .cpp file but then i get this error:

hidjoystickrptparser.cpp: In member function 'virtual void JoystickEvents::OnHatSwitch(uint8_t)':
hidjoystickrptparser.cpp:83: error: 'lcd' was not declared in this scope


codes again

PDE/Main Arduino code:

Code:
#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
#include <avrpins.h>
#include <max3421e.h>
#include <usbhost.h>
#include <usb_ch9.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <address.h>
#include <hid.h>
#include <hiduniversal.h>

#include "hidjoystickrptparser.h"

#include <printhex.h>
#include <message.h>
#include <hexdump.h>
#include <parsetools.h>

USB                                             Usb;
USBHub                                          Hub(&Usb);
HIDUniversal                                    Hid(&Usb);
JoystickEvents                                  JoyEvents;
JoystickReportParser                            Joy(&JoyEvents);
LiquidCrystal lcd(6,9,8,5,4,3,2);
void setup()
{
  Serial.begin( 9600 );
  Serial.println("Start");
  lcd.begin(20, 4);
  if (Usb.Init() == -1)
      Serial.println("OSC did not start.");
     
  delay( 200 );

  if (!Hid.SetReportParser(0, &Joy))
      ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1  );
}

void loop()
{
    Usb.Task();
}

.cpp file code:

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(6,9,8,5,4,3,2);
#include "hidjoystickrptparser.h"

JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
joyEvents(evt),
oldHat(0xDE),
oldButtons(0)
{
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
oldPad[i] = 0xD;
}

void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
{
bool match = true;

// Checking if there are changes in report since the method was last called
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
if (buf[i] != oldPad[i])
{
match = false;
break;
}

// Calling Game Pad event handler
if (!match && joyEvents)
{
joyEvents->OnGamePadChanged((const GamePadEventData*)buf);

for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];
}

uint8_t hat = (buf[5] & 0xF);

// Calling Hat Switch event handler
if (hat != oldHat && joyEvents)
{
joyEvents->OnHatSwitch(hat);
oldHat = hat;
}

uint16_t buttons = (0x0000 | buf[6]);
buttons <<= 4;
buttons |= (buf[5] >> 4);
uint16_t changes = (buttons ^ oldButtons);

// Calling Button Event Handler for every button changed
if (changes)
{
for (uint8_t i=0; i<0x0C; i++)
{
uint16_t mask = (0x0001 << i);

if (((mask & changes) > 0) && joyEvents)
if ((buttons & mask) > 0)
joyEvents->OnButtonDn(i+1);
else
joyEvents->OnButtonUp(i+1);
}
oldButtons = buttons;
}
}

void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)
{
Serial.print("X: ");
PrintHex<uint8_t>(evt->X);
Serial.print("\tY: ");
PrintHex<uint8_t>(evt->Y);
Serial.print("\tZ: ");
PrintHex<uint8_t>(evt->Z1);
Serial.print("\tZ: ");
PrintHex<uint8_t>(evt->Z2);
Serial.print("\tRz: ");
PrintHex<uint8_t>(evt->Rz);
Serial.println("");
}

void JoystickEvents::OnHatSwitch(uint8_t hat)
{
Serial.print("Hat Switch: ");
PrintHex<uint8_t>(hat);
        lcd.print("hat");
Serial.println("");
}

void JoystickEvents::OnButtonUp(uint8_t but_id)
{
Serial.print("Up: ");
Serial.println(but_id, DEC);
}

void JoystickEvents::OnButtonDn(uint8_t but_id)
{
Serial.print("Dn: ");
Serial.println(but_id, DEC);
}

and the THIRD box .h file remaining the same unchanged as unneeded
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, now you have two instances of the LiquidCrystal class using the same hardware. That is not allowed, apparently.

You might be able to get do something like this in hidjoystickrptparser.cpp:
LiquidCrystal lcd(6,9,8,5,4,3,2);
extern LiquidCrystal lcd;

This would inherit the lcd instance from the sketch.

If that doesn't work, then you'd need to rewrite the LiquidCrystal class to give it a no-argument constructor and a begin() method that takes the arguments the current constructor takes (as the class should have been written in the first place).
Logged

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

extern doesnt work seems i need to mod the lcd lib class now! ..Man!!!!!
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it now! its working with extern placed but i have tested compiling not real LCD play but then if it compiles it will 99% work as needed so more or less it seems the things are fine! thanks to extensive knowledge pool of your;s Mr.Paul.
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm getting things compiled finely! BUT whatever i put in the .cpp file to print is not getting printing only the Arduino PDE file LCD prints are working, Below is the code:

PDE file:
Code:
#include <avr/pgmspace.h>

#include <avrpins.h>
#include <max3421e.h>
#include <usbhost.h>
#include <usb_ch9.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <address.h>
#include <hid.h>
#include <hiduniversal.h>

#include "hidjoystickrptparser.h"

#include <printhex.h>
#include <message.h>
#include <hexdump.h>
#include <parsetools.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(6,8,5,4,3,2);
USB                                             Usb;
USBHub                                          Hub(&Usb);
HIDUniversal                                    Hid(&Usb);
JoystickEvents                                  JoyEvents;
JoystickReportParser                            Joy(&JoyEvents);

void setup()
{
  Serial.begin(9600);
  lcd.begin(20, 4);
  Serial.println("Start");
  lcd.clear();
  lcd.print("Start");

  if (Usb.Init() == -1) {
      lcd.print(" - OSC did not Start");
      Serial.println("OSC did not start.");
  }
  delay( 200 );

  if (!Hid.SetReportParser(0, &Joy))
      ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1  );
}

void loop()
{
    Usb.Task();
}


.cpp file from which i want the LCD prints to work:

Code:
#include "hidjoystickrptparser.h"
#include <LiquidCrystal.h>
extern LiquidCrystal lcd;

JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
joyEvents(evt),
oldHat(0xDE),
oldButtons(0)
{
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
oldPad[i] = 0xD;
}

void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
{
bool match = true;

// Checking if there are changes in report since the method was last called
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
if (buf[i] != oldPad[i])
{
match = false;
break;
}

// Calling Game Pad event handler
if (!match && joyEvents)
{
joyEvents->OnGamePadChanged((const GamePadEventData*)buf);

for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];
}

uint8_t hat = (buf[5] & 0xF);

// Calling Hat Switch event handler
if (hat != oldHat && joyEvents)
{
joyEvents->OnHatSwitch(hat);
oldHat = hat;
}

uint16_t buttons = (0x0000 | buf[6]);
buttons <<= 4;
buttons |= (buf[5] >> 4);
uint16_t changes = (buttons ^ oldButtons);

// Calling Button Event Handler for every button changed
if (changes)
{
for (uint8_t i=0; i<0x0C; i++)
{
uint16_t mask = (0x0001 << i);

if (((mask & changes) > 0) && joyEvents)
if ((buttons & mask) > 0)
joyEvents->OnButtonDn(i+1);
else
joyEvents->OnButtonUp(i+1);
}
oldButtons = buttons;
}
}

void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)
{
Serial.print("X: ");
PrintHex<uint8_t>(evt->X);
        Serial.print(",");
        Serial.println(evt->X, DEC);
Serial.print("\tY: ");
PrintHex<uint8_t>(evt->Y);
        Serial.print(",");
        Serial.println(evt->Y, DEC);
Serial.print("\tZ: ");
PrintHex<uint8_t>(evt->Z1);
        Serial.print(",");
        Serial.println(evt->Z1, DEC);
Serial.print("\tZ: ");
PrintHex<uint8_t>(evt->Z2);
        Serial.print(",");
        Serial.println(evt->Z2, DEC);
Serial.print("\tRz: ");
PrintHex<uint8_t>(evt->Rz);
        Serial.print(",");
        Serial.println(evt->Rz, DEC);
Serial.println("");
}

void JoystickEvents::OnHatSwitch(uint8_t hat)
{
Serial.print("Hat Switch: ");
        lcd.begin(20, 4);
lcd.print(hat, DEC);
Serial.println(hat, DEC);
}

void JoystickEvents::OnButtonUp(uint8_t but_id)
{
Serial.print("Up: ");
Serial.println(but_id, DEC);
}

void JoystickEvents::OnButtonDn(uint8_t but_id)
{
Serial.print("Dn: ");
Serial.println(but_id, DEC);
}

.H header file remains unchanged!

Remedy?
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void JoystickEvents::OnHatSwitch(uint8_t hat)
{
Serial.print("Hat Switch: ");
        lcd.begin(20, 4);
lcd.print(hat, DEC);
Serial.println(hat, DEC);
}
Why are you calling lcd.begin() here? You've already called lcd.begin() in setup() in the sketch.
Logged

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why are you calling lcd.begin() here? You've already called lcd.begin() in setup() in the sketch.

Thought the way we are re instantiating stuff in .cpp file in the same way i need to to this again, Even with it nothing happens now just removed it.
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Any Whereabouts? of why LCD prints aren't happening in the .cpp file?
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You didn't position LCD cursor so how do you expect to see th print out?
Logged


ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You didn't position LCD cursor so how do you expect to see th print out?

Im doing it but  is it not suppose to for default print on the first row and 1 column as be do just lcd.print in the hello world programme wihtout setting the cursor?
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Default print starts the printing at whatever last position of the cursor is. You need to position the cursor to where you want to print before you print.
Logged


ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I did that but on the whole nothing shows up on the screen now at all.
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1573
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I got the problem i think , The case is that the reset from the arduino is linked to the 7th pin of the USB shield(which is also connected to arduino inturn offcourse) to make this shield work and now the case is that LCD doesnot show anything/anychanges or even not working only when i try to use it along with the USB host shield , so now i need a remedy for this.

Is their any working out suggested here?

Regards--
Nishant
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Pages: 1 [2]   Go Up
Jump to: