diff --git a/main/watchMain.cpp b/main/watchMain.cpp index 12a1365..d61a852 100644 --- a/main/watchMain.cpp +++ b/main/watchMain.cpp @@ -32,14 +32,14 @@ void app_main(void); } struct DeviceState { - uint8_t minJumps; - uint8_t maxJumps; - uint8_t profile; - uint8_t restTime; + uint8_t minJumps{6}; + uint8_t maxJumps{32}; + uint8_t profile{}; + uint8_t restTime{}; float speed = 1.0; // Rad/sec float accel = 1.0; // Rad/sec^2 - Color color{16, 16, 16}; - uint8_t pictogram; + Color color{0x80, 0x80, 0x80}; + uint8_t pictogram{}; }; enum class MoveType { @@ -51,7 +51,12 @@ struct MoveCommand { MoveType type; uint8_t pos; }; +struct PictoIllumCommand { + uint8_t pos; // 0 for none / Valid indexes 1 .. 6 +}; + static fos::Queue commandQueue(8); +static fos::Queue pictoIllumQue(2); DeviceState activeState; static const char *TAG = "Watch"; @@ -83,6 +88,19 @@ void getConfig(Cli::CArgs args) { pShell->write(key + ": " + val + "\n"); } +void testPictoIllum(Cli::CArgs args) { + if (args.size() != 1) { + pShell->write("Needs position argument for illuminated position 0 for off, 1..6 for pictograms'\n"); + return; + } + try { + int p = std::stoi(args[0]); + pictoIllumQue.enqueue(std::make_unique(p)); + } catch (const std::exception &e) { + ESP_LOGI(TAG, "Failed to parse %s and %s", args[0].c_str(), args[1].c_str()); + } +} + void setConfig(Cli::CArgs args) { if (args.size() != 2) { pShell->write("Needs string key and value'\n"); @@ -180,6 +198,10 @@ static void telnetSocketTask(int addrFamily) { pShell->addBinding( {"move", "start movement (profile-idx,steps) as integers ", [](Cli::CArgs a) { triggerMovement(a); }}); + + pShell->addBinding( + {"tstIllum", "test illumination command 0=off /1..6", + [](Cli::CArgs a) { testPictoIllum(a); }}); }, [](TcpSession &session) { pShell->process(); @@ -233,13 +255,10 @@ static void UniverseCallback(const uint8_t *data, const uint16_t size) { } void moveToSubs(std::string data) { - // Syslog::logv(Syslog::Severity::Notice, - // "Running Stepper by %i steps with prof=%i a=%.2f w=%.2f", - // delta, profile, activeState.accel, activeState.speed); - ESP_LOGI(TAG, "MoveTo command '%s'", data.c_str()); try { int p = std::stoi(data); + Syslog::logv(Syslog::Severity::Informational, "got MoveTo-Cmd->%i", p); commandQueue.enqueue(std::make_unique(MoveType::StepTo, p)); auto &client = MqttClient::getInstance(); client.publish("watch/inPosition", data + "Picto"); @@ -252,6 +271,7 @@ void startShuffle(std::string data) { ESP_LOGI(TAG, "StartShuffle command %s", data.c_str()); try { + Syslog::logv(Syslog::Severity::Informational, "got Shuffle-Command"); commandQueue.enqueue(std::make_unique(MoveType::Shuffle, 0)); auto &client = MqttClient::getInstance(); client.publish("watch/changingPosition", "...."); @@ -419,7 +439,7 @@ void LightingTask() { // static PmwExpanderPca9685 pca9685(master); static PmwExpanderPca9685 ledDriver1(master, /*iverted=*/false); static PmwExpanderPca9685 ledDriver2(master, /*iverted=*/false, 0x41); - PmwExpanderPca9685::GrayValues values[2]; + PmwExpanderPca9685::GrayValues values[2]{}; OutputPort pwmEnable(GPIO_NUM_1, GpioPort::Logic::Inverted); pwmEnable.write(true); @@ -432,35 +452,62 @@ void LightingTask() { Effect::EffectProcessor(8), Effect::EffectProcessor(8), Effect::EffectProcessor(8)}; - processors[0].setEffect(Effect::macDemoStart, &colorSource, 16); - processors[1].setEffect(Effect::macDemoStart, &colorSource, 32); - processors[2].setEffect(Effect::macDemoPulse, &colorSource, 48); - processors[3].setEffect(Effect::macDemoPulse, &colorSource, 64); - processors[4].setEffect(Effect::macStartFlicker, &colorSource, 80); - processors[5].setEffect(Effect::macStartIdle, &colorSource, 96); + processors[0].setEffect(Effect::macDark, &colorSource, 0); + processors[1].setEffect(Effect::macDark, &colorSource, 0); + processors[2].setEffect(Effect::macDark, &colorSource, 0); + processors[3].setEffect(Effect::macDark, &colorSource, 0); + processors[4].setEffect(Effect::macDark, &colorSource, 0); + processors[5].setEffect(Effect::macDark, &colorSource, 0); - // processors[1].setEffect(Effect::macDark, &colorSource, 32); - // processors[2].setEffect(Effect::macDark, &colorSource, 48); - // processors[3].setEffect(Effect::macDark, &colorSource, 64); - // processors[4].setEffect(Effect::macDark, &colorSource, 80); - // processors[1].setEffect(Effect::macDemoStart, &colorSource, 32); - // processors[2].setEffect(Effect::macDemoPulse, &colorSource, 48); - // processors[3].setEffect(Effect::macDemoPulse, &colorSource, 64); - // processors[4].setEffect(Effect::macStartFlicker, &colorSource, 80); + const std::array PictoEffects{ + Effect::macDemoPulse, + Effect::macDemoPulse, + Effect::macDemoPulse, + Effect::macDemoPulse, + Effect::macDemoPulse, + Effect::macStartFlicker}; + + uint8_t lastPicto = 0; while (true) { - values[0].fill(0); - values[1].fill(0); + auto onPos = pictoIllumQue.dequeue(0); + if (onPos) { + ESP_LOGI(TAG, "Changing picto-Illum to pos = %i", onPos->pos); + ESP_LOGI(TAG, "Current color set r%i g%i b%i ", + activeState.color.getRed(), activeState.color.getGreen(), activeState.color.getBlue()); + auto pos = onPos->pos; + if (pos == 0) { + Syslog::logv(Syslog::Severity::Informational, + "Dimming picto-illum pos:%i ", lastPicto); + processors[lastPicto-1].setEffect(Effect::macDark, &colorSource, 0); + } else if ((pos >= 1) && (pos <= 6)) { + auto idx = pos - 1; + Syslog::logv(Syslog::Severity::Informational, + "Firing picto-illum pos:%i ", pos); + processors[idx].setEffect(PictoEffects[idx], &colorSource, 0); + lastPicto = pos; + } else { + Syslog::logv(Syslog::Severity::Error, + "Got invalid index for illuminated pictogram =%i ", pos); + } + } - // values[x][4] = 0x200; // Actually first - values[0][4] = activeState.color.getRed() * 257; - values[0][5] = activeState.color.getGreen() * 257; - values[0][6] = activeState.color.getBlue() * 257; - - // values[1][4] = 0x100; // Actually first - values[1][4] = activeState.color.getRed() * 257; - values[1][5] = activeState.color.getGreen() * 257; - values[1][6] = activeState.color.getBlue() * 257; + std::array startPoints{ + &(values[0][4]), + &(values[0][8]), + &(values[0][12]), + &(values[1][4]), + &(values[1][8]), + &(values[1][12]), + }; + for (size_t i = 0; i < processors.size(); i++) { + auto col = processors[i].tick(); + auto sp = startPoints[i]; + sp[0] = (uint16_t)(col.getWhite()) << 7; + sp[1] = (uint16_t)(col.getBlue()) << 7; + sp[2] = (uint16_t)(col.getGreen()) << 7; + sp[3] = (uint16_t)(col.getRed()) << 7; + } ledDriver1.sendImage(values[0]); ledDriver2.sendImage(values[1]); @@ -495,6 +542,8 @@ void MovementTask() { ESP_LOGI(TAG, "Fist Position is %f.2", stepper.getScaledPos() * 6); fos::Task::delayMs(1000); + activeState.pictogram = 0; + while (true) { ESP_LOGD(TAG, "Waiting for command"); auto cmd = commandQueue.dequeue(); @@ -537,17 +586,6 @@ void MovementTask() { } } -void IoTest() { - ESP_LOGI(TAG, "Start IO"); - InputPort in2(GPIO_NUM_19); - OutputPort nReset(GPIO_NUM_9); - - while (true) { - nReset.write(in2.read()); - fos::Task::delayMs(100); - } -} - void app_main(void) { esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { @@ -557,9 +595,8 @@ void app_main(void) { ESP_ERROR_CHECK(ret); initialize_ethernet(); NvsApi::init(); - // static fos::Task lightingTask(LightingTask, "Lights", 0x4000uL); + static fos::Task lightingTask(LightingTask, "Lights", 0x4000uL); // static fos::Task movementTask(MovementTask, "Motor", 0x4000uL); - // static fos::Task ioTask(IoTest, "IO", 0x4000uL); etherActive.take(); ESP_LOGI(TAG, "Start net services");