Skip to content

Commit

Permalink
Add .clang-format file and format all files
Browse files Browse the repository at this point in the history
Added a format to be consistent and apply that format
to all C and header files.
  • Loading branch information
Zheoni committed Feb 18, 2021
1 parent d03214f commit f1bde2a
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 204 deletions.
8 changes: 8 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BasedOnStyle: llvm
IndentWidth: 4
PointerAlignment: Left
SpaceAfterCStyleCast: true
AllowShortFunctionsOnASingleLine: false
SpaceBeforeAssignmentOperators: true
AlignConsecutiveMacros: true
Cpp11BracedListStyle: false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
.vscode/
20 changes: 13 additions & 7 deletions libs/picoz/button.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#include "button.h"
#include "hardware/clocks.h"
#include "hardware/pio.h"
#include "hardware/gpio.h"
#include "hardware/pio.h"

#include "button_debounce.pio.h"

const static PIO pios[2] = { pio0, pio1 };
static uint sm_count[2] = { 0 };
static uint offset[2];

bool button_init(uint button_pin, enum button_pull pull, bool pressed_state, button* b) {
bool button_init(uint button_pin, enum button_pull pull, bool pressed_state,
button* b) {
b->pin = button_pin;
gpio_init(button_pin);
gpio_set_dir(button_pin, GPIO_IN);
gpio_set_pulls(button_pin, pull == BUTTON_PULL_UP, pull == BUTTON_PULL_DOWN);
gpio_set_pulls(button_pin, pull == BUTTON_PULL_UP,
pull == BUTTON_PULL_DOWN);

b->pressed_state = pressed_state;
b->last_state = !pressed_state;
Expand All @@ -34,15 +36,17 @@ bool button_init(uint button_pin, enum button_pull pull, bool pressed_state, but
// load the program into the PIO if its not loaded
if (sm_count[b->pio_idx] == 0) {
if (pio_can_add_program(pios[b->pio_idx], &button_debounce_program)) {
offset[b->pio_idx] = pio_add_program(pios[b->pio_idx], &button_debounce_program);
offset[b->pio_idx] =
pio_add_program(pios[b->pio_idx], &button_debounce_program);
++sm_count[b->pio_idx];
} else {
return false;
}
}

// Configure the state machine
pio_sm_config c = button_debounce_program_get_default_config(offset[b->pio_idx]);
pio_sm_config c =
button_debounce_program_get_default_config(offset[b->pio_idx]);
sm_config_set_in_pins(&c, button_pin);
sm_config_set_jmp_pin(&c, button_pin);
sm_config_set_clkdiv_int_frac(&c, 40322, 149); // ~ 20 ms
Expand All @@ -59,7 +63,8 @@ void button_deinit(button* b) {
pio_sm_unclaim(pios[b->pio_idx], b->sm);
--sm_count[b->pio_idx];
if (sm_count[b->pio_idx] == 0) {
pio_remove_program(pios[b->pio_idx], &button_debounce_program, offset[b->pio_idx]);
pio_remove_program(pios[b->pio_idx], &button_debounce_program,
offset[b->pio_idx]);
}
}

Expand All @@ -72,7 +77,8 @@ bool button_set_debounce_time_us(button* b, uint32_t debounce_time_us) {
uint32_t sysclock = clock_get_hz(clk_sys);

float div = sysclock * debounce_time_us / 62000000.0;
if (div > 0xffff) div = 0xffff;
if (div > 0xffff)
div = 0xffff;
pio_sm_set_clkdiv(pios[b->pio_idx], b->sm, div);
}

Expand Down
3 changes: 2 additions & 1 deletion libs/picoz/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ typedef enum {
BUTTON_RELEASE,
} button_change_t;

bool button_init(uint button_pin, enum button_pull pull, bool pressed_state, button* b);
bool button_init(uint button_pin, enum button_pull pull, bool pressed_state,
button* b);

void button_deinit(button* b);

Expand Down
58 changes: 32 additions & 26 deletions libs/picoz/buzzer.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include "buzzer.h"
#include "hardware/clocks.h"
#include "hardware/pwm.h"
#include "hardware/gpio.h"
#include "hardware/pwm.h"
#include "hardware/sync.h"
#include "pico/time.h"
#include <stdlib.h>

#define TOP_MAX 65534
/**
* Calculate the top and div values for a sound depending
* on the sysclock.
*
* Got help from the mycropython function machine_pwm_freq
*/
* Calculate the top and div values for a sound depending
* on the sysclock.
*
* Got help from the mycropython function machine_pwm_freq
*/
sound buzzer_calc_sound(uint freq) {
uint32_t source_hz = clock_get_hz(clk_sys);

Expand All @@ -30,7 +30,8 @@ sound buzzer_calc_sound(uint freq) {
if (div16_top >= 16 * 5 && div16_top % 5 == 0 && top * 5 <= TOP_MAX) {
div16_top /= 5;
top *= 5;
} else if (div16_top >= 16 * 3 && div16_top % 3 == 0 && top * 3 <= TOP_MAX) {
} else if (div16_top >= 16 * 3 && div16_top % 3 == 0 &&
top * 3 <= TOP_MAX) {
div16_top /= 3;
top *= 3;
} else if (div16_top >= 16 * 2 && top * 2 <= TOP_MAX) {
Expand All @@ -46,7 +47,7 @@ sound buzzer_calc_sound(uint freq) {
return 0;
}

sound s = (sound) (((uint16_t) div16_top) << 16 | ((uint16_t) top));
sound s = (sound)(((uint16_t) div16_top) << 16 | ((uint16_t) top));
return s;
}

Expand All @@ -61,20 +62,21 @@ void buzzer_init(uint buzzer_pin) {
}

/**
* The sound is a 32 bit integer.
* The first half represent the div as a 12 bit integer
* with a padding of 4 0s before the value.
* The second half represent the top as a 16 bit integer.
*
* This 2 values make up the frequency. The frecuency is
* calculated with (sysclock)/(div * top).
* But div is special. The first 8 bits is the integer
* value and the other 4 bits are the fractional part.
*
* Then div is a decimal number: 8bits.4bits
*/
* The sound is a 32 bit integer.
* The first half represent the div as a 12 bit integer
* with a padding of 4 0s before the value.
* The second half represent the top as a 16 bit integer.
*
* This 2 values make up the frequency. The frecuency is
* calculated with (sysclock)/(div * top).
* But div is special. The first 8 bits is the integer
* value and the other 4 bits are the fractional part.
*
* Then div is a decimal number: 8bits.4bits
*/
void buzzer_play_sound(uint buzzer_pin, sound s) {
if (s == 0) return;
if (s == 0)
return;
uint16_t div, top;
div = (s & 0x0FFF0000) >> 16;
top = s & 0x0000FFFF;
Expand Down Expand Up @@ -132,7 +134,7 @@ static int64_t _buzzer_non_blocking_callback(alarm_id_t id, void* user_data) {

if (not_end(call->notes[call->current])) {
buzzer_play_sound(call->buzzer_pin, call->notes[call->current].s);
return - ((int64_t) call->notes[call->current].d) * 1000;
return -((int64_t) call->notes[call->current].d) * 1000;
} else {
free(call);
uint32_t state = save_and_disable_interrupts();
Expand All @@ -144,18 +146,21 @@ static int64_t _buzzer_non_blocking_callback(alarm_id_t id, void* user_data) {

bool buzzer_play_sound_sequence_non_blocking(uint buzzer_pin, note* notes) {
if (not_end(notes[0])) {
struct non_blocking_seq* call = (struct non_blocking_seq*) malloc(sizeof(struct non_blocking_seq));
struct non_blocking_seq* call =
(struct non_blocking_seq*) malloc(sizeof(struct non_blocking_seq));
call->buzzer_pin = buzzer_pin;
call->current = 0;
call->notes = notes;
if (call == NULL) return false;
if (call == NULL)
return false;

uint32_t state = save_and_disable_interrupts();
++running_non_blocking_sequences;
restore_interrupts(state);

buzzer_play_sound(buzzer_pin, notes[0].s);
if (add_alarm_in_ms(notes[0].d, _buzzer_non_blocking_callback, call, true) == -1) {
if (add_alarm_in_ms(notes[0].d, &_buzzer_non_blocking_callback, call,
true) == -1) {
free(call);
uint32_t state = save_and_disable_interrupts();
--running_non_blocking_sequences;
Expand All @@ -168,5 +173,6 @@ bool buzzer_play_sound_sequence_non_blocking(uint buzzer_pin, note* notes) {

void buzzer_block_until_sequences_finish() {
// dont need to worry about interruptions because the read is atomic
while (running_non_blocking_sequences > 0);
while (running_non_blocking_sequences > 0)
;
}
4 changes: 2 additions & 2 deletions libs/picoz/buzzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ typedef struct {
uint32_t d;
} note;

#define BUZZER_END_SEQUENCE { 0, 0 }
#define BUZZER_END_SEQUENCE \
{ 0, 0 }

// Calc a sound from a frequency.
// When return 0, ERROR.
Expand All @@ -27,5 +28,4 @@ void buzzer_play_sound_sequence(uint buzzer_pin, note* notes);
bool buzzer_play_sound_sequence_non_blocking(uint buzzer_pin, note* notes);
void buzzer_block_until_sequences_finish();


#endif /* end of include guard: BUZZER_H */
28 changes: 19 additions & 9 deletions libs/picoz/led.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ void led_init(uint led_pin, led* l) {
}

void led_enable_pwm(led* l) {
if (l->is_pwm_enabled) return;
if (l->is_pwm_enabled)
return;

bool current_state = gpio_get(l->pin);
uint16_t cc = current_state ? 0xFFFE : 0x0000;
Expand All @@ -31,14 +32,17 @@ void led_enable_pwm(led* l) {
}

void led_disable_pwm(led* l) {
if (!l->is_pwm_enabled) return;
if (!l->is_pwm_enabled)
return;

uint16_t cc = pwm_hw->slice[l->slice].cc;
cc = (cc >> (l->channel ? PWM_CH0_CC_B_LSB : PWM_CH0_CC_A_LSB)) & 0xFFFF;
bool state = cc > 0;

uint cc_other = pwm_hw->slice[l->slice].cc;
cc_other = (cc_other >> (l->channel ? PWM_CH0_CC_A_LSB : PWM_CH0_CC_B_LSB)) & 0xFFFF;
cc_other =
(cc_other >> (l->channel ? PWM_CH0_CC_A_LSB : PWM_CH0_CC_B_LSB)) &
0xFFFF;
if (cc_other == 0) {
pwm_set_enabled(l->slice, false);
}
Expand All @@ -50,14 +54,16 @@ void led_disable_pwm(led* l) {
}

void led_put(led* l, bool value) {
if (l->is_pwm_enabled) return;
if (l->is_pwm_enabled)
return;
gpio_put(l->pin, value);
}

void led_toggle(led* l) {
if (l->is_pwm_enabled) {
uint16_t cc = pwm_hw->slice[l->slice].cc;
cc = (cc >> (l->channel ? PWM_CH0_CC_B_LSB : PWM_CH0_CC_A_LSB)) & 0xFFFF;
cc =
(cc >> (l->channel ? PWM_CH0_CC_B_LSB : PWM_CH0_CC_A_LSB)) & 0xFFFF;
pwm_set_chan_level(l->slice, l->channel, !cc);
} else {
gpio_put(l->pin, !gpio_get(l->pin));
Expand All @@ -81,7 +87,8 @@ void led_off(led* l) {
}

void led_level(led* l, uint8_t level) {
if (!l->is_pwm_enabled) return;
if (!l->is_pwm_enabled)
return;
pwm_set_chan_level(l->slice, l->channel, level * level);
}

Expand All @@ -102,15 +109,18 @@ static bool _led_pulsating_callback(repeating_timer_t* rt) {
}

bool led_start_pulsating(led* l, int32_t delay_ms) {
if (!l->is_pwm_enabled) return false;
if (!l->is_pwm_enabled)
return false;

l->intensity = 0;
l->direction = 1;
return add_repeating_timer_ms(delay_ms, _led_pulsating_callback, (void*) l, &l->timer);
return add_repeating_timer_ms(delay_ms, _led_pulsating_callback, (void*) l,
&l->timer);
}

bool led_stop_pulsating(led* l) {
if (!l->is_pwm_enabled) return false;
if (!l->is_pwm_enabled)
return false;

return cancel_repeating_timer(&l->timer);
}
2 changes: 1 addition & 1 deletion libs/picoz/led.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef LED_H
#define LED_H

#include "pico/types.h"
#include "pico/time.h"
#include "pico/types.h"

typedef struct {
uint pin;
Expand Down
8 changes: 6 additions & 2 deletions libs/picoz/util.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "util.h"
#include "pico/time.h"

uint wait_button_push_detailed(uint16_t on_leds_mask, uint16_t pulsating_leds_mask, uint32_t delay_ms, led* leds, button* buttons, uint n, uint32_t timeout_ms) {
uint wait_button_push_detailed(uint16_t on_leds_mask,
uint16_t pulsating_leds_mask, uint32_t delay_ms,
led* leds, button* buttons, uint n,
uint32_t timeout_ms) {
uint16_t led_bit;
for (int i = 0; i < n; ++i) {
led_bit = (1 << i);
Expand All @@ -25,7 +28,8 @@ uint wait_button_push_detailed(uint16_t on_leds_mask, uint16_t pulsating_leds_ma
if (button_change_steady(&buttons[i]) == BUTTON_PRESS) {
exit = true;
pressed_button = i;
while (button_change_steady(&buttons[i]) != BUTTON_RELEASE);
while (button_change_steady(&buttons[i]) != BUTTON_RELEASE)
;
break;
}
}
Expand Down
9 changes: 6 additions & 3 deletions libs/picoz/util.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#ifndef UTIL_H
#define UTIL_H

#include "pico/types.h"
#include "led.h"
#include "button.h"
#include "led.h"
#include "pico/types.h"

uint wait_button_push(led* leds, button* buttons, uint n);
uint wait_button_push_detailed(uint16_t on_leds_mask, uint16_t pulsating_leds_mask, uint32_t delay_ms, led* leds, button* buttons, uint n, uint32_t timeout_ms);
uint wait_button_push_detailed(uint16_t on_leds_mask,
uint16_t pulsating_leds_mask, uint32_t delay_ms,
led* leds, button* buttons, uint n,
uint32_t timeout_ms);

#endif /* end of include guard: UTIL_H */
Loading

0 comments on commit f1bde2a

Please sign in to comment.