-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulticore.cpp
85 lines (70 loc) · 2.16 KB
/
multicore.cpp
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
#include "pico/stdlib.h"
#include "pico/util/queue.h"
#include "pico/multicore.h"
#include "pico/util/queue.h"
#include <stdint.h>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <stdio.h>
#include <cstring>
typedef struct
{
int32_t (*func)(char []);
char data[50];
} queue_entry_t;
queue_t call_queue;
queue_t results_queue;
void core1_entry() {
while (1) {
// Function pointer is passed to us via the queue_entry_t which also
// contains the function parameter.
// We provide an int32_t return value by simply pushing it back on the
// return queue which also indicates the result is ready.
printf("Inside core1....\n");
queue_entry_t entry;
queue_remove_blocking(&call_queue, &entry);
std::cout << "Entry is:" << entry.data << std::endl;
int32_t (*func)(char [50]){entry.func};
int32_t result = func(entry.data);
queue_add_blocking(&results_queue, &result);
}
}
int32_t displayString(char received[50]){
std::cout << "In function: " << received << std::endl;
sleep_ms(3000);
return 0;
}
std::string rcv;
int main() {
const uint LED_PIN = 25;
int32_t res;
bool first = false;
queue_entry_t entry;
stdio_init_all();
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
printf("Starting....\n");
printf("Setup queues...\n");
//setup queue
queue_init(&call_queue, sizeof(queue_entry_t), 2);
queue_init(&results_queue, sizeof(int32_t), 2);
printf("Launching core1\n");
multicore_launch_core1(core1_entry);
while (true) {
if(first==false){
rcv = "A long string to test with..............";
first=true;
entry.func = &displayString;
strncpy(entry.data, rcv.c_str(), sizeof(entry.data));
entry.data[sizeof(entry.data) - 1] = '\0';
queue_add_blocking(&call_queue, &entry);
}
if(queue_try_remove(&results_queue, &res)){
std::cout << "rcv is now:" << rcv << std::endl;
queue_add_blocking(&call_queue, &entry);
printf("Added to queue remove\n");
}
}
}