Skip to content

Commit

Permalink
Merge pull request #21 from Sensirion/clang-format
Browse files Browse the repository at this point in the history
Define and apply the project style
  • Loading branch information
abrauchli authored May 9, 2019
2 parents 3c75ad0 + 066292c commit b2be9b7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 32 deletions.
11 changes: 11 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 4
AlignAfterOpenBracket: Align
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
IndentCaseLabels: true
SpacesBeforeTrailingComments: 2
...
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ drivers=sps30-i2c
clean_drivers=$(foreach d, $(drivers), clean_$(d))
release_drivers=$(foreach d, $(drivers), release/$(d))

.PHONY: FORCE all $(release_drivers) $(clean_drivers)
.PHONY: FORCE all $(release_drivers) $(clean_drivers) style-check style-fix

all: $(drivers)

Expand Down Expand Up @@ -40,3 +40,20 @@ $(clean_drivers):

clean: $(clean_drivers)
rm -rf release sps30-i2c/sps_git_version.c

style-fix:
@if [ $$(git status --porcelain -uno 2> /dev/null | wc -l) -gt "0" ]; \
then \
echo "Refusing to run on dirty git state. Commit your changes first."; \
exit 1; \
fi; \
git ls-files | grep -e '\.\(c\|h\|cpp\)$$' | xargs clang-format -i -style=file;

style-check: style-fix
@if [ $$(git status --porcelain -uno 2> /dev/null | wc -l) -gt "0" ]; \
then \
echo "Style check failed:"; \
git diff; \
git checkout -f; \
exit 1; \
fi;
42 changes: 19 additions & 23 deletions sps30-i2c/sps30.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,25 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "sps30.h"
#include "sensirion_arch_config.h"
#include "sensirion_i2c.h"
#include "sensirion_common.h"
#include "sensirion_i2c.h"
#include "sps_git_version.h"
#include "sps30.h"


static const u8 SPS_I2C_ADDRESS = 0x69;


#define SPS_CMD_START_MEASUREMENT 0x0010
#define SPS_CMD_START_MEASUREMENT_ARG 0x0300
#define SPS_CMD_STOP_MEASUREMENT 0x0104
#define SPS_CMD_READ_MEASUREMENT 0x0300
#define SPS_CMD_GET_DATA_READY 0x0202
#define SPS_CMD_AUTOCLEAN_INTERVAL 0x8004
#define SPS_CMD_GET_SERIAL 0xd033
#define SPS_CMD_RESET 0xd304
#define SPS_WRITE_DELAY_US 20000

const char *sps_get_driver_version()
{
#define SPS_CMD_START_MEASUREMENT 0x0010
#define SPS_CMD_START_MEASUREMENT_ARG 0x0300
#define SPS_CMD_STOP_MEASUREMENT 0x0104
#define SPS_CMD_READ_MEASUREMENT 0x0300
#define SPS_CMD_GET_DATA_READY 0x0202
#define SPS_CMD_AUTOCLEAN_INTERVAL 0x8004
#define SPS_CMD_GET_SERIAL 0xd033
#define SPS_CMD_RESET 0xd304
#define SPS_WRITE_DELAY_US 20000

const char *sps_get_driver_version() {
return SPS_DRV_VERSION_STR;
}

Expand Down Expand Up @@ -90,8 +87,7 @@ s16 sps30_start_measurement() {
const u16 arg = SPS_CMD_START_MEASUREMENT_ARG;

return sensirion_i2c_write_cmd_with_args(SPS_I2C_ADDRESS,
SPS_CMD_START_MEASUREMENT,
&arg,
SPS_CMD_START_MEASUREMENT, &arg,
SENSIRION_NUM_WORDS(arg));
}

Expand Down Expand Up @@ -160,9 +156,9 @@ s16 sps30_get_fan_auto_cleaning_interval(u32 *interval_seconds) {
u16 u16[2];
u32 u32;
} data;
s16 ret = sensirion_i2c_read_cmd(SPS_I2C_ADDRESS,
SPS_CMD_AUTOCLEAN_INTERVAL,
data.u16, SENSIRION_NUM_WORDS(data.u16));
s16 ret =
sensirion_i2c_read_cmd(SPS_I2C_ADDRESS, SPS_CMD_AUTOCLEAN_INTERVAL,
data.u16, SENSIRION_NUM_WORDS(data.u16));
if (ret != STATUS_OK)
return ret;

Expand Down Expand Up @@ -197,8 +193,8 @@ s16 sps30_get_fan_auto_cleaning_interval_days(u8 *interval_days) {
}

s16 sps30_set_fan_auto_cleaning_interval_days(u8 interval_days) {
return sps30_set_fan_auto_cleaning_interval((u32)interval_days *
24 * 60 * 60);
return sps30_set_fan_auto_cleaning_interval((u32)interval_days * 24 * 60 *
60);
}

s16 sps30_reset() {
Expand Down
12 changes: 5 additions & 7 deletions sps30-i2c/sps30_example_usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h> // printf
#include <unistd.h> // sleep / usleep
#include <stdio.h> // printf
#include <unistd.h> // sleep / usleep

#include "sps30.h"

Expand All @@ -41,8 +41,7 @@
// #define printf(...)
// #define sleep(...)

int main(void)
{
int main(void) {
struct sps30_measurement m;
char serial[SPS_MAX_SERIAL_LEN];
u8 auto_clean_days = 4;
Expand Down Expand Up @@ -98,9 +97,8 @@ int main(void)
"\t%0.2f nc4.5\n"
"\t%0.2f nc10.0\n"
"\t%0.2f typical particle size\n\n",
m.mc_1p0, m.mc_2p5, m.mc_4p0, m.mc_10p0,
m.nc_0p5, m.nc_1p0, m.nc_2p5, m.nc_4p0, m.nc_10p0,
m.typical_particle_size);
m.mc_1p0, m.mc_2p5, m.mc_4p0, m.mc_10p0, m.nc_0p5, m.nc_1p0,
m.nc_2p5, m.nc_4p0, m.nc_10p0, m.typical_particle_size);
}

sleep(1);
Expand Down
2 changes: 1 addition & 1 deletion sps30-i2c/sps_git_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
#ifndef SPS_GIT_VERSION_H
#define SPS_GIT_VERSION_H

extern const char * SPS_DRV_VERSION_STR;
extern const char *SPS_DRV_VERSION_STR;

#endif /* SPS_GIT_VERSION_H */

0 comments on commit b2be9b7

Please sign in to comment.