~npisanti/rpiezos

99025aa9f855fb577dca3d3324c2e2bbef0cf66d — Nicola Pisanti 4 months ago 676c133
improves functionality
2 files changed, 28 insertions(+), 8 deletions(-)

M README.md
M src/main.c
M README.md => README.md +3 -0
@@ 18,6 18,8 @@ option list:
-p [destination port] : sets the destination port. This is mandatory to set.
-a [destination address] : sets the destination OSC address. defaults to /piezo
-p [piezos options] : sets the active piezos and range, see below     
-u [usec sleep] : sets the delay in the main loop in usec, defaults to 500     
-v [verbose] : activate a verbose output, doesn't need any argument      
```

after the `-p` option you can give a list of number from 0 to 5, to activate the correlated sensors, optionally you can add some `:` to set ranges, value outside the ranges won't be sent. For example:


@@ 40,6 42,7 @@ rpiezos -a /test -p 3333 -s 2:10:1024 -s 3:5:1024

sends messages to localhost:3333, at the OSC address /test, sends just the values from 10 to 1024 of sensor 2 and from 5 to 1024 of sensor 3.

The messages will be sent to the set address (default to `/piezo`) and they will have two arguments: an int for the sensor number and a normalized float for the signal value (low-high 10 bit range is remapped to 0.0f <--> 1.0f ).

## Building 


M src/main.c => src/main.c +25 -8
@@ 20,13 20,14 @@

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

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


@@ 57,6 58,7 @@ int main(int argc, char *argv[])
	config.port = NULL;
	config.address = "/piezo";
	config.verbose = 0;
	config.sleep_usecs = 500;

	// --------- args -------------------------
	rc = parse_args(argc, argv, piezos, &config);


@@ 165,18 167,27 @@ int main(int argc, char *argv[])
			a2d_val |= (data[2] & 0xff);

			if (piezos[p].enabled) {
				if (a2d_val >= piezos[p].min &&
				    a2d_val <= piezos[p].max &&
				    a2d_val != piezos[p].z1) {
					lo_send(dest, config.address, "ii", p, a2d_val);
					piezos[p].z1 = a2d_val;
				float range = (float)(piezos[p].max - piezos[p].min);
				float pct = (float)(a2d_val - piezos[p].min);
				float value = pct / range;
				if (value < 0.0f) {
					value = 0.0f;
				}
				if (value > 1.0f) {
					value = 1.0f;
				}

				if (value != piezos[p].z1) {
					lo_send(dest, config.address, "if", p, value);
					piezos[p].z1 = value;
					if (config.verbose) {
						printf("[rpiezos] n %d = %d\n", p, a2d_val);
						printf("[rpiezos] n %d | a2d value = %d | output = %f\n",
						       p, a2d_val, value);
					}
				}
			}
		}
		usec_sleep(500);
		usec_sleep(config.sleep_usecs);
	}

	return 0;


@@ 305,6 316,12 @@ int parse_args(int argc, char *argv[], piezo_t *piezos, config_t *config)
					c++;
				}
				break;
			case 'u':
				if (c < argc - 1) {
					config->sleep_usecs = atol(argv[c + 1]);
					c++;
				}
				break;
			case 'v':
				config->verbose = 1;
				break;