diff --git a/flake.nix b/flake.nix index 12ab68c..70c4edc 100644 --- a/flake.nix +++ b/flake.nix @@ -284,6 +284,30 @@ ]; }; + packages."x86_64-linux"."touch-sensor_" = + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + avr = pkgs.pkgsCross.avr.buildPackages; + in packages."x86_64-linux"."touch-sensor" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags attiny85; avr = true; }; + packages."x86_64-linux"."touch-sensor" = + { gcc, cflags, avr }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkProgram { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; + pname = "touch-sensor"; + psrc = ./touch-sensor; + inherit avr; + + pbuildInputs = [ + (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + (packages."x86_64-linux"."display" { inherit gcc cflags; }) + (packages."x86_64-linux"."shift-register" { inherit gcc cflags; }) + ]; + }; + devShell."x86_64-linux" = let pkgs = import nixpkgs { system = "x86_64-linux"; }; diff --git a/touch-sensor/main.c b/touch-sensor/main.c new file mode 100644 index 0000000..4a141ef --- /dev/null +++ b/touch-sensor/main.c @@ -0,0 +1,63 @@ +#include +#include +#include + +uint32_t read_capacitance(dio_t *dio) { + uint32_t i; + + dio_set_direction(dio, LINE_OUT); + dio_set(dio, 1); + + i = 0; + dio_set_direction(dio, LINE_IN); + while (dio_read(dio) > 0) i++; + return i; +} + +int main(void) { + dio_t sensor = (dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 }; + display_t display = { + .reg = { + .output = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 4 }, + .shift_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 3 }, + .latch_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 }, + }, + }; + display_init(&display); + + display_enable(&display); + display_clear(&display); + + display_set_location(&display, 0, 0); + display_write_message(&display, "Ready"); + + + // calibration + // + // 100 times: + // Output 1 + // Switch to Input + // measure in a loop until the input is 0 + // save the measurement + // + // normal function + // Output 1 + // Switch to input + // measure in a loop until the input is 0 + // check against the saved measurement + // enable pin if significantly higher than the saved measurement + // update the average + + MCUCR |= _BV(PUD); + + + while (1) { + uint32_t cap = read_capacitance(&sensor); + char msg[20]; + snprintf(msg, 20, "cap: %5ld", cap); + display_set_location(&display, 1, 0); + display_write_message(&display, msg); + _delay_ms(100); + } + +}