Closed DJKero closed 2 years ago
@DJKero I can try it at some point but I might not have a chance to test a real Teensy with it for a few days.
I actually only have 8 sensors (2 per arrow, 4 arrows) on my fully working pad but I can play around with it.
It's a clever idea to 0-pad the threshold value, since I'm pretty sure leading 0's in the threshold would still be accepted by older versions of the firmware, but this way you can assume that an input of more than 5 characters must have a sensor value that is more than one digit. There's probably a simpler way to parse than the switch statement on the initial digit.
@DJKero I can try it at some point but I might not have a chance to test a real Teensy with it for a few days.
I actually only have 8 sensors (2 per arrow, 4 arrows) on my fully working pad but I can play around with it.
It's a clever idea to 0-pad the threshold value, since I'm pretty sure leading 0's in the threshold would still be accepted by older versions of the firmware, but this way you can assume that an input of more than 5 characters must have a sensor value that is more than one digit. There's probably a simpler way to parse than the switch statement on the initial digit.
Cool, thanks! Yeah if you want to improve that part you're free to do so, I'm a bit burnt out from debugging this lol. It is ready for testing, in theory.
By the way, @teejusb @joshhead, please check this commit because these lines were the culprits, you may want to do something about them or not, I'm just pointing out that I had to comment them out. ^^
Edit: Also there's a bug when changing profiles. If you can hunt that one down it'd be appreciated :)
As far as I can tell, the below code is equivalent to the version of UpdateAndPrintThreshold
in this branch. Maybe not "simpler" but to me it's clearer. It compiles without warnings and I tested the approach with a snippet in an online compiler (https://onlinegdb.com/8rwNwFPEAm) but I haven't put it on a Teensy yet.
The question still remains whether this is the right way to support additional panels. :)
I think it would work and not be disruptive, but I think it's also worth asking some questions, like does it need to be backwards compatible? Maybe a new command like s400 400 400 400 400 400 400 400 400 400 400
that sets all thresholds at once, or s1 400
with sensor number followed by a space and the then the threshold.
void UpdateAndPrintThreshold(size_t bytes_read) {
// Need to specify:
// Sensor number + Threshold value.
// Threshold value should be 0-padded to 4 digits.
// {0, 1, 2, 3,...99} + "0000"-"1023"
// e.g. 30180 (fourth FSR, change threshold to 180)
// Ignore this command if it has too many or too few bytes.
if (bytes_read < 2 || bytes_read > 6) { return; }
// Enough room for two characters plus a null terminator.
char sensor_index_cstr[3];
// Will be set to the start of the threshold string to parse.
char* threshold_cstr;
// If bytes_read is less than 5, assume legacy command where
// threshold it not 0-padded, and sensor_index is always one
// character.
// If bytes_read is exactly 5, it could be a legacy command
// with a threshold >1000, or a 0 padded threshold, but either
// way the sensor_index will still be one character.
if (bytes_read < 6) {
sensor_index_cstr[0] = buffer_[0];
sensor_index_cstr[1] = '\0';
threshold_cstr = buffer_ + 1;
} else {
// bytes_read is equal to 6, meaning a two-digit sensor_index followed
// by a four-digit threshold value.
sensor_index_cstr[0] = buffer_[0];
sensor_index_cstr[1] = buffer_[1];
sensor_index_cstr[2] = '\0';
threshold_cstr = buffer_ + 2;
}
size_t sensor_index = strtoul(sensor_index_cstr, nullptr, 10);
if (sensor_index >= kNumSensors) { return; }
kSensors[sensor_index].UpdateThreshold(
strtoul(threshold_cstr, nullptr, 10));
PrintThresholds();
}
I think at this line sensor_index doesn't exist and we need to convert sensor_index_cstr to a number:
kSensors[sensor_index].UpdateThreshold
so something like this returns a long:
String(sensor_index_cstr).toInt()
so, it would end up like this:
long sensor_index = String(sensor_index_cstr).toInt();
kSensors[sensor_index].UpdateThreshold(
strtoul(threshold_cstr, nullptr, 10));
PrintThresholds();
Aside from that I believe it should work, yeah.
@DJKero I asked @teejusb about the command format and he suggested separating the index and threshold with a space. I just pushed up a branch that does it that way, check out #35 when you get a chance
In the above code the line size_t sensor_index = strtoul(sensor_index_cstr, nullptr, 10);
is where sensor_index_cstr
gets converted to a number.
@DJKero I asked @teejusb about the command format and he suggested separating the index and threshold with a space. I just pushed up a branch that does it that way, check out #35 when you get a chance
In the above code the line
size_t sensor_index = strtoul(sensor_index_cstr, nullptr, 10);
is wheresensor_index_cstr
gets converted to a number.
Oh I didn't see that line, Yeah I'll check #35 tomorrow if I don't forget about it. Thanks!
As mentioned, #35 seems like a nicer way to support more sensors so I'll close this. Thanks for your help in getting this functionality moving along!
@joshhead could you check if this works fine?