Optimaze with new mbed os for study

Dependencies:   TS_DISCO_F746NG BSP_DISCO_F746NG Graphics

RadarDemo/RadarDemo.cpp

Committer:
karpent
Date:
2016-11-04
Revision:
1:5e49b46de1b0
Parent:
0:d8b9955d2b36
Child:
2:8db224cc1fcb

File content as of revision 1:5e49b46de1b0:

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

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

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(100);

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

    //SetCenter(0, 0);

    // 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();

#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(NeedsRedraw()) {

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

        // Set color for markers
        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;
}