Permalink
Browse files

Add LED: definition

  • Loading branch information...
1 parent 19b722e commit 7a08511c0013ab8a190a937e7dda111ae3273b0a @humblehacker committed Aug 6, 2010
Showing with 174 additions and 48 deletions.
  1. +3 −2 hid_usages.cc
  2. +9 −3 keyboard.h
  3. +31 −10 kspec.atg
  4. +35 −0 lua_keyboard_visitor.cc
  5. +6 −1 main.cc
  6. +86 −28 templates/LEDs.elu.h
  7. +4 −4 test.kspec
View
@@ -1,4 +1,5 @@
#include <iostream>
+#include <exception>
#include "hid_usages.h"
using std::wcout;
@@ -26,7 +27,7 @@ lookup(const std::wstring &key, const std::wstring &page)
else if (page == L"Consumer_Control")
usages = &consumer_control_usages;
else
- throw "Unknown usage page";
+ return (*usages)[1]; // Unknown usage page;
int index = 0;
Usage *usage;
@@ -36,7 +37,7 @@ lookup(const std::wstring &key, const std::wstring &page)
return *usage;
++index;
}
- throw "Usage not found";
+ return (*usages)[0]; // Usage not found
}
//UsagePage:Keyboard_and_Keypad id:0x07
View
@@ -82,16 +82,21 @@ typedef std::map<wstring, KeyMap::Ptr> KeyMaps;
class LED
{
public:
- LED(const wstring &ident) : _name(ident), _std(none) {}
-
+ enum Standard { numlock, capslock, scrolllock, compose, kana, none };
typedef boost::shared_ptr<LED> Ptr;
enum FlowDir { source, sink };
- enum Standard { numlock, capslock, scrolllock, compose, kana, none };
+
+ LED(const wstring &ident, Standard std=none) : _name(ident), _std(std) {}
void set_pin(const wstring &pin) { _pin = pin; }
void set_flow(FlowDir flow) { _flow = flow; }
void set_std(Standard std) { _std = std; }
+ const wstring &name() const { return _name; }
+ const wstring &pin() const { return _pin; }
+ FlowDir flow() const { return _flow; }
+ Standard std() const { return _std; }
+
private:
wstring _name;
wstring _pin;
@@ -122,6 +127,7 @@ class Keyboard
const IOPins &col_pins() const { return _cpins; }
const IOPins &row_pins() const { return _rpins; }
const KeyMaps &maps() const { return _maps; }
+ const LEDs &leds() const { return _leds; }
bool block_ghost_keys() const { return _block_ghost_keys; }
void accept(KeyboardVisitor &visitor) const;
View
@@ -1,4 +1,5 @@
#include <iostream>
+#include <sstream>
#include "keyboard.h"
#include "hid_usages.h"
#include "utils.h"
@@ -7,6 +8,21 @@ COMPILER kspec
hh::Keyboard::Ptr kb;
+ const hh::Usage &lookup_usage(const std::wstring &id, const std::wstring &page)
+ {
+ const hh::Usage &usage = hh::Usage::lookup(id, page);
+ if (usage.id < 2)
+ {
+ std::wstringstream str;
+ if (usage.id == 0)
+ str << "Unknown usage '" << id << "'";
+ else
+ str << "Unknown usage page '" << page << "'";
+ SemErr(str.str().c_str());
+ }
+ return usage;
+ }
+
CHARACTERS
letter = 'A' .. 'Z' + 'a' .. 'z'.
digit = '0' .. '9'.
@@ -87,19 +103,24 @@ PRODUCTIONS
/* ----------------------------------------------------------------------------- */
Led =
- "LED:" ident (. hh::LED::Ptr led(new hh::LED(t->val)); .)
+ "LED:" (. hh::LED::Ptr led; .)
+ ("num_lock" (. led.reset(new hh::LED(t->val,
+ hh::LED::numlock)); .)
+ |"caps_lock" (. led.reset(new hh::LED(t->val,
+ hh::LED::capslock)); .)
+ |"scroll_lock" (. led.reset(new hh::LED(t->val,
+ hh::LED::scrolllock)); .)
+ |"compose" (. led.reset(new hh::LED(t->val,
+ hh::LED::compose)); .)
+ |"kana" (. led.reset(new hh::LED(t->val,
+ hh::LED::kana)); .)
+ | ident (. led.reset(new hh::LED(t->val)); .)
+ )
"pin:" ident (. led->set_pin(t->val); .)
"flow:"
("source" (. led->set_flow(hh::LED::source); .)
|"sink" (. led->set_flow(hh::LED::sink); .)
- )
- ["std:"
- ("num_lock" (. led->set_std(hh::LED::numlock); .)
- |"caps_lock" (. led->set_std(hh::LED::capslock); .)
- |"scroll_lock" (. led->set_std(hh::LED::scrolllock); .)
- |"compose" (. led->set_std(hh::LED::compose); .)
- |"kana" (. led->set_std(hh::LED::kana); .)
- )] (. kb->add_led(led); .)
+ ) (. kb->add_led(led); .)
.
/* ----------------------------------------------------------------------------- */
@@ -144,7 +165,7 @@ PRODUCTIONS
[Mods<mods>]
[
"page:" ident (. page = t->val; .)
- ] (. const hh::Usage &usage = hh::Usage::lookup(id, page);
+ ] (. const hh::Usage &usage = lookup_usage(id, page);
map.reset(new hh::Map(usage, mods)); .)
{
Label<loc, label> (. map->add_label(loc, label); .)
View
@@ -45,6 +45,41 @@ visit(const hh::Keyboard &kb)
lua_setfield(_L, -2, "rows");
lua_setfield(_L, -2, "matrix");
+ // kb.leds = { led1, led2 }
+ lua_newtable(_L);
+ int index = 1;
+ foreach(const LED::Ptr &led, kb.leds())
+ {
+ lua_pushnumber(_L, index++);
+ lua_newtable(_L);
+ set_field(_L, "name", led->name());
+ set_field(_L, "pin", led->pin());
+ if (led->flow() == hh::LED::source)
+ set_field(_L, "flow", "source");
+ else
+ set_field(_L, "flow", "sink");
+ switch(led->std())
+ {
+ case hh::LED::numlock:
+ set_field(_L, "std", "num_lock");
+ break;
+ case hh::LED::capslock:
+ set_field(_L, "std", "caps_lock");
+ break;
+ case hh::LED::scrolllock:
+ set_field(_L, "std", "scroll_lock");
+ break;
+ case hh::LED::compose:
+ set_field(_L, "std", "compose");
+ break;
+ case hh::LED::kana:
+ set_field(_L, "std", "kana");
+ break;
+ }
+ lua_settable(_L, -3);
+ }
+ lua_setfield(_L, -2, "leds");
+
// kb.block_ghost_keys = <boolean>
set_field(_L, "block_ghost_keys", kb.block_ghost_keys());
View
@@ -320,6 +320,11 @@ generate(const fs::path &template_filename, const fs::path &output_dir, lua_Stat
if (options.count("debug"))
{
+ cout << endl;
+ cout << "====================================================================" << endl;
+ cout << template_filename.string() << endl;
+ cout << "====================================================================" << endl;
+ cout << endl;
int linenum = 1;
while (getline(out, line))
{
@@ -360,4 +365,4 @@ double __strtod( const char *str, char ** endptr )
return strtod(str, endptr);
}
}
-#endif
+#endif
View
@@ -50,69 +50,127 @@
#error Do not include this file directly. Include LUFA/Drivers/Board/LEDS.h instead.
#endif
- /* Public Interface - May be used in end-application: */
- /* Macros: */
- /** LED mask for the first LED on the board. */
- #define LEDS_LED1 (1<<0)
-
- /** LED mask for the second LED on the board. */
- #define LEDS_LED2 (1<<1)
-
- /** LED mask for the third LED on the board. */
- #define LEDS_LED3 (1<<2)
-
- /** LED mask for the fourth LED on the board. */
- #define LEDS_LED4 (1<<3)
+ /* Public Interface - May be used in end-application: */
+ /* Macros: */
+
+ /** LED masks. */
+<%
+ stdleds = {}
+ otherleds = {}
+ for i,led in ipairs(kb.leds) do
+ if led.std == nil then
+ otherleds[#otherleds+1] = led
+ end
+ end
+%>
+ enum LEDs {
+ LED_NUM_LOCK = (1<<0),
+ LED_CAPS_LOCK = (1<<1),
+ LED_SCROLL_LOCK = (1<<2),
+ LED_COMPOSE = (1<<3),
+ LED_KANA = (1<<4),
+<% for i,led in ipairs(otherleds) do %>
+ LED_<%=string.upper(led.name)%> = (1<<<%=i+4%>),
+<% end %>
+ };
/** LED mask for all the LEDs on the board. */
- #define LEDS_ALL_LEDS (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4)
+ #define LEDS_ALL_LEDS (0<%
+ for i,led in ipairs(kb.leds) do
+ %> | LED_<%=string.upper(led.name)%><%
+ end%>)
/** LED mask for the none of the board LEDs */
#define LEDS_NO_LEDS 0
- /** Keyboard specific LED mask names */
- #define LED_NUM LEDS_LED1
- #define LED_CAPS LEDS_LED2
- #define LED_SCRL LEDS_LED3
-
/* Inline Functions: */
#if !defined(__DOXYGEN__)
static inline void LEDs_Init(void)
{
- // Keyboard LEDs are F0-F3
- DDRF |= LEDS_ALL_LEDS; // set pins as outputs
- PORTF |= LEDS_ALL_LEDS; // set pins high to prevent sinking (lights off)
+ <% for i,led in ipairs(kb.leds) do
+ port = string.upper(string.sub(led.pin,2,2))
+ pin = string.sub(led.pin,3,3)
+ name = 'LED_' .. string.upper(led.name) %>
+ // <%=name%>
+ DDR<%=port%> |= (1<<<%=pin%>); // set pin as output
+<% if led.flow == 'sink' then %>
+ PORT<%=port%> |= (1<<<%=pin%>); // set pin high to prevent sinking (led off)
+<% else %>
+ PORT<%=port%> &= ~(1<<<%=pin%>); // set pin low (led off)
+<% end
+ end %>
}
static inline void LEDs_TurnOnLEDs(const uint8_t LEDMask)
{
- PORTF &= ~LEDMask;
+<% for i,led in ipairs(kb.leds) do
+ port = "PORT_" .. string.upper(string.sub(led.pin,2,2))
+ pin = string.sub(led.pin,3,3)
+ name = 'LED_' .. string.upper(led.name) %>
+ if (LEDMask & <%=name%>)
+<% if led.flow == 'sink' then %>
+ <%=port%> &= ~(1<<<%=pin%>);
+<% else %>
+ <%=port%> |= (1<<<%=pin%>);
+<% end
+ end %>
}
static inline void LEDs_TurnOffLEDs(const uint8_t LEDMask)
{
- PORTF |= LEDMask;
+<% for i,led in ipairs(kb.leds) do
+ print(led.flow)
+ port = "PORT_" .. string.upper(string.sub(led.pin,2,2))
+ pin = string.sub(led.pin,3,3)
+ name = 'LED_' .. string.upper(led.name) %>
+ if (LEDMask & <%=name%>)
+<% if led.flow == 'sink' then %>
+ <%=port%> |= (1<<<%=pin%>);
+<% else %>
+ <%=port%> &= ~(1<<<%=pin%>);
+<% end
+ end %>
}
static inline void LEDs_SetAllLEDs(const uint8_t LEDMask)
{
- PORTF = ((PORTF | LEDS_ALL_LEDS) & ~LEDMask);
+ LEDs_TurnOffLEDs(LEDS_ALL_LEDS);
+ LEDs_TurnOnLEDs(LEDMask);
}
static inline void LEDs_ChangeLEDs(const uint8_t LEDMask, const uint8_t ActiveMask)
{
- PORTF = ((PORTF | LEDMask) & ~ActiveMask);
+ LEDs_TurnOffLEDs(LEDMask);
+ LEDs_TurnOnLEDs(ActiveMask);
}
static inline void LEDs_ToggleLEDs(const uint8_t LEDMask)
{
- PORTF = (PORTF ^ (LEDMask & LEDS_ALL_LEDS));
+<% for i,led in ipairs(kb.leds) do
+ port = "PORT_" .. string.upper(string.sub(led.pin,2,2))
+ pin = string.sub(led.pin,3,3)
+ name = 'LED_' .. string.upper(led.name) %>
+ if (LEDMask & <%=name%>)
+ <%=port%> ^= ~(1<<<%=pin%>);
+<% end %>
}
static inline uint8_t LEDs_GetLEDs(void) ATTR_WARN_UNUSED_RESULT;
static inline uint8_t LEDs_GetLEDs(void)
{
- return ~(PORTF & LEDS_ALL_LEDS);
+ uint8_t result = 0;
+<% for i,led in ipairs(kb.leds) do
+ port = "PORT_" .. string.upper(string.sub(led.pin,2,2))
+ pin = string.sub(led.pin,3,3)
+ name = 'LED_' .. string.upper(led.name) %>
+<% if led.flow == 'sink' then %>
+ if ((<%=port%> & ~(1<<<%=pin%>)) == <%=port%>)
+<% else %>
+ if ((<%=port%> & (1<<<%=pin%>)) == <%=port%>)
+<% end %>
+ result |= <%=name%>;
+<% end %>
+ return result;
}
#endif
View
@@ -13,10 +13,10 @@ Matrix:
ColPins: PC0 PC1 PC2 PC3 PC4 PC5 PC6 PC7 PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0
RowPins: PA0 PA1 PA2 PA3 PA4 PA5 PA6
- LED:num pin:PF0 flow:source std:num_lock
- LED:cap pin:PF1 flow:source std:caps_lock
- LED:scl pin:PF2 flow:source std:scroll_lock
- LED:dv pin:PF3 flow:source
+ LED:num_lock pin:PF0 flow:source
+ LED:caps_lock pin:PF1 flow:sink
+ LED:scroll_lock pin:PF2 flow:source
+ LED:dv pin:PF3 flow:sink
BlockGhostKeys:yes

0 comments on commit 7a08511

Please sign in to comment.