Home > Enterprise >  How to create a thread in mbed?
How to create a thread in mbed?

Time:12-19

Use the joystick to control the mousecursor. Use the LCD to read the temperature from the temeperature sensor. By running into 2 single threads.

/*
 * Copyright (c) 2006-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"
#include "USBMouse.h"
#include "LM75B.h"
#include "C12832.h"

Thread thread;
Thread thread1;
//Thread thread2;
//Thread thread3;
//Thread thread4;

USBMouse mouse;
// x and y axis of the joystick
AnalogIn   ainx(A0);
AnalogIn   ainy(A1);
BusIn*input = new BusIn(p15,p12,p13,p16);
int16_t x;
int16_t y;

C12832 lcd(p5, p7, p6, p8, p11);
 
LM75B sensor(p28,p27);
Serial pc(USBTX,USBRX);

void MouseCursor() {
   while (1){int state = input->read();  // get current state from input
    x = 0;
    y = 0;
    
    switch(state & input->mask()) { //mask bits to test
    case 0x0:
        // do nothing - stick neutral position
        break;
    case 0x1:
        // stick down (relative to LCD screen)
        y = -1;
        break;
    case 0x2:
        // stick up
        y = 1;
        break;
    case 0x4:
        // stick left
        x = -1;
        break;
    case 0x8:
        // stick right
        x = 1;
    }
        // moves mouse to specified x, y coordinates on screen
        mouse.move(x, y);
        wait_us(500);
    } 
}
void TemperatureSensor(){
    //Try to open the LM75B
    if (sensor.open()) {
        printf("Device detected!\n");
 
        while (1) {
            lcd.cls();
            lcd.locate(0,3);
            lcd.printf("Temp = %.3f\n", (float)sensor);
            wait(1.0);
        }
 
    } else {
        error("Device not detected!\n");
    }
}

int main() {
   thread.start(MouseCursor);
   thread1.start(TemperatureSensor);
}

The error info is: Error: Unknown type name 'Serial' in "main.cpp", Line: 27, Col: 1

These are the related links: https://os.mbed.com/docs/mbed-os/v6.15/apis/usbmouse.html , https://os.mbed.com/docs/mbed-os/v6.15/program-setup/concepts.html , https://os.mbed.com/cookbook/mbed-application-board#11-lm75b-temperature-sensor .

CodePudding user response:

When exporting the project from the MBED Online Compiler, add the mbed_config.h file generated by mbed to the device.h file of the target MCU as follows. :

Add the following declaration to the device.h file of the target MCU:

#include "mbed_config.h"

Below is the directory structure of the related files in the MBED Online Compiler output:

output_folder
  |
  |------ "mbed_config.h"
  |
  |------ mbed/
            |
            |------ {TARGET-PlATFORM}/
                      |
                      |------ {TOOLCHAIN}/
                                |
                                |------ "device.h"

CodePudding user response:

Delete the line:

Serial pc(USBTX,USBRX);

The variable pc is not used anywhere, and the reference to Serial is redundant.

  • Related