Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rusakovich mikhail committed Apr 27, 2014
0 parents commit f721f85
Show file tree
Hide file tree
Showing 37 changed files with 6,106 additions and 0 deletions.
163 changes: 163 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
*.map
#################
## Eclipse
#################

*.pydevproject
.project
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath


#################
## Visual Studio
#################

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates

# Build results
[Dd]ebug/
[Rr]elease/
*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.vspscc
.builds
*.dotCover

## TODO: If you have NuGet Package Restore enabled, uncomment this
#packages/

# Visual C++ cache files
ipch/
*.ncb
*.opensdf
*.sdf

# Visual Studio profiler
*.psess
*.vsp

# ReSharper is a .NET coding add-in
_ReSharper*

# Installshield output folder
[Ee]xpress

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish

# Others
[Bb]in
[Oo]bj
sql
TestResults
*.Cache
ClientBin
stylecop.*
~$*
*.dbmdl
Generated_Code #added for RIA/Silverlight projects

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML



############
## Windows
############

# Windows image file caches
Thumbs.db

# Folder config file
Desktop.ini


#############
## Python
#############

*.py[co]

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox

#Translations
*.mo

#Mr Developer
.mr.developer.cfg

# Mac crap
.DS_Store
22 changes: 22 additions & 0 deletions ADC.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <avr/io.h>
#include <avr/iom8.h>

static unsigned int ADC_value=0;

static void ADC_init()
{
ADMUX=(1<<REFS0); // For Aref=AVcc;
ADCSRA=(1<<ADEN)|(1 << ADPS2)|(0 << ADPS1)|(1 << ADPS0);
}

static unsigned int ADC_result(unsigned char ch)
{
ch=ch&0b00000111;
ADMUX=(1<<REFS0)|(ch);
ADCSRA|=(1<<ADSC);
while((ADCSRA&(1 << ADIF))==0);
ADCSRA|=(1<<ADIF);
ADC_value=(ADCL|ADCH << 8);
return ADC_value;

}
1 change: 1 addition & 0 deletions HID_GamePad.aps
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<AVRStudio><MANAGEMENT><ProjectName>HID_GamePad</ProjectName><Created>10-Apr-2012 22:40:41</Created><LastEdit>21-Apr-2012 13:20:04</LastEdit><ICON>241</ICON><ProjectType>0</ProjectType><Created>10-Apr-2012 22:40:41</Created><Version>4</Version><Build>4, 18, 0, 670</Build><ProjectTypeName>AVR GCC</ProjectTypeName></MANAGEMENT><CODE_CREATION><ObjectFile>default\HID_GamePad.elf</ObjectFile><EntryFile></EntryFile><SaveFolder>C:\winavr\HID_GamePad\</SaveFolder></CODE_CREATION><DEBUG_TARGET><CURRENT_TARGET>AVR Simulator</CURRENT_TARGET><CURRENT_PART>ATmega8.xml</CURRENT_PART><BREAKPOINTS></BREAKPOINTS><IO_EXPAND><HIDE>false</HIDE></IO_EXPAND><REGISTERNAMES><Register>R00</Register><Register>R01</Register><Register>R02</Register><Register>R03</Register><Register>R04</Register><Register>R05</Register><Register>R06</Register><Register>R07</Register><Register>R08</Register><Register>R09</Register><Register>R10</Register><Register>R11</Register><Register>R12</Register><Register>R13</Register><Register>R14</Register><Register>R15</Register><Register>R16</Register><Register>R17</Register><Register>R18</Register><Register>R19</Register><Register>R20</Register><Register>R21</Register><Register>R22</Register><Register>R23</Register><Register>R24</Register><Register>R25</Register><Register>R26</Register><Register>R27</Register><Register>R28</Register><Register>R29</Register><Register>R30</Register><Register>R31</Register></REGISTERNAMES><COM>Auto</COM><COMType>0</COMType><WATCHNUM>0</WATCHNUM><WATCHNAMES><Pane0></Pane0><Pane1></Pane1><Pane2></Pane2><Pane3></Pane3></WATCHNAMES><BreakOnTrcaeFull>0</BreakOnTrcaeFull></DEBUG_TARGET><Debugger><Triggers></Triggers></Debugger><AVRGCCPLUGIN><FILES><SOURCEFILE>HID_GamePad.c</SOURCEFILE><SOURCEFILE>usbdrv\usbdrvasm.S</SOURCEFILE><SOURCEFILE>usbdrv\oddebug.c</SOURCEFILE><SOURCEFILE>usbdrv\usbdrv.c</SOURCEFILE><SOURCEFILE>ADC.c</SOURCEFILE><HEADERFILE>usbconfig.h</HEADERFILE><HEADERFILE>usbdrv\iarcompat.h</HEADERFILE><HEADERFILE>usbdrv\oddebug.h</HEADERFILE><HEADERFILE>usbdrv\usbconfig-prototype.h</HEADERFILE><HEADERFILE>usbdrv\usbdrv.h</HEADERFILE><OTHERFILE>default\HID_GamePad.lss</OTHERFILE><OTHERFILE>default\HID_GamePad.map</OTHERFILE></FILES><CONFIGS><CONFIG><NAME>default</NAME><USESEXTERNALMAKEFILE>NO</USESEXTERNALMAKEFILE><EXTERNALMAKEFILE></EXTERNALMAKEFILE><PART>atmega8</PART><HEX>1</HEX><LIST>1</LIST><MAP>1</MAP><OUTPUTFILENAME>HID_GamePad.elf</OUTPUTFILENAME><OUTPUTDIR>default\</OUTPUTDIR><ISDIRTY>0</ISDIRTY><OPTIONS/><INCDIRS/><LIBDIRS/><LIBS/><LINKOBJECTS/><OPTIONSFORALL>-Wall -gdwarf-2 -std=gnu99 -DF_CPU=12000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums</OPTIONSFORALL><LINKEROPTIONS></LINKEROPTIONS><SEGMENTS/></CONFIG></CONFIGS><LASTCONFIG>default</LASTCONFIG><USES_WINAVR>1</USES_WINAVR><GCC_LOC>C:\Program Files\WinAVR\bin\avr-gcc.exe</GCC_LOC><MAKE_LOC>C:\Program Files\WinAVR\utils\bin\make.exe</MAKE_LOC></AVRGCCPLUGIN><ProjectFiles><Files><Name>C:\winavr\HID_GamePad\usbconfig.h</Name><Name>C:\winavr\HID_GamePad\usbdrv\iarcompat.h</Name><Name>C:\winavr\HID_GamePad\usbdrv\oddebug.h</Name><Name>C:\winavr\HID_GamePad\usbdrv\usbconfig-prototype.h</Name><Name>C:\winavr\HID_GamePad\usbdrv\usbdrv.h</Name><Name>C:\winavr\HID_GamePad\HID_GamePad.c</Name><Name>C:\winavr\HID_GamePad\usbdrv\usbdrvasm.S</Name><Name>C:\winavr\HID_GamePad\usbdrv\oddebug.c</Name><Name>C:\winavr\HID_GamePad\usbdrv\usbdrv.c</Name><Name>C:\winavr\HID_GamePad\ADC.c</Name></Files></ProjectFiles><IOView><usergroups/><sort sorted="0" column="0" ordername="1" orderaddress="1" ordergroup="1"/></IOView><Files><File00000><FileId>00000</FileId><FileName>HID_GamePad.c</FileName><Status>1</Status></File00000><File00001><FileId>00001</FileId><FileName>usbconfig.h</FileName><Status>1</Status></File00001><File00002><FileId>00002</FileId><FileName>usbdrv\usbconfig-prototype.h</FileName><Status>1</Status></File00002><File00003><FileId>00003</FileId><FileName>usbdrv\usbdrv.h</FileName><Status>1</Status></File00003><File00004><FileId>00004</FileId><FileName>usbdrv\usbdrv.c</FileName><Status>1</Status></File00004><File00005><FileId>00005</FileId><FileName>usbdrv\oddebug.c</FileName><Status>1</Status></File00005><File00006><FileId>00006</FileId><FileName>usbdrv\iarcompat.h</FileName><Status>1</Status></File00006><File00007><FileId>00007</FileId><FileName>usbdrv\oddebug.h</FileName><Status>1</Status></File00007><File00008><FileId>00008</FileId><FileName>usbdrv\usbdrvasm.S</FileName><Status>1</Status></File00008><File00009><FileId>00009</FileId><FileName>ADC.c</FileName><Status>1</Status></File00009></Files><Events><Bookmarks></Bookmarks></Events><Trace><Filters></Filters></Trace></AVRStudio>
188 changes: 188 additions & 0 deletions HID_GamePad.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/* Name: main.c
* Project: HID-Test
* Author: Christian Starkjohann
* Creation Date: 2006-02-02
* Tabsize: 4
* Copyright: (c) 2006 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt)
* This Revision: $Id: main.c 299 2007-03-29 17:07:19Z cs $
*/

#define F_CPU 12000000L /* evaluation board runs on 4MHz */

#include <avr/io.h>
#include <avr/iom8.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <util/delay.h>

#include "usbdrv/usbdrv.h"
#include "usbdrv/oddebug.h"
#include "ADC.c"

static const unsigned int HIGH_THRESHOLD=800;
static const unsigned int LOW_THRESHOLD=50;

static void hardwareInit(void)
{
uchar i, j;
PORTD = 0xfa; /* 1111 1010 bin: activate pull-ups except on USB lines */
DDRD = 0x07; /* 0000 0111 bin: all pins input except USB (-> USB reset) */
PORTC = 0xff; /* activate all pull-ups */
DDRC = 0; /* all pins input */
j = 0;
while(--j){ /* USB Reset by device only required on Watchdog Reset */
i = 0;
while(--i); /* delay >10ms for USB reset */
}
DDRD = 0x02; /* 0000 0010 bin: remove USB reset condition */
/* configure timer 0 for a rate of 12M/(1024 * 256) = 45.78 Hz (~22ms) */
TCCR0 = 5; /* timer 0 prescaler: 1024 */
}

/* ------------------------------------------------------------------------- */

struct dataexchange_t
{
uchar report_b1;
uchar report_b2;
};

static struct dataexchange_t keyReport = {0, 0};


/* The following function returns an index for the first key pressed. It
* returns 0 if no key is pressed.
*/

static unsigned int keyPressed(void)
{
unsigned int x=0;
uchar j=0;
keyReport.report_b1=0;
keyReport.report_b2=0;

for (uchar i=0; i<6; i++){
x=ADC_result(i);
if (x<=LOW_THRESHOLD){
if (i<4)
keyReport.report_b1|=1<<(i+j);
else
keyReport.report_b2|=1<<((i-4)+j);
}
else
if (x<=HIGH_THRESHOLD){
if (i<4)
keyReport.report_b1|=1<<((i+1)+j);
else
if (i==5) keyReport.report_b2|=1<<((i-4)+j);
else keyReport.report_b2|=1<<((i-3)+j);
}
j++;
if (i==3) j=0;
}

return (keyReport.report_b1+keyReport.report_b2);
}
/* ------------------------------------------------------------------------- */
/* ----------------------------- USB interface ----------------------------- */
/* ------------------------------------------------------------------------- */

static uchar reportBuffer[2]; /* buffer for HID reports */
static uchar idleRate; /* in 4 ms units */

PROGMEM char usbHidReportDescriptor[27] = { /* USB report descriptor */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x05, // USAGE (Game Pad)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x0b, // USAGE_MAXIMUM (Button 11)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x0b, // REPORT_COUNT (11)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0xc0 // END_COLLECTION

};


static void buildReport()
{
reportBuffer[0] = keyReport.report_b1;
reportBuffer[1] = keyReport.report_b2;
}

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

usbMsgPtr = reportBuffer;
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* we only have one report type, so don't look at wValue */
buildReport();
return sizeof(reportBuffer);
}else if(rq->bRequest == USBRQ_HID_GET_IDLE){
usbMsgPtr = &idleRate;
return 1;
}else if(rq->bRequest == USBRQ_HID_SET_IDLE){
idleRate = rq->wValue.bytes[1];
}
}else{
/* no vendor specific requests implemented */
}
return 0;
}

/* ------------------------------------------------------------------------- */

int main(void)
{
unsigned int key, lastKey = 0, keyDidChange = 0;
uchar idleCounter = 0;

wdt_enable(WDTO_2S);
hardwareInit();
ADC_init();
odDebugInit();
usbInit();
sei();
DBG1(0x00, 0, 0);
for(;;){ /* main event loop */
wdt_reset();
usbPoll();
key = keyPressed();
if(lastKey != key){
lastKey = key;
keyDidChange = 1;
}
if(TIFR & (1<<TOV0)){ /* 22 ms timer */
TIFR = 1<<TOV0;
if(idleRate != 0){
if(idleCounter > 4){
idleCounter -= 5; /* 22 ms in units of 4 ms */
}else{
idleCounter = idleRate;
keyDidChange = 1;
}
}
}
if(keyDidChange && usbInterruptIsReady()){
keyDidChange = 0;
/* use last key and not current key status in order to avoid lost
changes in key status. */
buildReport();
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
}
}
return 0;
}


/* ------------------------------------------------------------------------- */

Loading

0 comments on commit f721f85

Please sign in to comment.