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.
Dependencies: SoftSerial SDFileSystem mbed wave_player
Client.cpp@26:1c8a7d0bb4b7, 2017-05-17 (annotated)
- Committer:
- Thomas Cauwelier
- Date:
- Wed May 17 01:47:04 2017 +0200
- Revision:
- 26:1c8a7d0bb4b7
- Child:
- 27:6dbcad451cbc
add client logic
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 1 | // |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 2 | // Created by thoma on 17-May-17. |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 3 | // |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 4 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 5 | #include "Client.h" |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 6 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 7 | Client::Client(PinName rx_luart, int baudrate_luart) : |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 8 | aout(p18), fileSystem(p5, p6, p7, p8, "sd"), player(&aout), softSerial(NC, rx_luart), |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 9 | spi_amp(p11, p12, p13, p14) { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 10 | softSerial.baud(baudrate_luart); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 11 | spi_amp.format(8, 0); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 12 | spi_amp.frequency(1000000); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 13 | fileSystem.disk_initialize(); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 14 | wave_file = fopen("/sd/receive.wav", "w+"); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 15 | if (wave_file == NULL) { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 16 | error("Could not open file for w+\n"); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 17 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 18 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 19 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 20 | void Client::play_file() { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 21 | fflush(wave_file); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 22 | rewind(wave_file); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 23 | printf("playing_file\r\n"); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 24 | player.play(wave_file); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 25 | printf("done\r\n"); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 26 | rewind(wave_file); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 27 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 28 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 29 | void Client::set_volume() { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 30 | while (!softSerial.readable()); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 31 | int value = softSerial.getc(); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 32 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 33 | spi_amp.write(0); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 34 | spi_amp.write(int((value / 100.0) * 255.0)); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 35 | printf("volume set to %i\r\n", value); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 36 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 37 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 38 | void Client::run() { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 39 | while (true) { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 40 | handle_type(); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 41 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 42 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 43 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 44 | void Client::handle_type() { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 45 | while (!softSerial.readable()); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 46 | int packet_type = softSerial.getc(); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 47 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 48 | switch (packet_type) { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 49 | case SET_VOLUME: |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 50 | set_volume(); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 51 | break; |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 52 | case SEND_FILE: |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 53 | send_file(); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 54 | break; |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 55 | default: |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 56 | break; |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 57 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 58 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 59 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 60 | void Client::send_file() { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 61 | // get file size |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 62 | int size = 0; |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 63 | for (int i = 0; i < 4; ++i) { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 64 | while (!softSerial.readable()); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 65 | int b = softSerial.getc(); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 66 | while (!softSerial.writeable()); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 67 | softSerial.putc(b); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 68 | size |= b << ((3 - i) << 3); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 69 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 70 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 71 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 72 | // send data |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 73 | int j = 0; |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 74 | while (j < size) { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 75 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 76 | // get max 64 bytes |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 77 | int k = 0; |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 78 | for (k; k < 64; ++k) { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 79 | if (j == size) { |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 80 | break; |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 81 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 82 | while (!softSerial.readable()); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 83 | int b = softSerial.getc(); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 84 | buffer[k] = (char) b; |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 85 | j++; |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 86 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 87 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 88 | // store max 64 bytes |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 89 | fwrite(buffer, sizeof(char), (size_t) k, wave_file); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 90 | } |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 91 | |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 92 | play_file(); |
Thomas Cauwelier |
26:1c8a7d0bb4b7 | 93 | } |