diff --git a/clockclock/.vscode/settings.json b/clockclock/.vscode/settings.json new file mode 100644 index 0000000..35433ec --- /dev/null +++ b/clockclock/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "functional": "cpp", + "tuple": "cpp" + } +} \ No newline at end of file diff --git a/clockclock/include/board.h b/clockclock/include/board.h index 9787fb3..6a3bf56 100644 --- a/clockclock/include/board.h +++ b/clockclock/include/board.h @@ -1,5 +1,7 @@ #pragma once +#include "Arduino.h" + #ifndef HARDWAREVERSION #define HARDWAREVERSION 11 #endif @@ -36,7 +38,7 @@ #define MOT_DIR1 16 #define MOT_RST1 18 - #define MOT_VREF 25 + #define MOT_VREF A18 //25 #define MOT_EN 13 #define ADDR0 33 @@ -49,7 +51,7 @@ #define PROX_LED 27 #define LED_DATA 26 - #define PROOX_IN 36 + #define PROX_IN 36 #define MCU_BUT_MIN 19 #define MCU_BUT_PLUS 23 @@ -57,3 +59,5 @@ #warning NO BOARD SPECIFIED #endif +void initBoard( void ); + diff --git a/clockclock/platformio.ini b/clockclock/platformio.ini index 083a7bd..b7f1fba 100644 --- a/clockclock/platformio.ini +++ b/clockclock/platformio.ini @@ -13,6 +13,9 @@ framework = arduino lib_ldf_mode = deep+ lib_deps = http://192.168.2.3/Bonobo.Git.Server/Accelstepper.git + Fastled/FastLED @ ^3.4.0 + makuna/NeoPixelBus @ ^2.6.2 + ; [env:clockclock_hwV10_L422] ;platform = ststm32 diff --git a/clockclock/src/Proximity.h b/clockclock/src/Proximity.h new file mode 100644 index 0000000..5e440d5 --- /dev/null +++ b/clockclock/src/Proximity.h @@ -0,0 +1,12 @@ +#pragma once + +#include "board.h" +#include "Arduino.h" + +#define PROXIMITYINTERVAL 100 +#define SAMPLERATE 10 +#define ANALOGSAMPLES 30 + +void initProximity( void ); +void handleProximity( void ); +void getProximity( void ); \ No newline at end of file diff --git a/clockclock/src/board.cpp b/clockclock/src/board.cpp new file mode 100644 index 0000000..3a911fb --- /dev/null +++ b/clockclock/src/board.cpp @@ -0,0 +1,6 @@ +#include "board.h" + +void initBoard( void ) +{ + Serial.begin(115200); +} \ No newline at end of file diff --git a/clockclock/src/buttons.cpp b/clockclock/src/buttons.cpp index e4d413c..d5a0e46 100644 --- a/clockclock/src/buttons.cpp +++ b/clockclock/src/buttons.cpp @@ -25,3 +25,5 @@ void initButtons(void) } + + diff --git a/clockclock/src/comms.cpp b/clockclock/src/comms.cpp index 8861692..f246be7 100644 --- a/clockclock/src/comms.cpp +++ b/clockclock/src/comms.cpp @@ -6,12 +6,12 @@ uint8_t getAddress(void) { - uint8_t address = 0; + uint8_t address = COMMSBASE; - address += (digitalRead(ADDR0)) ? 1 : 0; - address += (digitalRead(ADDR1)) ? 2 : 0; - address += (digitalRead(ADDR2)) ? 4 : 0; - address += (digitalRead(ADDR3)) ? 8 : 0; + address += (!digitalRead(ADDR0)) ? 1 : 0; + address += (!digitalRead(ADDR1)) ? 2 : 0; + address += (!digitalRead(ADDR2)) ? 4 : 0; + address += (!digitalRead(ADDR3)) ? 8 : 0; return address; } @@ -27,23 +27,25 @@ void requestI2CEvent() { uint8_t address; -void initI2C( void) +void initComms( void) { + Serial.print("init Comms"); pinMode(ADDR0, INPUT_PULLUP); pinMode(ADDR1, INPUT_PULLUP); pinMode(ADDR2, INPUT_PULLUP); pinMode(ADDR3, INPUT_PULLUP); - + delay(10); address = getAddress(); - + Serial.printf("selected address: %u", address); // Start the I2C Bus as Slave on address 9 Wire.begin(address); // Attach a function to trigger when something is received. - Wire.onReceive(receiveI2CEvent); - Wire.onRequest(requestI2CEvent); + //Wire.onReceive(receiveI2CEvent); + //Wire.onRequest(requestI2CEvent); + Serial.println(" :Ok"); } -void handleI2C ( void ) +void handleComms ( void ) { } diff --git a/clockclock/src/comms.h b/clockclock/src/comms.h index f9af4a3..4d40572 100644 --- a/clockclock/src/comms.h +++ b/clockclock/src/comms.h @@ -3,6 +3,7 @@ #include "Arduino.h" #define MSGLEN 10 +#define COMMSBASE 80 class c_message { @@ -24,5 +25,5 @@ public: }; -void initI2C( void); -void handleI2C ( void ); +void initComms( void); +void handleComms ( void ); diff --git a/clockclock/src/led.cpp b/clockclock/src/led.cpp new file mode 100644 index 0000000..5f7d5b5 --- /dev/null +++ b/clockclock/src/led.cpp @@ -0,0 +1,44 @@ +#include "led.h" + +//CRGB leds[NUM_LEDS]; + + +uint64_t ledtimer1 = 0; +uint8_t hue = 0; +bool ledstate = 0; +#define colorSaturation 128 + +NeoPixelBus LEDS(NUM_LEDS, LED_DATA); + +RgbColor red(colorSaturation, 0, 0); +RgbColor green(0, colorSaturation, 0); +RgbColor blue(0, 0, colorSaturation); +RgbColor white(colorSaturation); +RgbColor black(0); + + + +/* ---------------- */ +/* public functions */ +/* ---------------- */ + +void initLeds(void) +{ + Serial.print("Init Leds: "); + LEDS.begin(); + LEDS.ClearTo(black); + + Serial.println("OK"); +} + +void handleLeds(void) +{ + uint64_t currentmillis = millis(); + if (currentmillis - ledtimer1 > LEDINTERVAL) + { + + + ledtimer1 = currentmillis; + + } +} \ No newline at end of file diff --git a/clockclock/src/led.h b/clockclock/src/led.h new file mode 100644 index 0000000..8c53c29 --- /dev/null +++ b/clockclock/src/led.h @@ -0,0 +1,11 @@ +#pragma once + +#include "board.h" +#include +#include + +#define NUM_LEDS 4 +#define LEDINTERVAL 10 + +void initLeds(void); +void handleLeds(void); \ No newline at end of file diff --git a/clockclock/src/main.cpp b/clockclock/src/main.cpp index 66e3a59..066b6ac 100644 --- a/clockclock/src/main.cpp +++ b/clockclock/src/main.cpp @@ -3,12 +3,20 @@ #include "motor.h" #include "buttons.h" #include "movement.h" +#include "Proximity.h" +#include "led.h" +#include "comms.h" void setup() { // put your setup code here, to run once: + initBoard(); initMotors(); initButtons(); initMovement(); + initProximity(); + initLeds(); + initComms(); + } void loop() { @@ -16,4 +24,7 @@ void loop() { handleMotors(); handleButtons(); handleMovement(); + handleProximity(); + handleLeds(); + handleComms(); } \ No newline at end of file diff --git a/clockclock/src/motor.cpp b/clockclock/src/motor.cpp index 4be165a..9400b45 100644 --- a/clockclock/src/motor.cpp +++ b/clockclock/src/motor.cpp @@ -11,6 +11,7 @@ MultiStepper motors; void c_motor::init(bool setvref) { + Serial.print("init Motors: "); pinMode(_rst, OUTPUT); pinMode(_en, OUTPUT); pinMode(_sclk, OUTPUT); @@ -27,9 +28,13 @@ void c_motor::init(bool setvref) if (setvref) { - pinMode(MOT_VREF, OUTPUT); + Serial.print("Setup Vref: "); + ledcSetup(0,12800,8); + //pinMode(MOT_VREF, OUTPUT); + ledcAttachPin(MOT_VREF,0); ledcWrite(MOT_VREF, 32); } + Serial.println("OK"); } void c_motor::begin(void) diff --git a/clockclock/src/proximity.cpp b/clockclock/src/proximity.cpp new file mode 100644 index 0000000..20cbf77 --- /dev/null +++ b/clockclock/src/proximity.cpp @@ -0,0 +1,70 @@ +#include "Proximity.h" + +uint64_t proximityTimer = 0; +uint64_t sampleTimer = 0; +uint16_t readbuffer[ANALOGSAMPLES]; +uint16_t readbuffer_idx = 0; +bool firstOutputSampleReady = false; +uint16_t currentProx = 0; + +uint32_t addSample(void) +{ + uint64_t currentmillis = millis(); + if (currentmillis - sampleTimer > SAMPLERATE) + { + sampleTimer = currentmillis; + if (readbuffer_idx > ANALOGSAMPLES - 1) + { + readbuffer_idx = 0; + firstOutputSampleReady = true; + } + digitalWrite(PROX_LED, HIGH); + delay(3); + readbuffer[readbuffer_idx++] = analogRead(PROX_IN); + + digitalWrite(PROX_LED, LOW); + + if (firstOutputSampleReady) + { + //average samples + uint64_t sampleAvg = 0; + for (int i = 0; i < ANALOGSAMPLES; i++) + { + sampleAvg += readbuffer[i]; + } + //Serial.printf("AnalogRead_sum(%l)\n", sampleAvg); + sampleAvg /= ANALOGSAMPLES; + currentProx = sampleAvg; + return sampleAvg; + } + } + return readbuffer[readbuffer_idx - 1]; +} + +void initProximity(void) +{ + Serial.print("Proximity init:"); + pinMode(PROX_LED, OUTPUT); + pinMode(PROX_IN, ANALOG); + + for (auto sample : readbuffer) + { + sample = 0; + } + Serial.println(" OK "); +} + +void handleProximity(void) +{ + uint64_t currentmillis = millis(); + if (currentmillis - proximityTimer > PROXIMITYINTERVAL) + { + //Serial.printf("proximity raw read = %u\n", currentProx); + proximityTimer = currentmillis; + } + + addSample(); +} +void getProximity(void) +{ +} \ No newline at end of file