firmware/main.c File Reference

Main functions for USB-keyboard. More...

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
#include "usbdrv.h"
#include "keycodes.h"

Go to the source code of this file.

Data Structures

struct  Key
 This structure can be used as a container for a single 'key'. More...

Defines

#define F_CPU   12000000L
 we use a 12MHz crystal
#define PORTCOLUMNS   PORTB
 port on which we read the state of the columns
#define PINCOLUMNS   PINB
 port on which we read the state of the columns
#define DDRCOLUMNS   DDRB
 port on which we read the state of the columns
#define PORTROWS1   PORTA
 first port connected to the matrix rows
#define PINROWS1   PINA
 first port connected to the matrix rows
#define DDRROWS1   DDRA
 first port connected to the matrix rows
#define PORTROWS2   PORTC
 second port connected to the matrix rows
#define PINROWS2   PINC
 second port connected to the matrix rows
#define DDRROWS2   DDRC
 second port connected to the matrix rows
#define PORTLEDS   PORTD
 port on which the LEDs are connected
#define PINLEDS   PIND
 port on which the LEDs are connected
#define DDRLEDS   DDRD
 port on which the LEDs are connected
#define LEDSCROLL   PIND4
 address of the scroll-lock LED
#define LEDCAPS   PIND5
 address of the caps-lock LED
#define LEDNUM   PIND6
 address of the num-lock LED
#define PORTJUMPERS   PORTD
 port for additional jumpers
#define PINJUMPERS   PIND
 port for additional jumpers
#define DDRJUMPERS   DDRD
 port for additional jumpers
#define JUMPER0   PD1
 address for jumper 0
#define JUMPER1   PD3
 address for jumper 1
#define JUMPER2   PD7
 address for jumper 2
#define LED_NUM   0x01
 num LED on a boot-protocol keyboard
#define LED_CAPS   0x02
 caps LED on a boot-protocol keyboard
#define LED_SCROLL   0x04
 scroll LED on a boot-protocol keyboard
#define LED_COMPOSE   0x08
 compose LED on a boot-protocol keyboard
#define LED_KANA   0x10
 kana LED on a boot-protocol keyboard

Functions

uint8_t usbFunctionSetup (uint8_t data[8])
 This function is called whenever we receive a setup request via USB.
uint8_t usbFunctionWrite (uchar *data, uchar len)
 The write function is called when LEDs should be set.
void usbSendReport (uint8_t mode, uint8_t key)
 Send a single report to the computer.
Key charToKey (char character)
 Convert an ASCII-character to the corresponding key-code and modifier-code combination.
void sendKey (Key keytosend)
 Send a key to the computer, followed by the release of all keys.
void sendString (char *string)
 Send a string to the computer.
void printMatrix (void)
 Print the current state of the keyboard in a readable form.
uint8_t scankeys (void)
 Scan and debounce keypresses.
int main (void)
 Main function, containing the main loop that manages timer- and USB-functionality.

Variables

uint8_t expectReport = 0
 flag to indicate if we expect an USB-report
uint8_t LEDstate = 0
 current state of the LEDs
char PROGMEM usbHidReportDescriptor [USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH]
 USB report descriptor (length is defined in usbconfig.h).
uint8_t curmatrix [16]
 contains current state of the keyboard
const uint8_t PROGMEM keymatrix [16][8]
 The keymatrix-array contains positions of keys in the matrix.
const uint8_t PROGMEM modmatrix [16][8]
 The modmatrix-array contains positions of the modifier-keys in the matrix.


Detailed Description

Main functions for USB-keyboard.

Author:
Ronald Schaten <ronald@schatenseite.de>
Version:
Id
main.c,v 1.5 2008-07-12 21:05:24 rschaten Exp

License: GNU GPL v2 (see License.txt)

Definition in file main.c.


Define Documentation

#define DDRCOLUMNS   DDRB

port on which we read the state of the columns

Definition at line 225 of file main.c.

#define DDRJUMPERS   DDRD

port for additional jumpers

Definition at line 242 of file main.c.

#define DDRLEDS   DDRD

port on which the LEDs are connected

Definition at line 235 of file main.c.

#define DDRROWS1   DDRA

first port connected to the matrix rows

Definition at line 228 of file main.c.

Referenced by scankeys().

#define DDRROWS2   DDRC

second port connected to the matrix rows

Definition at line 231 of file main.c.

Referenced by scankeys().

#define F_CPU   12000000L

we use a 12MHz crystal

Definition at line 207 of file main.c.

#define JUMPER0   PD1

address for jumper 0

Definition at line 243 of file main.c.

#define JUMPER1   PD3

address for jumper 1

Definition at line 244 of file main.c.

#define JUMPER2   PD7

address for jumper 2

Definition at line 245 of file main.c.

#define LED_CAPS   0x02

caps LED on a boot-protocol keyboard

Definition at line 291 of file main.c.

Referenced by usbFunctionWrite().

#define LED_COMPOSE   0x08

compose LED on a boot-protocol keyboard

Definition at line 293 of file main.c.

#define LED_KANA   0x10

kana LED on a boot-protocol keyboard

Definition at line 294 of file main.c.

#define LED_NUM   0x01

num LED on a boot-protocol keyboard

Definition at line 290 of file main.c.

Referenced by usbFunctionWrite().

#define LED_SCROLL   0x04

scroll LED on a boot-protocol keyboard

Definition at line 292 of file main.c.

Referenced by usbFunctionWrite().

#define LEDCAPS   PIND5

address of the caps-lock LED

Definition at line 237 of file main.c.

Referenced by usbFunctionWrite().

#define LEDNUM   PIND6

address of the num-lock LED

Definition at line 238 of file main.c.

Referenced by usbFunctionWrite().

#define LEDSCROLL   PIND4

address of the scroll-lock LED

Definition at line 236 of file main.c.

Referenced by usbFunctionWrite().

#define PINCOLUMNS   PINB

port on which we read the state of the columns

Definition at line 224 of file main.c.

Referenced by scankeys().

#define PINJUMPERS   PIND

port for additional jumpers

Definition at line 241 of file main.c.

#define PINLEDS   PIND

port on which the LEDs are connected

Definition at line 234 of file main.c.

#define PINROWS1   PINA

first port connected to the matrix rows

Definition at line 227 of file main.c.

#define PINROWS2   PINC

second port connected to the matrix rows

Definition at line 230 of file main.c.

#define PORTCOLUMNS   PORTB

port on which we read the state of the columns

Definition at line 223 of file main.c.

#define PORTJUMPERS   PORTD

port for additional jumpers

Definition at line 240 of file main.c.

#define PORTLEDS   PORTD

port on which the LEDs are connected

Definition at line 233 of file main.c.

Referenced by usbFunctionWrite().

#define PORTROWS1   PORTA

first port connected to the matrix rows

Definition at line 226 of file main.c.

Referenced by scankeys().

#define PORTROWS2   PORTC

second port connected to the matrix rows

Definition at line 229 of file main.c.

Referenced by scankeys().


Function Documentation

Key charToKey ( char  character  ) 

Convert an ASCII-character to the corresponding key-code and modifier-code combination.

character ASCII-character to convert

Returns:
structure containing the combination

Definition at line 493 of file main.c.

References Key::key, KEY_0, KEY_1, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_apostroph, KEY_backslash, KEY_comma, KEY_dot, KEY_equals, KEY_grave, KEY_hash, KEY_lbracket, KEY_minus, KEY_rbracket, KEY_Reserved, KEY_semicolon, KEY_slash, KEY_Spacebar, MOD_NONE, MOD_SHIFT_LEFT, and Key::mode.

Referenced by sendString().

int main ( void   ) 

Main function, containing the main loop that manages timer- and USB-functionality.

/return the obligatory integer that nobody cares about...

Definition at line 755 of file main.c.

References scankeys().

void printMatrix ( void   ) 

Print the current state of the keyboard in a readable form.

This function is used for debug-purposes only.

Definition at line 642 of file main.c.

References curmatrix, and sendString().

uint8_t scankeys ( void   ) 

Scan and debounce keypresses.

This is the main worker function for normal keyboard operation, the code contains lot of comments. Basically, it first scans the keyboard state. If a change is detected, it initializes a counter that is decreased each time this function is called. If the counter reaches 1, that means that the same scan result has been scanned ten times in a row, so we can be pretty sure that the keys are in a certain state (as in: not bouncing). Then, the codes for keys and modifiers are searched from the two arrays, the USB-message to send the state is prepared. The return value of this function indicates if the message has to be sent.

Returns:
flag to indicate whether something has changed

Definition at line 679 of file main.c.

References curmatrix, DDRROWS1, DDRROWS2, KEY_ErrorRollOver, KEY_Reserved, keymatrix, MOD_NONE, modmatrix, PINCOLUMNS, PORTROWS1, and PORTROWS2.

Referenced by main().

void sendKey ( Key  keytosend  ) 

Send a key to the computer, followed by the release of all keys.

This can be used repetitively to send a string.

Parameters:
keytosend key structure to send

Definition at line 621 of file main.c.

References Key::key, Key::mode, and usbSendReport().

Referenced by sendString().

void sendString ( char *  string  ) 

Send a string to the computer.

This function converts each character of an ASCII-string to a key-structure and uses sendKey() to send it.

Parameters:
string string to send

Definition at line 631 of file main.c.

References charToKey(), and sendKey().

Referenced by printMatrix().

uint8_t usbFunctionSetup ( uint8_t  data[8]  ) 

This function is called whenever we receive a setup request via USB.

Parameters:
data[8] eight bytes of data we received
Returns:
number of bytes to use, or 0xff if usbFunctionWrite() should be called

Definition at line 343 of file main.c.

References expectReport.

uint8_t usbFunctionWrite ( uchar *  data,
uchar  len 
)

The write function is called when LEDs should be set.

Normally, we get only one byte that contains info about the LED states.

Parameters:
data pointer to received data
len number ob bytes received
Returns:
0x01

Definition at line 384 of file main.c.

References expectReport, LED_CAPS, LED_NUM, LED_SCROLL, LEDCAPS, LEDNUM, LEDSCROLL, LEDstate, and PORTLEDS.

void usbSendReport ( uint8_t  mode,
uint8_t  key 
)

Send a single report to the computer.

This function is not used during normal typing, it is only used to send non-pressed keys to simulate input.

Parameters:
mode modifier-byte
key key-code

Definition at line 413 of file main.c.

Referenced by sendKey().


Variable Documentation

uint8_t curmatrix[16]

contains current state of the keyboard

Definition at line 424 of file main.c.

Referenced by printMatrix(), and scankeys().

uint8_t expectReport = 0

flag to indicate if we expect an USB-report

Definition at line 288 of file main.c.

Referenced by usbFunctionSetup(), and usbFunctionWrite().

const uint8_t PROGMEM keymatrix[16][8]

Initial value:

The keymatrix-array contains positions of keys in the matrix.

Here you can see which row is connected to which column when a key is pressed. This array probably has to be modified if this firmware is ported to a different keyboard.

See also:
modmatrix

Definition at line 433 of file main.c.

Referenced by scankeys().

uint8_t LEDstate = 0

current state of the LEDs

Definition at line 295 of file main.c.

Referenced by usbFunctionWrite().

const uint8_t PROGMEM modmatrix[16][8]

Initial value:

 { 
  
    {MOD_NONE,     MOD_NONE,       MOD_CONTROL_LEFT, MOD_NONE, MOD_NONE, MOD_NONE, MOD_CONTROL_RIGHT, MOD_NONE     }, 
    {MOD_NONE,     MOD_SHIFT_LEFT, MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_SHIFT_RIGHT,   MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_NONE,     MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_NONE     }, 
    {MOD_ALT_LEFT, MOD_NONE,       MOD_NONE,         MOD_NONE, MOD_NONE, MOD_NONE, MOD_NONE,          MOD_ALT_RIGHT}, 
}
The modmatrix-array contains positions of the modifier-keys in the matrix.

It is built in the same way as the keymatrix-array.

See also:
keymatrix

Definition at line 458 of file main.c.

Referenced by scankeys().

char PROGMEM usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH]

USB report descriptor (length is defined in usbconfig.h).

The report descriptor has been created with usb.org's "HID Descriptor Tool" which can be downloaded from http://www.usb.org/developers/hidpage/ (it's an .exe, but it even runs under Wine).

Definition at line 302 of file main.c.


Generated on Sat Jul 12 22:27:15 2008 for Dulcimer by  doxygen 1.5.4