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:
0:d8b9955d2b36
Child:
1:5e49b46de1b0

File content as of revision 0:d8b9955d2b36:

//
// 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()
{
    _needsRedraw = true;

    scanPeriod = 6000;
    demoTime = 0;   //2 * scanPeriod;

    AddSampleTargets(100);

    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;

#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;
    }

    //pc.printf("Sector :%3.1f, %3.1f\r\n", lastBeamAngle*180.f/M_PI, currentBeamAngle*180.f / M_PI) ;
    //pc.printf("Time : ms=%dms, r=%dms, l=%dms\r\n", msTime, runningTime, lastScanTime) ;

    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);
    
    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 = false;
}


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