Skip to content

Commit

Permalink
Merge pull request #147 from shinhub/led_simplify
Browse files Browse the repository at this point in the history
Led simplify
  • Loading branch information
securechicken authored Sep 14, 2019
2 parents c25ffde + 9f824a6 commit b0ccfb0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 80 deletions.
1 change: 0 additions & 1 deletion Firmware/ChameleonMini/Application/Detection.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "ISO14443-3A.h"
#include "../Codec/ISO14443-2A.h"
#include "../Memory/Memory.h"
#include "../LED.h"
#include "../Random.h"
#include "../Settings.h"
#include <string.h>
Expand Down
84 changes: 37 additions & 47 deletions Firmware/ChameleonMini/LED.c
Original file line number Diff line number Diff line change
@@ -1,61 +1,51 @@
#include <stdbool.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "LED.h"

uint8_t LEDLowPulseMask = 0, LEDHighPulseMask = 0;

inline
void LEDMode(void) {
LEDHighSetOff(LED_ONE);
LEDHighSetOff(LED_TWO);
LEDHighSetOff(LED_THREE);
LEDHighSetOff(LED_FOUR);
LEDLowSetOff(LED_FIVE);
LEDLowSetOff(LED_SIX);
LEDLowSetOff(LED_SEVEN);
LEDLowSetOff(LED_EIGHT);

switch(GlobalSettings.ActiveSetting) {
case 0: LEDHighSetOn(LED_ONE); break;
case 1: LEDHighSetOn(LED_TWO); break;
case 2: LEDHighSetOn(LED_THREE); break;
case 3: LEDHighSetOn(LED_FOUR); break;
case 4: LEDLowSetOn(LED_FIVE); break;
case 5: LEDLowSetOn(LED_SIX); break;
case 6: LEDLowSetOn(LED_SEVEN); break;
case 7: LEDLowSetOn(LED_EIGHT); break;
default: break;
}
#include "Settings.h"
#include "Common.h"

static const LedPining_t LEDPiningTable[] PROGMEM = {
[LED_ONE] = { .ledPort = &LED_HIGH_PORT, .ledPin = PIN5_bm },
[LED_TWO] = { .ledPort = &LED_HIGH_PORT, .ledPin = PIN4_bm },
[LED_THREE] = { .ledPort = &LED_HIGH_PORT, .ledPin = PIN3_bm },
[LED_FOUR] = { .ledPort = &LED_HIGH_PORT, .ledPin = PIN2_bm },
[LED_FIVE] = { .ledPort = &LED_LOW_PORT, .ledPin = PIN3_bm },
[LED_SIX] = { .ledPort = &LED_LOW_PORT, .ledPin = PIN2_bm },
[LED_SEVEN] = { .ledPort = &LED_LOW_PORT, .ledPin = PIN1_bm },
[LED_EIGHT] = { .ledPort = &LED_LOW_PORT, .ledPin = PIN0_bm }
};

INLINE void LEDAllOff(void) {
LED_HIGH_PORT.OUTCLR = LED_HIGH_MASK;
LED_LOW_PORT.OUTCLR = LED_LOW_MASK;
}

void LEDInit(void) {
LED_LOW_PORT.DIRSET = LED_LOW_MASK;
LED_HIGH_PORT.DIRSET = LED_HIGH_MASK;
INLINE void LEDMode(void) {
LEDAllOff();
LEDSetOn(GlobalSettings.ActiveSetting);
}

inline
void LEDTick(void) {
LED_HIGH_PORT.OUTCLR = LEDLowPulseMask;
LEDLowPulseMask = 0;
LED_LOW_PORT.OUTCLR = LEDHighPulseMask;
LEDHighPulseMask = 0;
LEDMode();
LED_HIGH_PORT.OUTCLR = LED_LOW_PULSE_MASK;
LED_LOW_PORT.OUTCLR = LED_HIGH_PULSE_MASK;
LEDMode();
}

inline
void LEDHighSetOn(uint8_t Mask) {
LED_HIGH_PORT.OUTSET = Mask;
void LEDInit(void) {
LED_LOW_PORT.DIRSET = LED_LOW_MASK;
LED_HIGH_PORT.DIRSET = LED_HIGH_MASK;
}

inline
void LEDLowSetOn(uint8_t Mask) {
LED_LOW_PORT.OUTSET = Mask;
INLINE void LEDSet(Led LedId, bool on) {
PORT_t * ledPort = ( (PORT_t *)pgm_read_ptr( &(LEDPiningTable[LedId].ledPort) ) );
uint8_t ledPin = ( (uint8_t)pgm_read_byte( &(LEDPiningTable[LedId].ledPin) ) );
(on) ? (ledPort->OUTSET = ledPin) : (ledPort->OUTCLR = ledPin);
}

inline
void LEDHighSetOff(uint8_t Mask) {
LED_HIGH_PORT.OUTCLR = Mask;
void LEDSetOn(Led LedId) {
LEDSet(LedId, true);
}

inline
void LEDLowSetOff(uint8_t Mask) {
LED_LOW_PORT.OUTCLR = Mask;
void LEDSetOff(Led LedId) {
LEDSet(LedId, false);
}
49 changes: 26 additions & 23 deletions Firmware/ChameleonMini/LED.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,37 @@
* Author: skuser
*/

#ifndef LED_H
#define LED_H
#ifndef _CM_LED_H_
#define _CM_LED_H_

#include "Common.h"
#include "Settings.h"
#include <avr/io.h>

#define LED_HIGH_PORT PORTA
#define LED_ONE PIN5_bm
#define LED_TWO PIN4_bm
#define LED_THREE PIN3_bm
#define LED_FOUR PIN2_bm
#define LED_HIGH_MASK (LED_ONE | LED_TWO | LED_THREE| LED_FOUR)
#define LED_HIGH_PORT PORTA
#define LED_HIGH_MASK (PIN5_bm | PIN4_bm | PIN3_bm | PIN2_bm)
#define LED_LOW_PORT PORTE
#define LED_LOW_MASK (PIN3_bm | PIN2_bm | PIN1_bm | PIN0_bm)
#define LED_LOW_PULSE_MASK 0
#define LED_HIGH_PULSE_MASK 0

#define LED_LOW_PORT PORTE
#define LED_FIVE PIN3_bm
#define LED_SIX PIN2_bm
#define LED_SEVEN PIN1_bm
#define LED_EIGHT PIN0_bm
#define LED_LOW_MASK (LED_FIVE | LED_SIX | LED_SEVEN| LED_EIGHT)
typedef enum {
LED_ONE,
LED_TWO,
LED_THREE,
LED_FOUR,
LED_FIVE,
LED_SIX,
LED_SEVEN,
LED_EIGHT
} Led;

typedef struct {
PORT_t * ledPort;
uint8_t ledPin;
} LedPining_t;

void LEDMode(void);
void LEDInit(void);
void LEDTick(void);
void CARDInit(void);
void LEDHighSetOn(uint8_t Mask);
void LEDLowSetOn(uint8_t Mask);
void LEDHighSetOff(uint8_t Mask);
void LEDLowSetOff(uint8_t Mask);
void LEDSetOn(Led LedId);
void LEDSetOff(Led LedId);

#endif /* LED_H */
#endif /* _CM_LED_H_ */
1 change: 0 additions & 1 deletion Firmware/ChameleonMini/Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <avr/eeprom.h>
#include "Configuration.h"
#include <string.h>
#include "LED.h"
#include "Memory/Memory.h"
#include "Terminal/CommandLine.h"
#include "System.h"
Expand Down
1 change: 0 additions & 1 deletion Firmware/ChameleonMini/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "Button.h"
#include "Configuration.h"
#include "Memory/Memory.h"

#define SETTINGS_COUNT 8
#define SETTINGS_FIRST 0
Expand Down
2 changes: 0 additions & 2 deletions Firmware/ChameleonMini/System.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include "System.h"
#include "LED.h"

ISR(BADISR_vect)
{
Expand Down
5 changes: 0 additions & 5 deletions Firmware/ChameleonMini/Terminal/Terminal.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "Terminal.h"
#include "../System.h"
#include "../LED.h"
#include "../LUFADescriptors.h"

#define INIT_DELAY (2000 / SYSTEM_TICK_MS)
Expand Down Expand Up @@ -56,8 +55,6 @@ static void ProcessByte(void) {

if (Byte >= 0) {
/* Byte received */
//LEDPulse(LED_RED);

if (XModemProcessByte(Byte)) {
/* XModem handled the byte */
} else if (CommandLineProcessByte(Byte)) {
Expand Down Expand Up @@ -123,12 +120,10 @@ void TerminalTick(void) {

/** Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void) {
//LEDSetOn(LED_GREEN);
}

/** Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void) {
//LEDSetOff(LED_GREEN);
}

/** Event handler for the library USB Configuration Changed event. */
Expand Down

0 comments on commit b0ccfb0

Please sign in to comment.