Page 1 of 1

Can't send double letters?

Posted: Fri Jan 18, 2013 4:51 am
by imagiro1
Sorta have the string working. I am trying to send a string "http://google.com". But all I get back is htp:/gogle.com. I can't figure out what I'm doing wrong. Thanks.

Code: Select all

char sendString[] PROGMEM = "http://google.com";
    while(1) {
        PORTC |= (1 << PC4);
       
        wdt_reset(); // keep the watchdog happy
        usbPoll();
       
        _delay_ms(50);
       
        for (i = 0; i < sizeof(sendString); i++) {
           
            if(usbInterruptIsReady() ){
               
                //Send char
                //buildReport(sendArray[i]);
               
                //Send string
                buildReport(sendString[i]);
               
                //PORTC |= (1<<PC4);
               
                //Send report
                usbSetInterrupt((void *)&keyboard_report, sizeof(keyboard_report));
                //PORTC &= ~(1 << PC4);
                _delay_ms(50);
               
                //Clear report
                for(j = 0; j < sizeof(keyboard_report); j++){
                    ((uchar *)&keyboard_report)[j] = 0;
                }
            }
        }
    }
    return 0;
}

Re: Can't send double letters?

Posted: Fri Jan 18, 2013 5:56 am
by declan smith
_delay_ms(50) ? what is that for ?

that means:
- 1 x 50ms delay before "for loop" (not sure for what)
- 17 x 50ms delay inside "for loop" (I didn't get it either)

give total of more than 800ms before next usbPool()? and you didn't get device disconnect? that's strange :roll:

Try to reduce the delay to under 20ms for total between usbPool() to maintain connection.
I don't think the delay needed, but try to use " _delay_ms(1)" if you need it.

Re: Can't send double letters?

Posted: Fri Jan 18, 2013 4:17 pm
by imagiro1
That delay was put in there to see if maybe I was trying to send too quickly. It was not the case.

It looks like the device is disconnecting. I added a light for testing. I had it just come on when it entered the while(1) loop. I couldn't figure out for the life of me why at some point it would turn off. That delay would explain it.

Furthermore, I did more testing. I changed the string to something like "http://google.com cnn.com yahoo.com". It then sent something like "http:/gogle.com" So it looks like the double letters was just a fluke. But I still can not send a whole string.

I've never used the VUSB library before. I'm not sure if I'm even using it correctly. I'm basing my code off code and life's work. His code doesn't send letters as quick as I'm trying to send them. I don't know if speed matters. Hence the delay I had thrown in there.

Re: Can't send double letters?

Posted: Sat Jan 19, 2013 5:01 am
by declan smith
That delay was put in there to see if maybe I was trying to send too quickly. It was not the case.

In USB connection "delay" can be a "case" if it too long.
If you want to delay something (especially long delay), I suggest you are using counter in while() loop. It's more save.


I'm basing my code off code and life's work.

You didn't mention which one, so I tried to use "Password Generator" from code and life's.
I disabled "eeprom write and read" lines and change the original string to:

Code: Select all

PROGMEM uchar measuring_message[] ="http://google.com cnn.com yahoo.com";

PROGMEM uchar finish_message[] = " New password saved.AAnndd TTrryy TTTToooo TTTyyppeeeee LLLLooooonnngggg TTTTTeeeexxxxtttt";



Here's the Password Generator code I used from code and life's :

Code: Select all

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>

#include "usbdrv.h"

//#define F_CPU 12000000L
#include <util/delay.h>


// *********************************
// *** BASIC PROGRAM DEFINITIONS ***
// *********************************

#define PASS_LENGTH 2 // password length for generated password
#define SEND_ENTER 0 // define to 1 if you want to send ENTER after password

PROGMEM uchar measuring_message[] = "http://google.com cnn.com yahoo.com";

PROGMEM uchar finish_message[] = " New password saved.AAnndd TTrryy TTTToooo TTTyyppeeeee LLLLooooonnngggg TTTTTeeeexxxxtttt";

// The buffer needs to accommodate the messages above and the password
#define MSG_BUFFER_SIZE 200

EEMEM uchar stored_password[MSG_BUFFER_SIZE];


// ************************
// *** USB HID ROUTINES ***
// ************************

// From Frank Zhao's USB Business Card project
// http://www.frank-zhao.com/cache/usbbusinesscard_details.php
PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = {
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x06,                    // USAGE (Keyboard)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x75, 0x01,                    //   REPORT_SIZE (1)
    0x95, 0x08,                    //   REPORT_COUNT (8)
    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)(Key Codes)
    0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)(224)
    0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)(231)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
    0x81, 0x02,                    //   INPUT (Data,Var,Abs) ; Modifier byte
    0x95, 0x01,                    //   REPORT_COUNT (1)
    0x75, 0x08,                    //   REPORT_SIZE (8)
    0x81, 0x03,                    //   INPUT (Cnst,Var,Abs) ; Reserved byte
    0x95, 0x05,                    //   REPORT_COUNT (5)
    0x75, 0x01,                    //   REPORT_SIZE (1)
    0x05, 0x08,                    //   USAGE_PAGE (LEDs)
    0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
    0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
    0x91, 0x02,                    //   OUTPUT (Data,Var,Abs) ; LED report
    0x95, 0x01,                    //   REPORT_COUNT (1)
    0x75, 0x03,                    //   REPORT_SIZE (3)
    0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs) ; LED report padding
    0x95, 0x06,                    //   REPORT_COUNT (6)
    0x75, 0x08,                    //   REPORT_SIZE (8)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)(Key Codes)
    0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))(0)
    0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)(101)
    0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
    0xc0                           // END_COLLECTION
};

typedef struct {
   uint8_t modifier;
   uint8_t reserved;
   uint8_t keycode[6];
} keyboard_report_t;

static keyboard_report_t keyboard_report; // sent to PC
volatile static uchar LED_state = 0xff; // received from PC
static uchar idleRate; // repeat rate for keyboards

#define STATE_SEND 1
#define STATE_DONE 0

static uchar messageState = STATE_DONE;
static uchar messageBuffer[MSG_BUFFER_SIZE] = "";
static uchar messagePtr = 0;
static uchar messageCharNext = 1;


#define MOD_SHIFT_LEFT (1<<1)

// The buildReport is called by main loop and it starts transmitting
// characters when messageState == STATE_SEND. The message is stored
// in messageBuffer and messagePtr tells the next character to send.
// Remember to reset messagePtr to 0 after populating the buffer!
uchar buildReport() {
    uchar ch;
   
    if(messageState == STATE_DONE || messagePtr >= sizeof(messageBuffer) || messageBuffer[messagePtr] == 0) {
        keyboard_report.modifier = 0;
        keyboard_report.keycode[0] = 0;
        return STATE_DONE;
    }

    if(messageCharNext) { // send a keypress
        ch = messageBuffer[messagePtr++];
       
        // convert character to modifier + keycode
        if(ch >= '0' && ch <= '9') {
            keyboard_report.modifier = 0;
            keyboard_report.keycode[0] = (ch == '0') ? 39 : 30+(ch-'1');
        } else if(ch >= 'a' && ch <= 'z') {
            keyboard_report.modifier = (LED_state & 2) ? MOD_SHIFT_LEFT : 0;
            keyboard_report.keycode[0] = 4+(ch-'a');
        } else if(ch >= 'A' && ch <= 'Z') {
            keyboard_report.modifier = (LED_state & 2) ? 0 : MOD_SHIFT_LEFT;
            keyboard_report.keycode[0] = 4+(ch-'A');
        } else {
            keyboard_report.modifier = 0;
            keyboard_report.keycode[0] = 0;
            switch(ch) {
         case '.':
            keyboard_report.keycode[0] = 0x37;
            break;
         case '_':
            keyboard_report.modifier = MOD_SHIFT_LEFT;
         case '-':
            keyboard_report.keycode[0] = 0x2D;
            break;
            case ' ':
                keyboard_report.keycode[0] = 0x2C;
                break;
            case '\t':
                keyboard_report.keycode[0] = 0x2B;
                break;
            case '\n':
                keyboard_report.keycode[0] = 0x28;
                break;
            }
        }
    } else { // key release before the next keypress!
        keyboard_report.modifier = 0;
        keyboard_report.keycode[0] = 0;
    }
   
    messageCharNext = !messageCharNext; // invert for toggling, key release report must be sent
                                       // before next key pressed report
   
    return STATE_SEND;
}

usbMsgLen_t usbFunctionSetup(uchar data[8]) {
    usbRequest_t *rq = (void *)data;

    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
        switch(rq->bRequest) {
        case USBRQ_HID_GET_REPORT: // send "no keys pressed" if asked here
            // wValue: ReportType (highbyte), ReportID (lowbyte)
            usbMsgPtr = (void *)&keyboard_report; // we only have this one
            keyboard_report.modifier = 0;
            keyboard_report.keycode[0] = 0;
            return sizeof(keyboard_report);
      case USBRQ_HID_SET_REPORT: // if wLength == 1, should be LED state
            return (rq->wLength.word == 1) ? USB_NO_MSG : 0;
        case USBRQ_HID_GET_IDLE: // send idle rate to PC as required by spec
            usbMsgPtr = &idleRate;
            return 1;
        case USBRQ_HID_SET_IDLE: // save idle rate as required by spec
            idleRate = rq->wValue.bytes[1];
            return 0;
        }
    }
   
    return 0; // by default don't return any data
}

void caps_toggle(); // defined later in program logic

usbMsgLen_t usbFunctionWrite(uint8_t * data, uchar len) {
   if (data[0] == LED_state)
        return 1;
    else
        LED_state = data[0];
   
    caps_toggle();
   
   return 1; // Data read, not expecting more
}

// *********************
// *** PROGRAM LOGIC ***
// *********************

// Routine to return a random character - currently uses the timer 0 as
// a source for randomness - it goes from 0 to 255 over 7800 times a second
// so it should be random enough (remember, this is called after user
// presses caps lock). Of course USB timings may decrease this randomness
// - replace with a better one if you want.
uchar generate_character() {
    uchar counter = TCNT0 & 63;
   
    if(counter < 26)
        return 'a' + counter;
    else if(counter < 52)
        return 'A' + counter - 26;
    else if(counter < 62)
        return '0' + counter - 52;
    else if(counter == 62)
        return '-';
    else
        return '_';
}

#define CAPS_COUNTING 0
#define CAPS_MEASURING 1

static uchar capsCount = 0;
static uchar capsState = CAPS_COUNTING;

// This routine is called by usbFunctionWrite every time the keyboard LEDs
// toggle - basically we count 4 toggles and then start regenerating
void caps_toggle() {
    if(capsState == CAPS_COUNTING) {   
        if(capsCount++ < 1)
            return;
       
        capsCount = 0;
        capsState = CAPS_MEASURING;

        // Type a message to the PC that we're regenerating the password
        memcpy_P(messageBuffer, measuring_message, sizeof(measuring_message));
        messagePtr = 0;
        messageState = STATE_SEND;
    } else {
        messageBuffer[capsCount++] = generate_character();
       
        if(capsCount >= PASS_LENGTH) { // enough characters generated
#if SEND_ENTER
            messageBuffer[capsCount++] = '\n';
#endif
            messageBuffer[capsCount] = '\0';
           
            // Store password to EEPROM - might lose the USB connection, but so what
            //eeprom_write_block(messageBuffer, stored_password, sizeof(messageBuffer));
           
            // Type a message to the PC that new password has been generated
            memcpy_P(messageBuffer, finish_message, sizeof(finish_message));
            messagePtr = 0;
            messageState = STATE_SEND;
           
            capsCount = 0;
            capsState = CAPS_COUNTING;
        }
    }   
}


#define abs(x) ((x) > 0 ? (x) : (-x))

// Called by V-USB after device reset
void hadUsbReset() {
    int frameLength, targetLength = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
    int bestDeviation = 9999;
    uchar trialCal, bestCal = 0, step, region;

    // do a binary search in regions 0-127 and 128-255 to get optimum OSCCAL
    for(region = 0; region <= 1; region++) {
        frameLength = 0;
        trialCal = (region == 0) ? 0 : 128;
       
        for(step = 64; step > 0; step >>= 1) {
            if(frameLength < targetLength) // true for initial iteration
                trialCal += step; // frequency too low
            else
                trialCal -= step; // frequency too high
               
            OSCCAL = trialCal;
            frameLength = usbMeasureFrameLength();
           
            if(abs(frameLength-targetLength) < bestDeviation) {
                bestCal = trialCal; // new optimum found
                bestDeviation = abs(frameLength -targetLength);
            }
        }
    }

    OSCCAL = bestCal;
}

int main() {
   uchar i;
   
    // Fetch password from EEPROM and send it
    //eeprom_read_block(messageBuffer, stored_password, sizeof(messageBuffer));
    messagePtr = 0;
    messageState = STATE_SEND;
   
    for(i=0; i<sizeof(keyboard_report); i++) // clear report initially
        ((uchar *)&keyboard_report)[i] = 0;

    wdt_enable(WDTO_1S); // enable 1s watchdog timer

    usbInit();
   
    usbDeviceDisconnect(); // enforce re-enumeration
    for(i = 0; i<250; i++) { // wait 500 ms
        wdt_reset(); // keep the watchdog happy
        _delay_ms(2);
    }
    usbDeviceConnect();
   
    TCCR0 |= (1 << CS01); // timer 0 at clk/8 will generate randomness

    sei(); // Enable interrupts after re-enumeration
   
    while(1)
   {
        wdt_reset(); // keep the watchdog happy
        usbPoll();
      // characters are sent when messageState == STATE_SEND and after receiving
        // the initial LED state from PC (good way to wait until device is recognized)
        if(usbInterruptIsReady() && messageState == STATE_SEND && LED_state != 0xff)
      {
            messageState = buildReport();
            usbSetInterrupt((void *)&keyboard_report, sizeof(keyboard_report));
        }
    }
   
    return 0;
}


And here's the way you test it (if you're using Windows):
1. Open Notepad
2. Attach Device and wait for "ding" sound
3. Hit Capslock, it will type : httpgoogle.com cnn.com yahoo.com ( measuring_message[] string but no"://" )
4. Hit Capslock again twice and it will type : New password saved.AAnndd TTrryy TTTToooo TTTyyppeeeee LLLLooooonnngggg TTTTTeeeexxxxtttt

The result in the Notepad was the same text, except there's no "://" in the measuring_message[] result which I don't know why. Maybe you can figure it out and post it here :wink:
As you can see there's no problem with double or repeated with long string.
Hope it helps for your research :wink:


His code doesn't send letters as quick as I'm trying to send them. I don't know if speed matters.

The keyboard rate speed depends on keyboard idle rate which configured automatically by Host, so no need to worried about.


I've never used the VUSB library before. I'm not sure if I'm even using it correctly.

That's happen to everyone, more practice will help :wink:

Re: Can't send double letters?

Posted: Sat Jan 19, 2013 1:31 pm
by winelight
Strangely I did once experience a double letter problem. A dot matrix printer would print a corrupt line if you had a double letter "t" anywhere on it. You had to be careful what you wrote!

Re: Can't send double letters?

Posted: Wed Jan 23, 2013 5:18 am
by imagiro1
Thanks Declan. I didn't even think to look at the password generator code. I was using the HID keyboard. I had tried the original code from Frank Zhao's USB business card without success. And now I've figured out why. Linux or Windows running within a VM are not the same thing as Windows. The Linux box can display the text, but does not respond to the CAPS key being pressed. This is why I started from the HID keyboard example and was going to add a button to replace the CAP key functionality. And it simply doesn't work in windows that's being run in VMware.

As for adding : and /... _BV(1) is the same as MOD_SHIFT_LEFT

Code: Select all

case ':':
                    keyboard_report.modifier = _BV(1); // hold shift
                    // fall through

Code: Select all

case '/':
                    keyboard_report.keycode[0] = 0x38;
                    break;


I've also made my own character to replicate Windows key + r.

Code: Select all

case '\b':
                    keyboard_report.modifier = MOD_GUI_LEFT;
                    keyboard_report.keycode[0] = 4+('r' - 'a');
                    break;

So when you press the caps key, it launches Window+r and then http://google.com\n to launch google.com.

Thanks again for all the help.