This commit is contained in:
DashyFox 2024-08-26 00:11:00 +03:00
parent 45771f8f5f
commit 3dd1f7828b
3 changed files with 62 additions and 53 deletions

View File

@ -1,5 +1,6 @@
{
"port": "COM3",
"board": "arduino:avr:uno",
"sketch": "MusicRing.ino"
"board": "STMicroelectronics:stm32:GenF4",
"sketch": "MusicRing.ino",
"configuration": "pnum=BLACKPILL_F411CE,upload_method=swdMethod,xserial=generic,usb=CDCgen,xusb=FS,opt=osstd,dbg=none,rtlib=nano"
}

View File

@ -5,35 +5,30 @@
#include "./implement/Display__Adafruit_SSD1306.h"
#include "./misc/bitmaps.h"
LED_Ring_PINOUT pinout { 5, 6, 7, 8, 9 };
LED_Ring_PINOUT pinout { PA4, PA3, PA5, PA1, PA2 };
LED_Ring ring(32, pinout);
Screen screen(128, 32, Display::Ori_HORIZONTAL);
void ISR() { ring.isr(); }
void setup() {
Serial.begin(115200);
ring.begin();
screen.begin();
ring.begin(ISR);
screen.adafruit->drawBitmap(0, 0, DashyFox_logo, 128, 32, WHITE);
screen.adafruit->display();
}
ISR(TIMER1_COMPA_vect) { ring.isr(); }
void loop() {
static uint32_t tmr;
static uint8_t arr[6] = { 0x3f >> 1, 0xf0 ,0x7f >> 3, 0xf8, 0x3f >> 1, 0xf0 };
static uint8_t arr2[6] = { 0 };
static bool f = false;
if (millis() - tmr > 350) {
screen.adafruit->drawBitmap(105, 23, arr, 16, 3, f);
screen.adafruit->display();
tmr = millis();
f = !f;
if (Serial.available())
{
uint8_t input = Serial.parseInt();
ring.setFrec(input);
}
}

View File

@ -3,15 +3,17 @@
struct LED_Ring_PINOUT {
uint8_t
Data,
CLK_inside,
CLK_outside,
RESET,
OutputEnable;
Data, // yellow
CLK_inside, // red
CLK_outside, // orange
RESET, // green
OutputEnable; // pur
};
class LED_Ring {
private:
HardwareTimer *MyTim = nullptr;
LED_Ring_PINOUT pinOut;
uint16_t ledCount;
uint16_t updateFrec;
@ -35,7 +37,7 @@ public:
};
void begin() {
void begin(void(*func)(void)) {
reset();
@ -58,7 +60,7 @@ public:
delay(del);
}
timerIni(frec);
timerIni(frec, func);
}
///////////////////////////////////////////////////////////////////////////////////////
@ -96,7 +98,7 @@ public:
//////////////////////////////////////////////////////////////////////////////
void isr() {
if (position >= ledCount) {
if (position > ledCount) {
point_ini(1);
position = 1;
} else shift(1);
@ -104,36 +106,47 @@ public:
//////////////////////////////////////////////////////////////////////////////
void timerIni(uint16_t _frec) { //TODO: Сделать возможность выбора таймеров
period = F_CPU / 8 / ledCount / _frec;
// инициализация Timer1
cli(); // отключить глобальные прерывания
TCCR1A = 0; // установить регистры в 0
TCCR1B = 0;
void timerIni(uint16_t _frec, void(*func)(void)) {
// Определяем таймер, например, используем TIM2
MyTim = new HardwareTimer(TIM2);
OCR1A = period; // установка регистра совпадения
TCCR1B |= (1 << WGM12); // включение в CTC режим
// Рассчитываем период
uint32_t prescaler = 8; // Делитель
period = (SystemCoreClock / (prescaler * _frec)) - 1;
// Установка битов CS на коэффициент деления 8
TCCR1B |= 0b00000010;
// Останавливаем таймер перед настройкой
MyTim->pause();
TIMSK1 |= (1 << OCIE1A); // включение прерываний по совпадению
sei(); // включить глобальные прерывания
// Настраиваем предделитель (делитель частоты)
MyTim->setPrescaleFactor(prescaler);
// Настраиваем таймер в режиме CTC (count-to-clear)
MyTim->setOverflow(period);
MyTim->attachInterrupt(func);
MyTim->resume();
}
void setFrec(uint8_t frec) {
period = F_CPU / 8 / ledCount / frec;
cli();
OCR1A = period; // установка регистра совпадения
sei();
void setFrec(uint16_t frec) {
uint32_t prescaler = 8; // Делитель
period = (SystemCoreClock / (prescaler * frec)) - 1;
// Останавливаем таймер перед изменением периода
MyTim->pause();
MyTim->setOverflow(period);
MyTim->refresh(); // Обновление настроек таймера
MyTim->resume();
}
void setPeriod(uint8_t per) {
void setPeriod(uint32_t per) {
period = per;
cli();
OCR1A = period; // установка регистра совпадения
sei();
// Останавливаем таймер перед изменением периода
MyTim->pause();
MyTim->setOverflow(period);
MyTim->refresh(); // Обновление настроек таймера
MyTim->resume();
}
/////////////////////////////////////////////////////////////////
};