Skip to content

Commit

Permalink
add src/button.h and src/button.c
Browse files Browse the repository at this point in the history
- this relates to the issue #8
  • Loading branch information
IvanDyachenko committed Aug 19, 2019
1 parent 1d691cd commit a5dfcd7
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/button.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of the 'Yet another gauge' project.
*
* Copyright (C) 2018 Ivan Dyachenko <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>

#include <subject.h>

#include <libopencm3/stm32/gpio.h>

#include "button.h"

struct button_t {
uint16_t gpio_pin;
uint32_t gpio_port;

enum rcc_periph_clken clken;

subject_t *subject;
};

button_t *button_new(enum rcc_periph_clken clken, uint16_t gpio_pin, uint32_t gpio_port) {
// Enable the clock on particular peripheral
rcc_periph_clock_enable(clken);

// Enable the peripheral clock
gpio_mode_setup(gpio_port, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, gpio_pin);

button_t *button = malloc(sizeof(button_t));

button->gpio_pin = gpio_pin;
button->gpio_port = gpio_port;

button->clken = clken;

button->subject = subject_new();

return button;
}

void button_free(button_t *button) {
if (button != NULL) {
subject_delete(button->subject);

free(button);
}
}

void button_attach(button_t *button, const observer_t *observer) {
subject_attach(button->subject, observer);
}

void button_detach(button_t *button, const observer_t *observer) {
subject_detach(button->subject, observer);
}

void button_notify(const button_t *button, int argc, const void *argv[]) {
subject_notify(button->subject, argc, argv);
}
68 changes: 68 additions & 0 deletions src/button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* This file is part of the 'Yet another gauge' project.
*
* Copyright (C) 2018 Ivan Dyachenko <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef YET_ANOTHER_GAUGE__LOOP_CALIBRATOR__BUTTON_H
#define YET_ANOTHER_GAUGE__LOOP_CALIBRATOR__BUTTON_H

#include <stdint.h>

#include <observer.h>

#include <libopencm3/stm32/rcc.h>

typedef struct button_t button_t;

/**
* @brief Allocate a struct button_t, intended to be used as an button
* @param [in] clken
* @param [in] gpio_pin
* @param [in] gpio_port
* @return The button
*/
button_t *button_new(enum rcc_periph_clken clken, uint16_t gpio_pin, uint32_t gpio_port);

/**
* @brief Free the memory for the given button
* @param [in,out] button
*/
void button_free(button_t *);

/**
* @brief Attach the observer to a button
* @param [in,out] button
* @param [in] observer
*/
void button_attach(button_t *, const observer_t *);

/**
* @brief Detach the given observer from the button
* @param [in,out] button
* @param [in] observer
*/
void button_detach(button_t *, const observer_t *);

/**
* @brief Send a notification to all the observers of the button
* @param [in] button
* @param [in] argc
* @param [in] argv
*/
void button_notify(const button_t *, int argc, const void *argv[]);

#endif // YET_ANOTHER_GAUGE__LOOP_CALIBRATOR__BUTTON_H

0 comments on commit a5dfcd7

Please sign in to comment.