From faefaed63cb9bb430216e1f418cba08c72020036 Mon Sep 17 00:00:00 2001 From: Tristan Krause Date: Thu, 16 May 2019 11:03:49 +0200 Subject: [PATCH] nullpointer check added --- control/examples/pegel/Makefile | 2 +- control/examples/pegel/main.cpp | 11 ++++------ control/src/drv/b15f.cpp | 38 ++++++++++++++++++++++++++------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/control/examples/pegel/Makefile b/control/examples/pegel/Makefile index fc23b62..8000d6e 100644 --- a/control/examples/pegel/Makefile +++ b/control/examples/pegel/Makefile @@ -24,7 +24,7 @@ help: clean: @echo "Cleaning..." - rm -f $(OBJECTS) $(OUT) + rm -f $(OBJECTS) $(OUT) *.bin gnuplotscript.gp .cpp.o: $(COMPILE) -c $< -o $@ diff --git a/control/examples/pegel/main.cpp b/control/examples/pegel/main.cpp index e3eb666..960b8ad 100644 --- a/control/examples/pegel/main.cpp +++ b/control/examples/pegel/main.cpp @@ -3,7 +3,7 @@ #include #include -constexpr char * PLOT_FILE = "plot.bin"; +const char PLOT_FILE[] = "plot.bin"; int main() { @@ -12,7 +12,6 @@ int main() PlottyFile pf; uint16_t ba[1024]; - uint16_t bb[1024]; const uint16_t sample_count = 1024; const uint16_t delta = 1; @@ -31,14 +30,12 @@ int main() uint8_t curve = 0; - drv.analogSequence(0, &ba[0], 0, 1, &bb[0], 0, 0, delta, sample_count); + drv.analogSequence(0, &ba[0], 0, 1, nullptr, 0, 0, delta, sample_count); for(uint16_t x = 0; x < sample_count * delta; x += delta) { - drv.analogWrite0(x); - uint16_t y = drv.analogRead(0); - std::cout << x << " - " << y << std::endl; - pf.addDot(Dot(x, y, curve)); + std::cout << x << " - " << ba[x] << std::endl; + pf.addDot(Dot(x, ba[x], curve)); } // speichern und plotty starten diff --git a/control/src/drv/b15f.cpp b/control/src/drv/b15f.cpp index 3d5de24..22937e9 100644 --- a/control/src/drv/b15f.cpp +++ b/control/src/drv/b15f.cpp @@ -240,8 +240,12 @@ uint16_t B15F::analogRead(uint8_t channel) void B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count) { - buffer_a += offset_a; - buffer_b += offset_b; + // check pointers + if(buffer_a) + buffer_a += offset_a; + if(buffer_b) + buffer_b += offset_b; + usart.clearInputBuffer(); usart.writeByte(RQ_ADC_DAC_STROKE); @@ -249,15 +253,33 @@ void B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset usart.writeByte(channel_b); usart.writeInt(start); usart.writeInt(static_cast(delta)); - usart.writeInt(count); - + usart.writeInt(count); for(uint16_t i = 0; i < count; i++) { - buffer_a[i] = usart.readInt(); - buffer_b[i] = usart.readInt(); - if(buffer_a[i] > 1023 || buffer_b[i] > 1023) - abort("Bad ADC data detected (2)"); + if(buffer_a) + { + buffer_a[i] = usart.readInt(); + + if(buffer_a[i] > 1023) // check for broken usart connection + abort("Bad ADC data detected (2)"); + } + else + { + usart.readInt(); + } + + if(buffer_b) + { + buffer_b[i] = usart.readInt(); + + if(buffer_b[i] > 1023) // check for broken usart connection + abort("Bad ADC data detected (3)"); + } + else + { + usart.readInt(); + } } uint8_t aw = usart.readByte();