Skip to content

Commit

Permalink
Clean up style
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Jan 3, 2023
1 parent dc9f7a8 commit 42d66fe
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 234 deletions.
5 changes: 4 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
BasedOnStyle: WebKit
BasedOnStyle: LLVM

ColumnLimit: 120
IndentWidth: 4
ContinuationIndentWidth: 4
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
PointerAlignment: Left
21 changes: 14 additions & 7 deletions include/eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ struct eeprom {

/**
* Initializes the EEPROM (at startup).
*
*
* Restores the pages to a known good state if needed.
*
*
* @param num_keys number of keys (0 to `num_keys` - 1).
*/
static void init(int num_keys);

/**
* Reads the 16 bit value from the specified key.
*
*
* @param key key
* @param value reference to variable receiving value
* @return `true` if value was found, `false` otherwise
Expand All @@ -43,17 +43,24 @@ struct eeprom {

/**
* Writes a 16 bit value for the specified key.
*
*
* @param key key
* @param value value to write
* @return `true` if the write was successful, `false` otherwise
*/
static bool write(uint16_t key, uint16_t value);

private:
enum class status_e { no_valid_page, page_full, op_completed };
private:
enum class status_e {
no_valid_page,
page_full,
op_completed
};

enum class operation_e { read, write };
enum class operation_e {
read,
write
};

static int num_keys_;

Expand Down
17 changes: 9 additions & 8 deletions include/fusb302.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ enum class fusb302_state {
};

/// Event kind
enum class event_kind { none, state_changed, message_received };
enum class event_kind {
none,
state_changed,
message_received
};

/// Event queue by FUSB302 instance for clients (such as `pd_sink`)
struct event {
Expand All @@ -44,15 +48,12 @@ struct event {
/// Message payload (valid if event_kind = `message_received`, possibly `null`)
const uint8_t* msg_payload;

event() : kind(event_kind::none) { }
event() : kind(event_kind::none) {}

event(event_kind evt_kind) : kind(evt_kind) { }
event(event_kind evt_kind) : kind(evt_kind) {}

event(uint16_t header, const uint8_t* payload = nullptr)
: kind(event_kind::message_received)
, msg_header(header)
, msg_payload(payload)
{ }
: kind(event_kind::message_received), msg_header(header), msg_payload(payload) {}
};

/**
Expand Down Expand Up @@ -114,7 +115,7 @@ struct fusb302 {
/// Retrieves the oldest event and removes it from the queue
event pop_event();

private:
private:
void check_for_interrupts();
void check_for_msg();
void start_measurement(int cc);
Expand Down
1 change: 0 additions & 1 deletion include/fusb302_regs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <stdint.h>


namespace usb_pd {

/// FUSB302 register addresses
Expand Down
15 changes: 7 additions & 8 deletions include/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ enum class color {
off = 0b111
};


/**
* Hardware abstraction layer.
*
Expand Down Expand Up @@ -85,29 +84,29 @@ struct mcu_hal {

/**
* Returns if the button has been pressed.
*
*
* Button presses are reported after the button has
* been released.
*
*
* It will return 'true' once and then return 'false' until
* the button has been released and pressed again.
*
*
* @return `true` if the button has been pressed
*/
bool has_button_been_pressed();

/**
* Returns true if the button is currently being pressed.
*
*
* @return `true` if the button is being pressed
*/
bool is_button_being_pressed();

/**
* Returns if the button is being pressed for an extended period.
*
*
* Use to check for long press.
*
*
* @return `true` if the button is being pressed.
*/
bool is_long_press();
Expand Down Expand Up @@ -139,7 +138,7 @@ struct mcu_hal {
*/
bool has_expired(uint32_t timeout);

private:
private:
void update_led();

color led_color;
Expand Down
11 changes: 6 additions & 5 deletions include/pd_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ void debug_init();

#else

#define DEBUG_LOG(MSG, VAL) \
do { } while (false)
#define DEBUG_INIT() \
do { } while (false)
#define DEBUG_LOG(MSG, VAL) \
do { \
} while (false)
#define DEBUG_INIT() \
do { \
} while (false)

#endif

24 changes: 12 additions & 12 deletions include/pd_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ enum class callback_event {

/**
* USB PD power sink.
*
*
* Implements the USB-PD protocol and the negotiation with a power source.
*/
struct pd_sink {
Expand All @@ -77,16 +77,16 @@ struct pd_sink {

/**
* Sets the handler to be notified about USB-PD events.
*
*
* Set the handler before calling `init()`.
*
* @param cb
*
* @param cb callback handler
*/
void set_event_callback(event_callback cb);

/**
* Polls the power sink for events.
*
*
* This function needs to be called regularly from the main loop.
*/
void poll();
Expand All @@ -100,7 +100,7 @@ struct pd_sink {
*
* If the source hasn't advertised a matching voltage, no message is sent and
* -1 is returned.
*
*
* If a programmable power supply (PPS) capability is selected, the sink starts
* to send a `request` message every 8 seconds as required by the standard.
* Otherwise, the source will revert to 5V after 10 seconds.
Expand All @@ -109,7 +109,7 @@ struct pd_sink {
* reduce the voltage until the current is no longer exceeded. A fixed supply uses
* the specified current to distribute the current between multiple outputs. If
* exceed, it might revert to 5V or stop supplying power altogether.
*
*
* @param voltage the desired voltage (in mV)
* @param max_current the highest current (in mA) the sink will draw,
* or 0 for the maximum current the source can provide for the selected voltage
Expand All @@ -119,19 +119,19 @@ struct pd_sink {

/**
* Requests the specified voltage from the specified source capability.
*
*
* The source will respond with `accepted` and `ps_ready` (if successful)
* or `rejected` if unsucessful. Separate events will be triggered for these
* messages.
*
*
* If the specified voltage or current is out of the range for the specified
* source capability or if the index is invalid, no request is sent and -1 is returned.
*
*
* If the sink draws more power the specified maximum current, a PPS capability will
* reduce the voltage until the current is no longer exceeded. A fixed supply uses
* the specified current to distribute the current between multiple outputs. If
* exceed, it might revert to 5V or stop supplying power altogether.
*
*
* @param index index of the source capability
* @param voltage the desired voltage (in mV)
* @param max_current the highest current (in mA) the sink will draw (at least 25mA)
Expand Down Expand Up @@ -166,7 +166,7 @@ struct pd_sink {
/// Specification revision (of last message)
uint8_t spec_rev = 1;

private:
private:
void handle_msg(uint16_t header, const uint8_t* payload);
void handle_src_cap_msg(uint16_t header, const uint8_t* payload);
bool update_protocol();
Expand Down
25 changes: 9 additions & 16 deletions include/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace usb_pd {
* used by a single reader and a single writer.
*/
template <class T, int N> struct queue {
private:
private:
/// Allocation size
static constexpr int BUF_SIZE = N + 1;

Expand All @@ -36,7 +36,7 @@ template <class T, int N> struct queue {

T buffer[BUF_SIZE];

public:
public:
/// Constructs a new queue instance
queue();

Expand All @@ -59,11 +59,9 @@ template <class T, int N> struct queue {
void clear();
};

template <class T, int N>
queue<T, N>::queue() : buf_head(0), buf_tail(0) { }
template <class T, int N> queue<T, N>::queue() : buf_head(0), buf_tail(0) {}

template <class T, int N> int queue<T, N>::avail_items()
{
template <class T, int N> int queue<T, N>::avail_items() {
int head = buf_head;
int tail = buf_tail;

Expand All @@ -74,8 +72,7 @@ template <class T, int N> int queue<T, N>::avail_items()
}
}

template <class T, int N> int queue<T, N>::num_items()
{
template <class T, int N> int queue<T, N>::num_items() {
int head = buf_head;
int tail = buf_tail;

Expand All @@ -86,8 +83,7 @@ template <class T, int N> int queue<T, N>::num_items()
}
}

template <class T, int N> void queue<T, N>::add_item(T&& item)
{
template <class T, int N> void queue<T, N>::add_item(T&& item) {
int head = buf_head;

int new_head = head + 1;
Expand All @@ -101,8 +97,7 @@ template <class T, int N> void queue<T, N>::add_item(T&& item)
buf_head = new_head;
}

template <class T, int N> void queue<T, N>::add_item(T& item)
{
template <class T, int N> void queue<T, N>::add_item(T& item) {
int head = buf_head;

int new_head = head + 1;
Expand All @@ -116,8 +111,7 @@ template <class T, int N> void queue<T, N>::add_item(T& item)
buf_head = new_head;
}

template <class T, int N> T queue<T, N>::pop_item()
{
template <class T, int N> T queue<T, N>::pop_item() {
int tail = buf_tail;
if (tail == buf_head)
return T(); // queue is empty
Expand All @@ -130,8 +124,7 @@ template <class T, int N> T queue<T, N>::pop_item()
return std::move(buffer[tail]);
}

template <class T, int N> void queue<T, N>::clear()
{
template <class T, int N> void queue<T, N>::clear() {
buf_head = 0;
buf_tail = 0;
}
Expand Down
6 changes: 2 additions & 4 deletions include/usb_pd.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ struct pd_header {
return static_cast<pd_msg_type>(((num_data_objs(header) != 0) << 7) | (header & 0x1f));
}

static int spec_rev(uint16_t header) {
return ((header >> 6) & 0x03) + 1;
}
static int spec_rev(uint16_t header) { return ((header >> 6) & 0x03) + 1; }

static uint16_t create_ctrl(pd_msg_type msg_type, int rev = 1) {
return (msg_type & 0x1f) | 0x40 | ((rev - 1) << 6);
}

static uint16_t create_data(pd_msg_type msg_type, int num_data_objs, int rev = 1) {
return ((num_data_objs & 0x07) << 12) | (msg_type & 0x1f) | 0x40 | ((rev - 1) << 6);
}
Expand Down
Loading

0 comments on commit 42d66fe

Please sign in to comment.