Skip to content

Commit

Permalink
renamed classes, added event class
Browse files Browse the repository at this point in the history
  • Loading branch information
adams1mon committed Sep 18, 2022
1 parent b89a9c9 commit d8a29e2
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 57 deletions.
29 changes: 29 additions & 0 deletions include/event.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef EVENT_H_
#define EVENT_H_

#include <stdint.h>

#define MAX_CALLBACKS_PER_EVENT 16

// the function type to which the callback addresses are converted
#define CALLBACK_TYPE void(*)(void)

class Event
{

public:
Event();
~Event();
uint8_t addListener(uint32_t callback);
void removeListeners();
void emit();

private:
uint32_t _callbacks[MAX_CALLBACKS_PER_EVENT];
uint8_t _endIndex;

void _clearAllCallbacks();
void _clearCallbacks();
};

#endif /* EVENT_H_ */
10 changes: 1 addition & 9 deletions include/gpio.h → include/gpio.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
/*
* gpio.h
*
* Created on: Jul 10, 2022
* Author: Adam Simon
*/

#ifndef GPIO_H_
#define GPIO_H_

#include "stm32f103x6.h"
#include "core_cm3.h"

#include "nvic_interrupts.h"

#include "nvic_interrupts.hpp"

enum class GpioOutputSpeed:uint8_t { LOW = 0x02, MEDIUM = 0x01, HIGH = 0x03 };
enum class GpioInputMode:uint8_t { ANALOG = 0x00, FLOATING = 0x04, PUPD = 0x08 };
Expand Down
7 changes: 0 additions & 7 deletions include/nvic_interrupts.h → include/nvic_interrupts.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*
* interrupts.h
*
* Created on: Jul 17, 2022
* Author: Adam Simon
*/

#ifndef NVIC_INTERRUPTS_H_
#define NVIC_INTERRUPTS_H_

Expand Down
11 changes: 1 addition & 10 deletions include/rtc.h → include/rtc.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
/*
* Timers on STM32F103C8T6
*
* timer.h
*
* Created on: Aug 14, 2022
* Author: Adam Simon
*/

#ifndef RTCHEADER_H_
#define RTCHEADER_H_

#include "stm32f103x6.h"
#include "nvic_interrupts.h"
#include "nvic_interrupts.hpp"

// RTC final frequency = Input Clock Frequency / (PRESCALER + 1)

Expand Down
11 changes: 1 addition & 10 deletions include/timer.h → include/timer.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
/*
* Timers on STM32F103C8T6
*
* timer.h
*
* Created on: Aug 7, 2022
* Author: Adam Simon
*/

#ifndef TIMERHEADER_H_
#define TIMERHEADER_H_

#include "stm32f103x6.h"
#include "nvic_interrupts.h"
#include "nvic_interrupts.hpp"

// all timers are configured as general purpose, no advanced feature timers
// all timers are configured in edge-aligned up-counting mode (timers count from 0 to the auto-reload value)
Expand Down
54 changes: 54 additions & 0 deletions src/event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "event.hpp"

Event::Event()
{
_clearAllCallbacks();
}

Event::~Event() {
_clearCallbacks();
}

uint8_t Event::addListener(uint32_t callback)
{
if (_endIndex < MAX_CALLBACKS_PER_EVENT)
{
uint8_t callbackInd = _endIndex;
_callbacks[_endIndex++] = callback;
return callbackInd;
}
// error
return MAX_CALLBACKS_PER_EVENT;
}

void Event::removeListeners()
{
_clearCallbacks();
}

void Event::emit()
{
for (uint8_t i = 0; i < _endIndex; ++i)
{
// convert the function address to a callable void function and call it
((CALLBACK_TYPE)_callbacks[i])();
}
}

void Event::_clearAllCallbacks()
{
for (uint8_t i = 0; i < MAX_CALLBACKS_PER_EVENT; ++i)
{
_callbacks[i] = 0x00000000;
}
_endIndex = 0;
}

void Event::_clearCallbacks()
{
for (uint8_t i = 0; i < _endIndex; ++i)
{
_callbacks[i] = 0x00000000;
}
_endIndex = 0;
}
2 changes: 1 addition & 1 deletion src/gpio.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "gpio.h"
#include "gpio.hpp"

GpioPin::GpioPin(GPIO_TypeDef* port, int pin)
{
Expand Down
42 changes: 25 additions & 17 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#include "core_cm3.h"


#include "gpio.h"
#include "timer.h"
#include "rtc.h"
#include "gpio.hpp"
#include "timer.hpp"
#include "rtc.hpp"
#include "event.hpp"


#define LEDPORT (GPIOC)
Expand All @@ -22,19 +23,6 @@ void ms_delay(int ms)
}
}

// //Alternates blue and green LEDs quickly
// int main(void)
// {
// ENABLE_GPIO_CLOCK; // enable the clock to GPIO
// LEDPORT->_MODER |= GPIOMODER; // set pins to be general purpose output
// for (;;) {
// ms_delay(500);
// LEDPORT->ODR ^= (1<<LED1); // toggle diodes
// }

// return 0;
// }

Rtc rtc;

Timer timer_2(TIM2);
Expand Down Expand Up @@ -68,11 +56,27 @@ void toggle_led_with_timer() {
}

void toggle_led_with_rtc() {
toggle_led();
ms_delay(2000);
toggle_led();
rtc.clearInterruptPending();
rtc.clearNvicInterruptPending();
}

void toggle_led_with_rtc_0() {
toggle_led();
ms_delay(200);
toggle_led();
ms_delay(500);
}


Event rtcEvent;

void emitRtcEvent() {
rtcEvent.emit();
}


int main() {

Expand All @@ -92,7 +96,11 @@ int main() {
// timer_2.enableInterrupt();
// timer_2.start();

rtc.enableNvicInterrupt(NvicInterruptPriority::HIGH, (uint32_t) toggle_led_with_rtc);
rtcEvent.addListener((uint32_t) (toggle_led_with_rtc_0));
rtcEvent.addListener((uint32_t) (toggle_led_with_rtc));

// rtc.enableNvicInterrupt(NvicInterruptPriority::HIGH, (uint32_t) toggle_led_with_rtc);
rtc.enableNvicInterrupt(NvicInterruptPriority::HIGH, (uint32_t) emitRtcEvent);
rtc.setupAndStartWithNVICInterrupt();


Expand Down
3 changes: 1 addition & 2 deletions src/rtc.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

#include "rtc.h"
#include "rtc.hpp"

Rtc::Rtc(uint32_t prescaler)
{
Expand Down
2 changes: 1 addition & 1 deletion src/timer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "timer.h"
#include "timer.hpp"

Timer::Timer(TIM_TypeDef* timer, uint16_t counter, uint16_t prescaler, uint16_t autoReloadValue)
{
Expand Down

0 comments on commit d8a29e2

Please sign in to comment.