avr-something

Something something AVR side…
git clone git://henryandlizzy.uk/avr-something
Log | Files | Refs | README

hidModule.cpp (1282B)


      1 #include "hidModule.h"
      2 #include "ctrl.h"
      3 #include <util/delay.h>
      4 
      5 void hid::init()
      6 {
      7 	io::direction::B() = ledRED;
      8 	
      9 	io::out::D() = wheelCOM | buttonCOM;
     10 	io::direction::D() = wheelCOM | buttonCOM | ledYEL | ledGRN;
     11 }
     12 
     13 uint8_t hid::sampleInput()
     14 {
     15 	constexpr uint8_t maskC = bits(2,3,4,5);
     16 	constexpr uint8_t maskD = wheelCOM | buttonCOM;
     17 	
     18 	const auto saveCdir = io::direction::C();
     19 	io::direction::C() = saveCdir & ~maskC;
     20 	
     21 	const auto saveCout = io::out::C();
     22 	io::out::C() = saveCout | maskC;
     23 	io::out::D() = mix(io::out::D(), wheelCOM, maskD);
     24 
     25 	_delay_us(1);
     26 	auto buttons = (maskC & ~io::pin::C()) << 1;
     27 	io::pin::D() = maskD;
     28 	
     29 	_delay_us(1);
     30 	buttons |= (maskC & ~io::pin::C()) >> 2;
     31 	
     32 	io::out::C() = saveCout;
     33 	io::direction::C() = saveCdir;
     34 	
     35 	return buttons;
     36 }
     37 
     38 void hid::setLeds(const uint8_t val)
     39 {
     40 	io::out::B() = mix(io::out::B(), val, ledRED);
     41 	io::out::D() = mix(io::out::D(), val, ledYEL | ledGRN);
     42 }
     43 
     44 void hid::test()
     45 {
     46 	ctrl::disableJTAG();
     47 	
     48 	init();
     49 	
     50 	for (;;)
     51 	{
     52 		auto val = sampleInput();
     53 		
     54 		const auto r = (val & (buttonL | wheelA)) ? ledRED : 0;
     55 		const auto y = (val & (buttonD | buttonU)) ? ledYEL : 0;
     56 		const auto g = (val & (buttonR | wheelB)) ? ledGRN : 0;
     57 		
     58 		const auto leds = r | y | g;
     59 		
     60 		setLeds((val & buttonC) ? ~leds : leds);
     61 	}
     62 }