aboutsummaryrefslogtreecommitdiffstats
path: root/gui/stmdsp.cpp
blob: 633c61ac3845754d8835484582dfcb4155dbf50c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "stmdsp.hpp"

#include <serial/serial.h>

namespace stmdsp
{
    std::list<std::string>& scanner::scan()
    {
        auto devices = serial::list_ports();
        for (auto& device : devices) {
            if (device.hardware_id.find(STMDSP_USB_ID) != std::string::npos)
                m_available_devices.emplace_front(device.port);
        }

        return m_available_devices;
    }

    device::device(const std::string& file) :
        m_serial(file, 1000000/*230400*/, serial::Timeout::simpleTimeout(50))
    {
        if (m_serial.isOpen()) {
           m_serial.write("i");
           if (m_serial.read(6) != "stmdsp")
               m_serial.close();
        }
    }

    /*std::vector<adcsample_t> device::sample(unsigned long int count) {
        if (connected()) {
            uint8_t request[3] = {
                'd',
                static_cast<uint8_t>(count),
                static_cast<uint8_t>(count >> 8)
            };
            m_serial.write(request, 3);
            std::vector<adcsample_t> data (count);
            m_serial.read(reinterpret_cast<uint8_t *>(data.data()), data.size() * sizeof(adcsample_t));
            return data;
        } else {
            return {};
        }
    }*/

    void device::continuous_set_buffer_size(unsigned int size) {
        if (connected()) {
            uint8_t request[3] = {
                'B',
                static_cast<uint8_t>(size),
                static_cast<uint8_t>(size >> 8)
            };
            m_serial.write(request, 3);
        }
    }

    void device::continuous_start() {
        if (connected())
            m_serial.write("R");
    }

    void device::continuous_start_measure() {
        if (connected())
            m_serial.write("M");
    }

    uint32_t device::continuous_start_get_measurement() {
        uint32_t count = 0;
        if (connected()) {
            m_serial.write("m");
            m_serial.read(reinterpret_cast<uint8_t *>(&count), sizeof(uint32_t));
        }

        return count / 2;
    }

    std::vector<adcsample_t> device::continuous_read() {
        if (connected()) {
            m_serial.write("a");
            std::vector<adcsample_t> data (2048);
            m_serial.read(reinterpret_cast<uint8_t *>(data.data()), 2048 * sizeof(adcsample_t));
            return data;
        }

        return {};
    }

    void device::continuous_stop() {
        if (connected())
            m_serial.write("S");
    }

    void device::siggen_upload(dacsample_t *buffer, unsigned int size) {
        if (connected()) {
            uint8_t request[3] = {
                'D',
                static_cast<uint8_t>(size),
                static_cast<uint8_t>(size >> 8)
            };
            m_serial.write(request, 3);

            m_serial.write((uint8_t *)buffer, size * sizeof(dacsample_t));
        }
    }

    void device::siggen_start() {
        if (connected())
            m_serial.write("W");
    }

    void device::siggen_stop() {
        if (connected())
            m_serial.write("w");
    }

    void device::upload_filter(unsigned char *buffer, size_t size) {
        if (connected()) {
            uint8_t request[3] = {
                'E',
                static_cast<uint8_t>(size),
                static_cast<uint8_t>(size >> 8)
            };
            m_serial.write(request, 3);

            m_serial.write(buffer, size);
        }
    }

    void device::unload_filter() {
        if (connected())
            m_serial.write("e");
    }
}