Simple radar simulator. Example of 2D graphics on DISCO-F746NG display.

Dependencies:   BSP_DISCO_F746NG Graphics mbed TS_DISCO_F746NG

RadarDemo/RadarDemo.cpp

Committer:
karpent
Date:
2016-11-04
Revision:
2:8db224cc1fcb
Parent:
1:5e49b46de1b0
Child:
3:732f7144ec81

File content as of revision 2:8db224cc1fcb:

//
// RadarDemo.cpp - example of graphic commands usage to create 2D graphics.
//

#include "RadarDemo.h"
#include "RK043FN48H.h"

// Size of range control areas on the screen
const int raSize = 50;

RadarDemo::RadarDemo(Display* display) : Radar(display)
{
}


RadarDemo::~RadarDemo()
{
#ifndef _SDL_timer_h
    t.stop();
#endif
    Radar::~Radar();
}


void RadarDemo::Initialize()
{
    // Set scan perion for 6 seconds
    scanPeriod = 6000;

    // Run forever, to set limited time for the demo
    // use for example: demoTime = 10 * scanPeriod;
    demoTime = 0;

    // Put a number of sample targets on the display
    AddSampleTargets(30);

    // Use medium range, values 0..2 are available now
    SetRange(1);

    // Remark : Data member initializer is not allowed for ARM compiler,
    //          initialize data in class constructor.
    runningTime = 0;
    lastScanTime = runningTime;
    currentBeamAngle = 0;
    lastBeamAngle = currentBeamAngle;

    // Force background redraw
    UnvalidateBackground();

    // Initialize touchscreen
    ts.Init(((RK043FN48H*)GetDisplay())->DisplayWidth(), ((RK043FN48H*)GetDisplay())->DisplayHeight());

#ifndef _SDL_timer_h
    t.start();
#endif
}


void RadarDemo::Render()
{
    // Reset scan time after one full turn
    if (runningTime >= (lastScanTime + scanPeriod)) {
        lastScanTime = runningTime;
    }

    // Calculate actual beam position
    lastBeamAngle = currentBeamAngle;

#ifdef _SDL_timer_h
    uint32_t msTime = SDL_GetTicks();
#else
    int msTime = t.read_ms();
#endif
    currentBeamAngle = 2 * M_PI * (msTime - lastScanTime) / (float)scanPeriod;
    if(currentBeamAngle >= (2 * M_PI)) {
        currentBeamAngle -= 2 * M_PI;
    }

    RK043FN48H* display = (RK043FN48H*)GetDisplay();

    if(DetectTouch(window))
    {
        // Set timeout for the next touch detection
    }

    if(NeedsRedraw()) {

        display->SetActiveLayer(Background);
        display->Clear();

        // Set draw color before calling draw method
        display->SetDrawColor(0x80, 0, 0, 0xFF);
        DrawRangeButtons(raSize);

        display->SetDrawColor(0x40, 0x40, 0x40, 0xFF);
        DrawMarkers();

        display->SetDrawColor(0x80, 0x80, 0x80, 0xFF);
        DrawBorder();

        display->SetActiveLayer(Foreground);
        _needsRedraw = false;

    }

#ifdef _SDL_timer_h
    runningTime = SDL_GetTicks();
#else
    runningTime = t.read_ms();
#endif

    UpdateTargetsLocation(lastBeamAngle, currentBeamAngle, runningTime);

    // Redraw foreground
    display->Clear();
    DrawTracks();

    DrawRadarBeam(currentBeamAngle);
}


bool RadarDemo::IsRunning()
{
#ifdef _SDL_timer_h
    runningTime = SDL_GetTicks();
#else
    runningTime = t.read_ms();
#endif

    return demoTime > 0 ? runningTime <= demoTime : true;
}


void RadarDemo::AddSampleTargets(int count)
{
    const float minSpeed = 200; // [km/h]
    const float maxSpeed = 800; // [km/h]
    Target *target;

#ifdef _SDL_timer_h
    srand(SDL_GetTicks());
#else
    srand(t.read_us());
#endif

    for (int i = 0; i<count; i++) {
        // Generate target parameters
        float angle = 2 * M_PI * rand() / (float)RAND_MAX;
        float range = GetMaxRange() * rand() / (float)RAND_MAX;
        float speed = minSpeed + (maxSpeed - minSpeed) * rand() / (float)RAND_MAX;
        float direction = 2 * M_PI * rand() / (float)RAND_MAX;

        // Create a new target
        target = new Target(i + 100, speed, direction);
        target->SetLocationAngular(range, angle);

        // Put target on the list
        targetsList.push_back(*target);
    }
}


void RadarDemo::UnvalidateBackground()
{
    _needsRedraw = true;
}


bool RadarDemo::NeedsRedraw()
{
    return _needsRedraw;
}


bool RadarDemo::DetectTouch(Window screen)
{
    TS_StateTypeDef tsState;

    ts.GetState(&tsState);

    if (tsState.touchDetected) {

        if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] < screen.y1 + raSize) {
            if(ChangeRange(1))
                UnvalidateBackground();
        }

        else if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] > (screen.y2-raSize)) {
            if(ChangeRange(-1))
                UnvalidateBackground();
        }

        else {
            SetCenter(tsState.touchX[0], tsState.touchY[0]);
            UnvalidateBackground();
        }
        
        ts.ResetTouchData(&tsState);
        return true;
    }

    return false;
}