From 546fb2d9dce879b79855af50379fb0cdbba46551 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 9 Jul 2020 11:00:08 -0400 Subject: [PATCH] fixed command receiving code --- source/main.cpp | 146 ++++++++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 72 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index b73776a..95211b4 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,72 +1,74 @@ -/** - * @file main.cpp - * @brief Program entry point. - * - * Copyright (C) 2020 Clyne Sullivan - * - * Distributed under the GNU GPL v3 or later. You should have received a copy of - * the GNU General Public License along with this program. - * If not, see . - */ - -#include "ch.h" -#include "hal.h" - -#include "adc.hpp" -#include "dac.hpp" -#include "usbserial.hpp" - -#include - -static_assert(sizeof(adcsample_t) == sizeof(uint16_t)); - -#if CACHE_LINE_SIZE > 0 -CC_ALIGN(CACHE_LINE_SIZE) -#endif -static std::array adc_samples; - -int main() -{ - halInit(); - chSysInit(); - - palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // LED - - ADCd adc (ADCD1, GPTD4); - adc.start(); - - //DACd dac (DACD1, { - // .init = 0, - // .datamode = DAC_DHRM_12BIT_RIGHT, - // .cr = 0 - //}); - //dac.start(); - //dac.write(0, 1024); - - USBSeriald usbd (SDU1); - usbd.start(); - - while (true) { - if (usbd.active()) { - // Expect to receive a byte command 'packet'. - if (char cmd[3]; usbd.read(&cmd, 3) > 0) { - switch (cmd[0]) { - case 'r': // Read in analog signal - if (auto count = std::min(static_cast(cmd[1] | (cmd[2] << 8)), adc_samples.size()); count > 0) { - adc.getSamples(&adc_samples[0], count); - usbd.write(adc_samples.data(), count * sizeof(adcsample_t)); - } - break; - case 'i': // Identify ourself as an stmdsp device - usbd.write("stmdsp", 6); - break; - default: - break; - } - } - } - - chThdSleepMilliseconds(1); - } -} - +/** + * @file main.cpp + * @brief Program entry point. + * + * Copyright (C) 2020 Clyne Sullivan + * + * Distributed under the GNU GPL v3 or later. You should have received a copy of + * the GNU General Public License along with this program. + * If not, see . + */ + +#include "ch.h" +#include "hal.h" + +#include "adc.hpp" +#include "dac.hpp" +#include "usbserial.hpp" + +#include + +static_assert(sizeof(adcsample_t) == sizeof(uint16_t)); + +#if CACHE_LINE_SIZE > 0 +CC_ALIGN(CACHE_LINE_SIZE) +#endif +static std::array adc_samples; + +int main() +{ + halInit(); + chSysInit(); + + palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // LED + + ADCd adc (ADCD1, GPTD4); + adc.start(); + + //DACd dac (DACD1, { + // .init = 0, + // .datamode = DAC_DHRM_12BIT_RIGHT, + // .cr = 0 + //}); + //dac.start(); + //dac.write(0, 1024); + + USBSeriald usbd (SDU1); + usbd.start(); + + while (true) { + if (usbd.active()) { + // Expect to receive a byte command 'packet'. + if (char cmd[3]; usbd.read(&cmd, 1) > 0) { + switch (cmd[0]) { + case 'r': // Read in analog signal + if (usbd.read(&cmd[1], 2) < 2) + break; + if (auto count = std::min(static_cast(cmd[1] | (cmd[2] << 8)), adc_samples.size()); count > 0) { + adc.getSamples(&adc_samples[0], count); + usbd.write(adc_samples.data(), count * sizeof(adcsample_t)); + } + break; + case 'i': // Identify ourself as an stmdsp device + usbd.write("stmdsp", 6); + break; + default: + break; + } + } + } + + chThdSleepMilliseconds(1); + } +} +