Skip to content

Commit

Permalink
Add LED support
Browse files Browse the repository at this point in the history
  • Loading branch information
kkonradpl committed Jul 13, 2024
1 parent f046cbb commit 3807329
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 3 deletions.
4 changes: 4 additions & 0 deletions FM-DX-Tuner.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "src/Controllers/Button/Button.hpp"
#include "src/Controllers/Rotator/Rotator.hpp"
#include "src/Controllers/Dispatcher/Dispatcher.hpp"
#include "src/Controllers/Led/Led.hpp"
#include "src/Controllers/Tuner/Tuner.hpp"
#include "src/Controllers/Tuner/SAF7730/SAF7730.hpp"
#include "src/Controllers/Tuner/TEF668X/TEF668X.hpp"
Expand Down Expand Up @@ -49,6 +50,9 @@ ANTENNA_DRIVER antenna(tuner, ANTENNA_PINS);
Controller *ctrls[] =
{
&dispatcher,
#if LED_ENABLED
&led,
#endif
#if BUTTON_ENABLED
&button,
#endif
Expand Down
19 changes: 16 additions & 3 deletions presets/tef-headless/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@
/* ----------------------------------------------------------------------- */


/* -------------------------------------------------------------------------
LED configuration
----------------------------------------------------------------------- */
#define LED_ENABLED 1

#define LED_COUNT 2
#define LED_PINS PA15, PA8

#define LED_ID_POWER 0
#define LED_ID_COMMAND 1
/* ----------------------------------------------------------------------- */


/* -----------------------------------------------------------------------
Antenna switch configuration
----------------------------------------------------------------------- */
Expand All @@ -54,9 +67,9 @@
/* -----------------------------------------------------------------------
Rotator controller configuration
----------------------------------------------------------------------- */
#define ROTATOR_ENABLED 0
#define ROTATOR_PIN_CW 6
#define ROTATOR_PIN_CCW 7
#define ROTATOR_ENABLED 1
#define ROTATOR_PIN_CW PB4
#define ROTATOR_PIN_CCW PB5

/* Automatic rotator stop after specified time [seconds] */
#define ROTATOR_TIMEOUT 90
Expand Down
6 changes: 6 additions & 0 deletions src/Controllers/Dispatcher/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <Arduino.h>
#include "Dispatcher.hpp"
#include "../../Controllers/Led/Led.hpp"
#include "../../Protocol.h"
#include "../../../Config.hpp"

Expand Down Expand Up @@ -105,6 +106,11 @@ Dispatcher::read()

buff[buff_pos] = '\0';
buff_pos = 0;

#if LED_ENABLED && defined(LED_ID_COMMAND)
led.blink(LED_ID_COMMAND, 50);
#endif

return true;
}

Expand Down
78 changes: 78 additions & 0 deletions src/Controllers/Led/Led.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
*
* FM-DX Tuner
* Copyright (C) 2024 Konrad Kosmatka
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

#include <Arduino.h>
#include "Led.hpp"

void
Led::setup()
{
for (uint8_t id = 0; id < LED_COUNT; id++)
{
pinMode(this->pins[id], OUTPUT);
digitalWrite(this->pins[id], LOW);
}
}

void
Led::hello()
{
}

void
Led::loop()
{
for (uint8_t id = 0; id < LED_COUNT; id++)
{
if (this->timers[id].process(Timer::Mode::Oneshot))
{
digitalWrite(this->pins[id], LOW);
}
}
}

const Command*
Led::getCommands(uint8_t *len)
{
*len = 0;
return NULL;
}

void
Led::on(uint8_t id)
{
digitalWrite(this->pins[id], HIGH);
this->timers[id].unset();
}

void
Led::off(uint8_t id)
{
digitalWrite(this->pins[id], LOW);
this->timers[id].unset();
}

void
Led::blink(uint8_t id,
Timer::Interval timeout)
{
digitalWrite(this->pins[id], HIGH);
this->timers[id].set(timeout);
}

#if LED_ENABLED
Led led{LED_PINS};
#endif
51 changes: 51 additions & 0 deletions src/Controllers/Led/Led.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
*
* FM-DX Tuner
* Copyright (C) 2024 Konrad Kosmatka
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

#ifndef FMDX_TUNER_LED_H
#define FMDX_TUNER_LED_H
#include <stdint.h>
#include "../Controller.hpp"
#include "../../Utils/Timer.hpp"
#include "../../../Config.hpp"

class Led : public Controller
{
public:
template <typename... Args>
Led(Args... args) : pins{ uint8_t(args)... }
{
static_assert(sizeof...(Args) == LED_COUNT, "Invalid count of pins");
}

void setup();
void hello();
void loop();
const Command* getCommands(uint8_t *len);

void on(uint8_t id);
void off(uint8_t id);
void blink(uint8_t id, Timer::Interval timeout);

private:
const uint8_t pins[LED_COUNT];
Timer timers[LED_COUNT];
};

#if LED_ENABLED
extern Led led;
#endif /* LED_ENABLED */

#endif /* FMDX_TUNER_LED_H */
9 changes: 9 additions & 0 deletions src/Controllers/Tuner/Tuner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "../../Utils/Utils.hpp"
#include "../../Protocol.h"
#include "../../../Config.hpp"
#include "../Led/Led.hpp"

void
Tuner::setup()
Expand Down Expand Up @@ -95,6 +96,10 @@ Tuner::start()
constexpr Timer::Interval squelchInterval = 50;
this->timerSquelch.set(squelchInterval);

#if LED_ENABLED && defined(LED_ID_POWER)
led.on(LED_ID_POWER);
#endif

return true;
}

Expand All @@ -113,6 +118,10 @@ Tuner::shutdown()
/* Release SDA and SCL lines used by hardware I2C */
TWCR = 0;
#endif

#if LED_ENABLED && defined(LED_ID_POWER)
led.off(LED_ID_POWER);
#endif
}

void
Expand Down

0 comments on commit 3807329

Please sign in to comment.