Simple schematics and implementation:
And now some software tips. For this to work it is recommended to have pre-generated data samples, mostly because of the timing.
In this case, raw binary data of serial ASCII packets (start bit, payload, stop bit). You might make them like this:
ofstream outfile;
char * buffer = new char [55];
// Start bit (low)
for (int i = 0; i < 5; i++) {
buffer[i] = 25;
}
// What you need here in binary (low is 25, high is 230), just remember to reverse the order!
// Stop bit (hight)
for (int i = 45; i < 50; i++) {
buffer[i] = 230;
}
outfile.open ("snd.raw",ofstream::binary);
outfile.write (buffer,55);
outfile.close();
Written for C, assuming the sound sampling frequency is 48000, because then one bit is 5 samples in length.
All of that produces this nifty image on the oscilloscope (blue being Schmitt trigger output and the yellow being serial output) :
Now to use it in an Android app:
- we need direct access to the PCM stream and a buffer to generate what we want to output from the pre-generated samples:
private static final Integer minBufferSize = android.media.AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT);
private final AudioTrack oTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT, minBufferSize, AudioTrack.MODE_STREAM);
private byte[] sndBufferTrack = new byte[minBufferSize];
- next we need an array for the samples (because of the faster read access) and then to read them in:
try {
inputStream = getResources().openRawResource(R.raw.snd);
inputStream.read(sndSamples[0]);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//You'll all figure out how to do this for the rest 255 of them
- then we fill the buffer with the generated data and play it:
for (int i = 0; i < 50; i++) {
sndBufferTrack[i] = sndSamples[0][i];
}
oTrack.write(sndBufferTrack, 0, minBufferSize);
oTrack.stop();
oTrack.release();
And all of this, with right polarity, correctly generated samples and the right amount of volume finally produces consistent and mostly artefact's free data transfer: