Set up an experimental touch sensor app

This commit is contained in:
Savanni D'Gerinel 2023-07-11 23:09:42 -04:00
parent b15c5edb4d
commit ed3c2a3c86
2 changed files with 87 additions and 0 deletions

View File

@ -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" = devShell."x86_64-linux" =
let let
pkgs = import nixpkgs { system = "x86_64-linux"; }; pkgs = import nixpkgs { system = "x86_64-linux"; };

63
touch-sensor/main.c Normal file
View File

@ -0,0 +1,63 @@
#include <dio.h>
#include <stdio.h>
#include <display.h>
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);
}
}