WIP phototransistor stuff

This commit is contained in:
joeycastillo 2023-01-23 11:27:15 -06:00
parent 8014d5c8c5
commit 61c3b0cac7
8 changed files with 262 additions and 0 deletions

View File

@ -148,6 +148,7 @@ SRCS += \
$(TOP)/watch-library/hardware/hpl/sercom/hpl_sercom.c \ $(TOP)/watch-library/hardware/hpl/sercom/hpl_sercom.c \
$(TOP)/watch-library/hardware/hpl/slcd/hpl_slcd.c \ $(TOP)/watch-library/hardware/hpl/slcd/hpl_slcd.c \
$(TOP)/watch-library/hardware/hpl/systick/hpl_systick.c \ $(TOP)/watch-library/hardware/hpl/systick/hpl_systick.c \
$(TOP)/watch-library/shared/driver/phototransistor_driver.c \
$(TOP)/watch-library/shared/driver/thermistor_driver.c \ $(TOP)/watch-library/shared/driver/thermistor_driver.c \
$(TOP)/watch-library/shared/driver/lis2dw.c \ $(TOP)/watch-library/shared/driver/lis2dw.c \
$(TOP)/watch-library/shared/driver/spiflash.c \ $(TOP)/watch-library/shared/driver/spiflash.c \

View File

@ -96,6 +96,7 @@ SRCS += \
../watch_faces/complication/morsecalc_face.c \ ../watch_faces/complication/morsecalc_face.c \
../watch_faces/complication/rpn_calculator_face.c \ ../watch_faces/complication/rpn_calculator_face.c \
../watch_faces/complication/ships_bell_face.c \ ../watch_faces/complication/ships_bell_face.c \
../watch_faces/sensor/phototransistor_readout_face.c \
# New watch faces go above this line. # New watch faces go above this line.
# Leave this line at the bottom of the file; it has all the targets for making your project. # Leave this line at the bottom of the file; it has all the targets for making your project.

View File

@ -28,6 +28,7 @@
#include "movement_faces.h" #include "movement_faces.h"
const watch_face_t watch_faces[] = { const watch_face_t watch_faces[] = {
phototransistor_readout_face,
simple_clock_face, simple_clock_face,
world_clock_face, world_clock_face,
sunrise_sunset_face, sunrise_sunset_face,

View File

@ -74,6 +74,7 @@
#include "morsecalc_face.h" #include "morsecalc_face.h"
#include "rpn_calculator_face.h" #include "rpn_calculator_face.h"
#include "ships_bell_face.h" #include "ships_bell_face.h"
#include "phototransistor_readout_face.h"
// New includes go above this line. // New includes go above this line.
#endif // MOVEMENT_FACES_H_ #endif // MOVEMENT_FACES_H_

View File

@ -0,0 +1,104 @@
/*
* MIT License
*
* Copyright (c) 2023 Joey Castillo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include "phototransistor_driver.h"
#include "phototransistor_readout_face.h"
void phototransistor_readout_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
(void) settings;
(void) watch_face_index;
if (*context_ptr == NULL) {
*context_ptr = malloc(sizeof(phototransistor_readout_state_t));
memset(*context_ptr, 0, sizeof(phototransistor_readout_state_t));
// Do any one-time tasks in here; the inside of this conditional happens only at boot.
}
// Do any pin or peripheral setup here; this will be called whenever the watch wakes from deep sleep.
}
void phototransistor_readout_face_activate(movement_settings_t *settings, void *context) {
(void) settings;
(void) context;
phototransistor_driver_enable();
gpio_set_pin_function(A4, GPIO_PIN_FUNCTION_OFF);
gpio_set_pin_direction(A4, GPIO_DIRECTION_IN);
movement_request_tick_frequency(16);
}
bool phototransistor_readout_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
(void) settings;
(void) context;
switch (event.event_type) {
case EVENT_ACTIVATE:
// Show your initial UI here.
watch_display_string("LT", 0);
break;
case EVENT_TICK:
// If needed, update your display here.
if (watch_get_pin_level(A4)) {
watch_display_string("HI", 8);
} else {
watch_display_string("LO", 8);
}
break;
case EVENT_MODE_BUTTON_UP:
// You shouldn't need to change this case; Mode almost always moves to the next watch face.
movement_move_to_next_face();
break;
case EVENT_LIGHT_BUTTON_UP:
// If you have other uses for the Light button, you can opt not to illuminate the LED for this event.
movement_illuminate_led();
break;
case EVENT_ALARM_BUTTON_UP:
// Just in case you have need for another button.
break;
case EVENT_TIMEOUT:
// Your watch face will receive this event after a period of inactivity. If it makes sense to resign,
// you may uncomment this line to move back to the first watch face in the list:
// movement_move_to_face(0);
break;
case EVENT_LOW_ENERGY_UPDATE:
// If you did not resign in EVENT_TIMEOUT, you can use this event to update the display once a minute.
// Avoid displaying fast-updating values like seconds, since the display won't update again for 60 seconds.
// You should also consider starting the tick animation, to show the wearer that this is sleep mode:
// watch_start_tick_animation(500);
break;
default:
break;
}
// return true if the watch can enter standby mode. If you are PWM'ing an LED or buzzing the buzzer here,
// you should return false since the PWM driver does not operate in standby mode.
return true;
}
void phototransistor_readout_face_resign(movement_settings_t *settings, void *context) {
(void) settings;
(void) context;
phototransistor_driver_disable();
}

View File

@ -0,0 +1,56 @@
/*
* MIT License
*
* Copyright (c) 2023 Joey Castillo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef PHOTOTRANSISTOR_READOUT_FACE_H_
#define PHOTOTRANSISTOR_READOUT_FACE_H_
#include "movement.h"
/*
* A DESCRIPTION OF YOUR WATCH FACE
*
* and a description of how use it
*
*/
typedef struct {
// Anything you need to keep track of, put it here!
uint8_t unused;
} phototransistor_readout_state_t;
void phototransistor_readout_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
void phototransistor_readout_face_activate(movement_settings_t *settings, void *context);
bool phototransistor_readout_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
void phototransistor_readout_face_resign(movement_settings_t *settings, void *context);
#define phototransistor_readout_face ((const watch_face_t){ \
phototransistor_readout_face_setup, \
phototransistor_readout_face_activate, \
phototransistor_readout_face_loop, \
phototransistor_readout_face_resign, \
NULL, \
})
#endif // PHOTOTRANSISTOR_READOUT_FACE_H_

View File

@ -0,0 +1,59 @@
/*
* MIT License
*
* Copyright (c) 2022 Joey Castillo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "phototransistor_driver.h"
#include "watch.h"
#include "watch_utility.h"
void phototransistor_driver_enable(void) {
// Enable the ADC peripheral, which we'll use to read the phototransistor value.
watch_enable_adc();
// Enable analog circuitry on the sense pin, which is tied to the phototransistor's base.
watch_enable_analog_input(PHOTOTRANSISTOR_SENSE_PIN);
// Enable digital output on the enable pin, which is the power to the phototransistor circuit.
watch_enable_digital_output(PHOTOTRANSISTOR_ENABLE_PIN);
// and make sure it's off.
// watch_set_pin_level(PHOTOTRANSISTOR_ENABLE_PIN, !PHOTOTRANSISTOR_ENABLE_VALUE);
watch_set_pin_level(PHOTOTRANSISTOR_ENABLE_PIN, PHOTOTRANSISTOR_ENABLE_VALUE);
}
void phototransistor_driver_disable(void) {
// Disable the ADC peripheral.
watch_disable_adc();
// Disable analog circuitry on the sense pin to save power.
watch_disable_analog_input(PHOTOTRANSISTOR_SENSE_PIN);
// Disable the enable pin's output circuitry.
watch_disable_digital_output(PHOTOTRANSISTOR_ENABLE_PIN);
}
uint16_t phototransistor_driver_get_light_level(void) {
// set the enable pin to the level that powers the phototransistor circuit.
// watch_set_pin_level(PHOTOTRANSISTOR_ENABLE_PIN, PHOTOTRANSISTOR_ENABLE_VALUE);
// get the sense pin level
uint16_t value = watch_get_analog_pin_level(PHOTOTRANSISTOR_SENSE_PIN);
// and then set the enable pin to the opposite value to power down the phototransistor circuit.
// watch_set_pin_level(PHOTOTRANSISTOR_ENABLE_PIN, !PHOTOTRANSISTOR_ENABLE_VALUE);
return value;
}

View File

@ -0,0 +1,39 @@
/*
* MIT License
*
* Copyright (c) 2022 Joey Castillo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdint.h>
#ifndef PHOTOTRANSISTOR_DRIVER_H_
#define PHOTOTRANSISTOR_DRIVER_H_
#define PHOTOTRANSISTOR_SENSE_PIN (A4)
#define PHOTOTRANSISTOR_ENABLE_PIN (A0)
#define PHOTOTRANSISTOR_ENABLE_VALUE (true)
#define PHOTOTRANSISTOR_SERIES_RESISTANCE (1000.0)
void phototransistor_driver_enable(void);
void phototransistor_driver_disable(void);
uint16_t phototransistor_driver_get_light_level(void);
#endif // PHOTOTRANSISTOR_DRIVER_H_