~npisanti/rpiezos

e7ddea38a28740c5e0d75e3da785d3c01925986a — Nicola Pisanti 3 months ago 16e7368 master
cleaning
3 files changed, 49 insertions(+), 47 deletions(-)

M .clang-format
M Makefile
M src/main.c
M .clang-format => .clang-format +2 -1
@@ 2,4 2,5 @@ BasedOnStyle: LLVM
IndentWidth: 8
UseTab: ForIndentation
BreakBeforeBraces : Linux
ColumnLimit: 0
\ No newline at end of file
ColumnLimit: 0
PointerAlignment: Middle
\ No newline at end of file

M Makefile => Makefile +1 -1
@@ 27,7 27,7 @@ CFLAGS += $(INC_FLAGS) -MMD -MP

all: release

release: CFLAGS += -Os -DNDEBUG
release: CFLAGS += -O3 -DNDEBUG
release: $(TARGET)

debug: CFLAGS += -DDEBUG -Wpedantic -Wextra 

M src/main.c => src/main.c +46 -45
@@ 18,33 18,33 @@

#define NUM_PIEZOS 6

typedef struct piezo_t {
struct piezo_t {
	int enabled;
	int min;
	int max;
	int mark;
	float z1;
} piezo_t;
};

typedef struct config_t {
struct config_t {
	long int sleep_usecs;
	int verbose;
	const char *ip;
	const char *port;
	const char *address;
} config_t;
	const char * ip;
	const char * port;
	const char * address;
};

void quit_handler(int signo);
int parse_args(int argc, char *argv[], piezo_t *piezos, config_t *config);
int parse_args(int argc, char * argv[], struct piezo_t * piezos, struct config_t * config);
void usec_sleep(long int usecs);

int running = 1;

int main(int argc, char *argv[])
int main(int argc, char * argv[])
{
	int rc = 1;
	int rc = 0;

	piezo_t piezos[NUM_PIEZOS];
	struct piezo_t piezos[NUM_PIEZOS];
	for (unsigned i = 0; i < NUM_PIEZOS; ++i) {
		piezos[i].enabled = 0;
		piezos[i].min = 0;


@@ 53,7 53,7 @@ int main(int argc, char *argv[])
		piezos[i].z1 = -1;
	}

	config_t config;
	struct config_t config;
	config.ip = NULL;
	config.port = NULL;
	config.address = "/piezo";


@@ 75,12 75,12 @@ int main(int argc, char *argv[])

	// --------- signal handling -------------
	if (signal(SIGINT, quit_handler) == SIG_ERR) {
		printf("[rpiezos] Error on assigning signal handler\n");
		return 2;
		fprintf(stderr, "[rpiezos] Error on assigning signal handler\n");
		return -1;
	}
	if (signal(SIGTERM, quit_handler) == SIG_ERR) {
		printf("[rpiezos] Error on assigning signal handler\n");
		return 2;
		fprintf(stderr, "[rpiezos] Error on assigning signal handler\n");
		return -1;
	}

	// --------- mcp init --------------------


@@ 91,8 91,8 @@ int main(int argc, char *argv[])

	spi_fd = open("/dev/spidev0.0", O_RDWR);
	if (spi_fd < 0) {
		printf("[rpiezos] Error! opening spi device failed\n");
		return 1;
		fprintf(stderr, "[rpiezos] Error! opening spi device failed\n");
		return -1;
	}

	mode = SPI_MODE_0;


@@ 101,32 101,38 @@ int main(int argc, char *argv[])

	rc = ioctl(spi_fd, SPI_IOC_WR_MODE, &mode);
	if (rc != 0) {
		printf("[rpiezos] Error! ioctl failed, cound not set SPI mode (WR)\n");
		printf("[rpiezos] Error! ioctl failed, "
		       "cound not set SPI mode (WR)\n");
		return rc;
	}
	rc = ioctl(spi_fd, SPI_IOC_RD_MODE, &mode);
	if (rc != 0) {
		printf("[rpiezos] Error! ioctl failed, cound not set SPI mode (RD)\n");
		fprintf(stderr, "[rpiezos] Error! ioctl failed, "
		                "cound not set SPI mode (RD)\n");
		return rc;
	}
	rc = ioctl(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &bits_per_word);
	if (rc != 0) {
		printf("[rpiezos] Error! ioctl failed, cound not set SPI bits per word (WR)\n");
		fprintf(stderr, "[rpiezos] Error! ioctl failed, "
		                "cound not set SPI bits per word (WR)\n");
		return rc;
	}
	rc = ioctl(spi_fd, SPI_IOC_RD_BITS_PER_WORD, &bits_per_word);
	if (rc != 0) {
		printf("[rpiezos] Error! ioctl failed, cound not set SPI bits per word (RD) \n");
		fprintf(stderr, "[rpiezos] Error! ioctl failed, "
		                "cound not set SPI bits per word (RD) \n");
		return rc;
	}
	rc = ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (rc != 0) {
		printf("[rpiezos] Error! ioctl failed, cound not set SPI speed (WR) \n");
		fprintf(stderr, "[rpiezos] Error! ioctl failed, "
		                "cound not set SPI speed (WR) \n");
		return rc;
	}
	rc = ioctl(spi_fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
	if (rc != 0) {
		printf("[rpiezos] Error! ioctl failed, cound not set SPI speed (RD) \n");
		fprintf(stderr, "[rpiezos] Error! ioctl failed, "
		                "cound not set SPI speed (RD) \n");
		return rc;
	}



@@ 158,8 164,9 @@ int main(int argc, char *argv[])

			ret_val = ioctl(spi_fd, SPI_IOC_MESSAGE(3), &spi);
			if (ret_val < 0) {
				printf("[rpiezos] Error! ioctl failed durint spi data transfer\n");
				return 1;
				fprintf(stderr, "[rpiezos] Error! ioctl failed durint "
				                "spi data transfer\n");
				return -1;
			}

			int a2d_val = 0;


@@ 194,13 201,6 @@ int main(int argc, char *argv[])
	return 0;
}

int args_are_not_valid(int argc, char *argv[])
{
	(void)argc;
	(void)argv;
	return 0;
}

void quit_handler(int signo)
{
	if (signo == SIGINT || signo == SIGTERM) {


@@ 217,9 217,9 @@ void usec_sleep(long int usecs)
	nanosleep(&tim, &tim2);
}

int parse_piezo(char *arg, piezo_t *piezos)
int parse_piezo(char * arg, struct piezo_t * piezos)
{
	unsigned len = strlen(arg);
	size_t len = strlen(arg);
	int mode = 0;
	int min = 0;
	int max = 1024;


@@ 261,12 261,12 @@ int parse_piezo(char *arg, piezo_t *piezos)
	}

	if (min >= 1024) {
		printf("[rpiezos] min value should be lower than 1024");
		return 1;
		fprintf(stderr, "[rpiezos] min value should be lower than 1024");
		return -1;
	}
	if (max > 1024) {
		printf("[rpiezos] min value should not be greater than 1024");
		return 1;
		fprintf(stderr, "[rpiezos] min value should not be greater than 1024");
		return -1;
	}

	for (int i = 0; i < NUM_PIEZOS; ++i) {


@@ 280,7 280,7 @@ int parse_piezo(char *arg, piezo_t *piezos)
	return 0;
}

int parse_args(int argc, char *argv[], piezo_t *piezos, config_t *config)
int parse_args(int argc, char * argv[], struct piezo_t * piezos, struct config_t * config)
{
	int has_port = 0;
	int has_piezos = 0;


@@ 333,13 333,14 @@ int parse_args(int argc, char *argv[], piezo_t *piezos, config_t *config)
	}

	if (!has_port) {
		printf("[rpiezos] destination port not given! use the -p argument to set it\n");
		return 1;
		fprintf(stderr, "[rpiezos] destination port not given!"
		                " use the -p argument to set it\n");
		return -1;
	}
	if (!has_piezos) {
		printf("[rpiezos] no piezo sensor activated, use -s to set it\n"
		       "          for example -s 124 to activate sensors 1, 2 and 4\n");
		return 1;
		fprintf(stderr, "[rpiezos] no piezo sensor activated, use -s to set it\n"
		                "          for example -s 124 to activate sensors 1, 2 and 4\n");
		return -1;
	}

	return 0;