Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 3:f04c8a88a27d
- Parent:
- 2:4857a7cbb9da
- Child:
- 4:42b85520dca6
--- a/main.cpp Wed Jan 23 17:56:08 2019 +0000
+++ b/main.cpp Mon Feb 04 16:11:54 2019 +0000
@@ -2,6 +2,10 @@
// -----------------------
// Periodically write to the AnalogOut to create a sine wave
// Alternate between two fixed frequencies every 30 sec
+//
+// This code uses the Ticker API to create a periodic interrupt
+// Devices used in this way must not call locks; a variant of
+// the AnalogOut device w/o locking is created for this
//
// THIS VERSION HAS DEBUGGING CODE USING THE SERIAL PORT
@@ -10,11 +14,25 @@
#include "mbed.h"
#include "sineTable.h"
+// --------------------------
+// This declaration introduces a derived version of the mbed AnalogOut
+// class with the locking removed.
+// You do NOT NEED TO UNDERSTAND this
+class AnalogOut_unsafe : public AnalogOut {
+ public:
+ AnalogOut_unsafe (PinName pin) : AnalogOut (pin) {}
+ protected:
+ virtual void lock() {}
+ virtual void unlock() {}
+};
+// --------------------------
+
Ticker tick ; // Creates periodic interrupt
-AnalogOut ao(PTE30) ; // Analog output
+AnalogOut_unsafe ao(PTE30) ; // Analog output
// --- following code for debugging ---
Thread debugT ;
+EventFlags event_flags;
Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
// Put a simple representation of the sine wave
@@ -24,14 +42,14 @@
pc.putc('*') ;
while (sine4--) pc.putc('*') ;
pc.putc('\n') ;
- //pc.putc('\r') ;
+ pc.putc('\r') ; // depends on the OS being used
}
volatile int index = 0 ; // this variable is not just for debugging!!
void debugCallback() {
while (true) {
- Thread::signal_wait(0x1) ;
+ event_flags.wait_any(0x1) ;
debug(index) ; // there is a race condition here
}
}
@@ -41,7 +59,7 @@
// Write new value to AnalogOut
void writeAout() {
ao.write_u16(sine[index]) ;
- debugT.signal_set(0x1) ; // DEBUGGING low frequency only
+ event_flags.set(0x1) ; // DEBUGGING low frequency only
index = (index + 1) % 64 ;
}