app.add_static_files('/static', 'static', follow_symlink = True)
css = """
<style>
.number-icon {
    /* please replace by your own marker image, eg, /static/marker-icon-40x22.png! */
    background-image: url("http://peter.kleynjan.nl/css/marker-icon-40x22.png");
    background-size: 22px 40px;
    text-align: center;
    color: white;   
    font-size: 10pt;
    font-weight: 400;
    line-height: 18pt;
})
</style>"""

nicegui를 사용하면 파이썬에서 매우 쉽게 web 인터페이스를 구현할 수 있다.

자세한 사용 방법은 https://nicegui.io/ 페이지를 검색해보면 된다.

 

간혹 ROS 패키지를 작성하다가 UI가 필요한 경우에 많이 사용하게 되는데

때로는 지도에 어떤 마커라든지, 경로점 등을 표시하고 싶을 때가 있다. 이때 leaflet이라는 자바스크립트 플러그인을 활용하면 쉽게 구현할 수 있다. 

그런데 경우에 따라서는 기본 제공하는 마커 외에도 어떤 방향을 가진 마커 라던지 하는 특수한 마커를 표시하고 싶은 경우가 있을 것이다.

이 글에서는 nicegui의 leaflet 플러그인 기능을 확장해서 지도에 마커를 추가하고 활용하는 방법을 알아볼 것이다.

 

아래 그림은 지도에 표시된 마커에 회전된 sgv 아이콘이 적용된 화면이다.

 

leaflet이란?

leaflet은 오픈소스 기반의 자바스크립트 라이브러리로 널리 사용되는 지도 환경이다. 자세한 내용은 아래 링크를 참조한다.

https://leafletjs.com/

 

Leaflet — an open-source JavaScript library for interactive maps

Leaflet is a modern, lightweight open-source JavaScript library for mobile-friendly interactive maps.

leafletjs.com

nicegui에서 지도를 추가하는 방법은 다음 문서를 참조하면 된다.

https://nicegui.io/documentation/leaflet

 

ui.leaflet | NiceGUI

 

nicegui.io

leaflet에 마커 추가하기

간단하게 server.py라는 파일을 하나 만들고 다음과 같이 작성한 다음

# For nicegui
from nicegui import app, Client, ui, events

class WebServer:
    def __init__(self):
        with Client.auto_index_client:
            #create a row
            with ui.row().classes('w-full h-60 no-wrap').style("height: 100%"):
                self.ui_map = ui.leaflet(center=(37.500, 127.0363))
        ui.run()
            
if __name__ in { '__main__', '__mp_main__' } :
	WebServer()

 

python3 server.py 로 실행하면 지도가 하나 만들어진다.

 

이제 지도를 클릭했을 때 어떤 함수를 호출하도록 하는 루틴을 작성해보자

self.ui_map 을 선언한 곳 아래에 다음 내용을 추가하고

self.ui_map.on('map-click', self.handle_map_click)

이 코드가 호출할 함수를 다음과 같이 작성한다.

def handle_map_click(self, e: events.GenericEventArguments):
        lat = e.args['latlng']['lat']
        lng = e.args['latlng']['lng']
        print(f"lat:{lat}, long: {lng}")

 

그러면 지도를 클릭할때마다 터미널에 해당 위치의 위도, 경도를 출력하게 된다.

해당 위치에 마커를 추가하려면 handle_map_click 함수에 `self.ui_map.marker(latlng=(lat,lng))` 코드를 추가한다.

    def handle_map_click(self, e: events.GenericEventArguments):
        lat = e.args['latlng']['lat']
        lng = e.args['latlng']['lng']
        print(f"lat:{lat}, long: {lng}")
        self.ui_map.marker(latlng=(lat,lng))

이제 지도를 클릭하면 새로운 마커가 만들어진다.

 

그런데 이 마커는 아무런 표식도, 의미도 없으므로 어딘가에 해당 객체에 대한 정보를 가지고 있어야 한다.

그래서 __init__ 영역에서 리스트를 하나 선언하고 이 마커를 추가해주면 해당 마커에 대한 정보를 가지고 색인할 수 있다.

그러면 버튼을 하나씩 삭제할 수 도 있다.

    def __init__(self):
        with Client.auto_index_client:
            #create a row
            with ui.row().classes('w-full h-60 no-wrap').style("height: 100%"):
                self.ui_map = ui.leaflet(center=(37.500, 127.0363))
                self.ui_map.on('map-click', self.handle_map_click)
                ui.button('Remove', on_click=lambda :self.remove_marker())
        self.markers=[]
        ui.run()
        
    def handle_map_click(self, e: events.GenericEventArguments):
        lat = e.args['latlng']['lat']
        lng = e.args['latlng']['lng']
        print(f"lat:{lat}, long: {lng}")
        self.markers.append(self.ui_map.marker(latlng=(lat,lng))) 
        
    def remove_marker(self):
        if self.markers:
            self.ui_map.remove_layer(self.markers.pop(-1))

이제 마커를 추가했다가 remove버튼을 누르면 하나씩 마커가 제거되는 것을 확인할 수 있다.

 

인덱스가 있는 마커 추가하기

leaflet의 기본 마커는 아무런 표시가 없기때문에 지도에 어떤 순서로 클릭했는지 알 수 없다. 그럼 커스텀 된 마커를 추가할 수 있는 방법은 없을까?

leaflet은 기본적으로 자바스크립트로 구현된 플러그인이기 때문에 자바스크립트로 작성된 어떤것이든 추가할 수 있다.

그래서 마커를 클릭할때마다 인덱스를 추가하는 방법을 알아보자.

이 방법은 nicegui포럼에 다음 글을 참조하였다.

https://github.com/zauberzeug/nicegui/discussions/2361

 

Select marker · zauberzeug nicegui · Discussion #2361

Question Thank you for making NiceGui, realy nice! I am trying a leaflet app having markers. That works fine. But the markers represent objects with data properties. So I want to select a marker an...

github.com

글의 내용을 간단히 요약하면 다음과 같다.

먼저 server.py 가 있는 위치에서 static 이라는 폴더를 하나 만든 다음

그 폴더에 marker.js라는 파일을 만들고 다음 내용을 추가하자.

// markers.js test

var markers = { 'initialized': false, 'feature_group': null, 'icon': null };

function init_markers(map_id, icon_type) {
    // run after leaflet is loaded
    if ( ! markers.initialized ) {

        var map = getElement(map_id).map;       // see templates/index.html
        // map = window.app.$refs["r" + map_id].map;
        if ( ! map ) {
            console.log('Leaflet map object ' + map_id + ' not found');
            return;
        }  

        // group all markers in feature_group and add to map
        markers.feature_group = new L.featureGroup([]);
        map.addLayer(markers.feature_group);

        // define icon prototype, if necessary modify between adding markers
        markers.icon = L.DivIcon.extend({
            options: {
                className: "number-icon",
                iconSize: [22, 40],
                iconAnchor: [11,40],    // from top-left; add half the width. all of the height
                popupAnchor: [3, -40],
                html: ""
            }
        });

        markers.initialized = true;
    }
}

function add_marker(map_id, lat, lng, i, z=0) {
    init_markers(map_id);

    if ( ! markers.initialized ) {
        console.log('Markers not initialized / Leaflet probably not yet loaded');
        return;
    }

    var iconx = new markers.icon({ html: i });
    var marker = L.marker([lat, lng], {
        icon: iconx,
        zIndexOffset: z
    })
    .addTo( markers.feature_group )
    .on('click', handle_marker_click)

    // custom attribute, avoids closure in event handling & selector for deletion
    marker._ix = i;
}

function delete_marker(i) {
    if ( markers.initialized ) {
        markers.feature_group.eachLayer(function (layer) {
            if (layer._ix == i) {
                markers.feature_group.removeLayer(layer);
            }
        }
    )};
}

function delete_all_markers() { 
    if ( markers.initialized ) {
        markers.feature_group.remove();
        markers.feature_group = new L.featureGroup([]);
    }
}

// example event handlers
function handle_marker_click(e) { 
    // send ix back to Python
    emitEvent('marker-clicked', {'index': e.target._ix});
    // or do something locally
    delete_marker(e.target._ix);
};

또 static 폴더에 다음 마커 아이콘 파일을 다운로드 하고 파일이름을 marker-icon-40x22.png 라고 변경해준다.

 

그리고 이 파일을 server.py에 static 파일로 추가하기 위해 server.py 상단에 다음 내용을 한줄 추가해준다.

app.add_static_files("/static", "static")

이렇게 하면 어플에서 사용할 자바스크립트, 이미지 파일등을 브라우저의 static 경로로 로드하게 된다.

server.py 상단에 다음 내용도 추가해준다.

css = """
<style>
.number-icon {
    /* please replace by your own marker image, eg, /static/marker-icon-40x22.png! */
    background-image: url("/static/marker-icon-40x22.png");
    background-size: 22px 40px;
    text-align: center;
    color: white;   
    font-size: 10pt;
    font-weight: 400;
    line-height: 18pt;
})
</style>"""

class WebServer:
    def __init__(self):
        ui.add_head_html(css)
        ui.add_head_html("<script src='/static/markers.js'></script>")
        self.markers_id = 0

handle_map_click 함수를 다음과 같이 수정해주면 self.markers_id를 계속 추가해주면서 add_marker라는 함수를 호출하게 된다.

    def handle_map_click(self, e: events.GenericEventArguments):
        lat = e.args['latlng']['lat']
        lng = e.args['latlng']['lng']
        print(f"lat:{lat}, long: {lng}")
        ui.run_javascript(f"add_marker( {self.ui_map.id}, {lat}, {lng}, {self.markers_id})")
        self.markers_id+=1

이제 프로그램을 저장하고 다시 실행하면 다음과 같이 인덱스가 있는 마커가 표시되게 된다.

마커를 하나씩 제거하는것도 가능하다.

    def remove_marker(self):
        if self.markers_id > 0:
             self.markers_id -= 1
        ui.run_javascript(f"delete_marker( {self.markers_id})")

 

회전 가능한 마커 추가하기

마커는 어떤 위치만을 나타낼 수 있을 뿐 방향을 표시하지는 않는다.

비행기나 자동차의 위치를 표현할때는 방향도 같이 나오는 것이 필요하다. 

그럼 어떻게 해야 하는가?

 

위에 marker.js를 잘 살펴보면 답이 있다. 그리고 약간의 javascript 지식이 요구된다.

다행히 chatgpt는 이런 문제를 잘 알려준다.

몇번 시행착오 끝에 svg 라는 이미지 포맷을 활용하면 쉽게 구현할 수 있다.

역시 static 폴더 안에 이번에는 arrow.js 라는 파일을 하나 만들고 다음 코드를 추가한다.

// arrows.js test

var arrows = { 'initialized': false, 'feature_group': null, 'icon': null };

function init_arrows(map_id) {
    // run after leaflet is loaded
    if ( ! arrows.initialized ) {

        var map = getElement(map_id).map;       // see templates/index.html
        // map = window.app.$refs["r" + map_id].map;
        if ( ! map ) {
            console.log('Leaflet map object ' + map_id + ' not found');
            return;
        }  
        // Add markers to the map
        arrows.feature_group = L.layerGroup().addTo(map);

        arrows.initialized = true;
    }
}

function add_arrow(map_id, lat, lng, rotationAngle = 0, i=0, z=0) {
    init_arrows(map_id);
    if ( ! arrows.initialized ) {
        console.log('Markers not initialized / Leaflet probably not yet loaded');
        return;
    }
    // Create SVG icon as arrow and apply rotation
    var icon = L.divIcon({
        className: 'marker-icon',
        html:  `<svg width="50" height="60">\
                <!-- Main Icon -->
                <path d="M25 10 L10 50 L25 40 Z" fill="red"/>\
                <path d="M25 10 L25 40 L40 50 Z" fill="darkred"/>\
                </svg>`,
        iconAnchor: [25, 30],
        iconSize: [30, 40],
        iconRotation: rotationAngle
    });

    // Create marker
    var arrow = L.marker([lat, lng], {
        icon: icon,
        rotationAngle: rotationAngle
    }).addTo( arrows.feature_group )
    // Rotate using CSS
    var rotationStyle = 'transform: rotate(' + rotationAngle + 'deg);';
    arrow._icon.children[0].setAttribute('style', rotationStyle);
    arrow._ix = i;
    return arrow;
}

function delete_arrow(i) {
    if ( arrows.initialized ) {
        arrows.feature_group.eachLayer(function (layer) {
            if (layer._ix == i) {
                arrows.feature_group.removeLayer(layer);
            }
        }
    )};
}

function delete_all_arrows() { 
    if ( arrows.initialized ) {
        arrows.feature_group.remove();
        arrows.feature_group = new L.featureGroup([]);
    }
}

이번에는 icon 대신에 html에 svg 태그를 사용하여 임의의 아이콘을 만들고 

rotationStyle이라는 css 태그를 적용해서 아이콘을 회전하였다.

이 arrow.js 라는 코드를 static 폴더에 추가하기 위해 init에 다음 항목을 추가한다.

ui.add_head_html("<script src='/static/arrow.js'></script>")

이제 지도를 클릭했을 때 run_javascript 에서 add_marker 대신에 add_arrow 라는 함수를 호출하면 된다.

이때 회전하는 각도를 임의로 45로 넣으면 된다. 

ui.run_javascript(f"add_arrow( {self.ui_map.id}, {lat}, {lng}, {45}, {self.markers_id})")

이제 실행해보면 다음과 같이 지도에 45도 기울어진 화살표가 표시되는 것을 확인할 수 있다.

html 영역에 코드 확장하기

html의 기능을 알면 글자를 추가한다거나 하는 기능을 넣을 수 있다.

예를 들면 arrow.js의 html 영역에 다음과 같은 내용을 추가하면 

html:  `<svg width="50" height="60">\
        <!-- Main Icon -->
        <path d="M25 10 L10 50 L25 40 Z" fill="red"/>\
        <path d="M25 10 L25 40 L40 50 Z" fill="darkred"/>\
        <text x="25" y="35" text-anchor="middle" fill="white" font-size="12">\
        ${rotationAngle}</text>\
        </svg>`,

이렇게 각도를 표시할 수 있다.

 

그런데 글자 회전되어 있어서 보기가 어려우므로 글자에만 css의 transform 에다가 -값을 넎어서 되돌리는 코드를 넣어보자.

여기서 rotate 다음에 나오는 2개의 숫자는 회전하는 중심 x,y 좌표를 의미한다.

html:  `<svg width="50" height="60">\
        <!-- Main Icon -->
        <path d="M25 10 L10 50 L25 40 Z" fill="red"/>\
        <path d="M25 10 L25 40 L40 50 Z" fill="darkred"/>\
        <text x="25" y="35" text-anchor="middle" fill="white" font-size="12"\ 
        transform="rotate(${-rotationAngle} 25 30)">\
        ${rotationAngle}</text>\
        </svg>`,

이제 글자가 정상적으로 표시된다.

 

이 기능을 확장해보면 여러가지 다양한 기능을 추가해볼 수 있다.

예를 들면 그림자를 넣어본다던지...

var angle_rad = rotationAngle * Math.PI / 180.0
var dx = Math.sin(angle_rad) * 8
var dy = Math.cos(angle_rad) * 8
var icon = L.divIcon({
    className: 'marker-icon',
    html:  `<svg width="50" height="60">\
            <!-- Drop Shadow Filter -->
            <defs>
                <filter id="drop-shadow" x="-30%" y="-30%" width="200%" height="250%">
                    <feOffset in="SourceAlpha" dx="${dx}" dy="${dy}" result="offsetBlur"/>
                    <feGaussianBlur in="offsetBlur" stdDeviation="2" result="blur"/>
                    <feBlend in="SourceGraphic" in2="blur" mode="normal"/>
                </filter>
            </defs>
            <!-- Apply Drop Shadow Filter to the Icon -->
            <path d="M25 10 L10 50 L25 40 L40 50 Z" fill="black" opacity="0.4" filter="url(#drop-shadow)"/>
            <!-- Main Icon -->
            <path d="M25 10 L10 50 L25 40 Z" fill="red"/>\
            <path d="M25 10 L25 40 L40 50 Z" fill="darkred"/>\
            <!-- Text Area -->
            <rect x="12" y="25" width="28" height="12" fill="black" opacity="0.4" transform="rotate(${-rotationAngle} 25 30)"/>\
            <text x="25" y="35" text-anchor="middle" fill="white" font-size="12"\
            transform="rotate(${-rotationAngle} 25 30)">${Math.ceil(rotationAngle)}</text>\
            </svg>`,

 

Little Navmap (리틀네브맵) 에서 비행 경로 만들기

리틀네브맵은 MSFS의 비행경로(PLN)파일을 생성하는 프로그램이다. 

리틀네브맵 설치

리틀네브맵 프로젝트 페이지는 아래 링크에 있다.

https://albar965.github.io/littlenavmap.html 

 

Alex Projects - Little Navmap

Alex’ Projects ► Little Navmap Little Navmap Links ► Releases and Downloads ► Translation Packages ► Screenshots ► Screenshots of new 2.8 Features ► Screenshots of new 2.6 Features ► User manuals for Little Navmap and Little Navconnect in a

albar965.github.io

https://github.com/albar965/littlenavmap/releases

다음 깃허브 페이지에서 최신의 네브맵 설치파일을 다운로드 한다

 

Releases · albar965/littlenavmap

Little Navmap is a free flight planner, navigation tool, moving map, airport search and airport information system for Flight Simulator X, Microsoft Flight Simulator 2020, Prepar3D and X-Plane. - ...

github.com

Yes를 눌러 다음으로 진행한다.
MSFS 설치경로를 지정한다. (기본값으로 설정)

 

Little Navmap에서 생성한 플랜 모습

BushMissionGen(BMG) 프로그램으로 내보내기

이제 만들어진 PLN파일을 MSFS에서 인식할 수 있는 패키지로 만들기 위해 BMG 프로그램을 설치한다.

다운로드는 다음 경로에서 가능하다. (Flightsim.to 가입이 필요함)

https://flightsim.to/file/3681/bushmissiongen

 

BushMissionGen for Microsoft Flight Simulator | MSFS

Recent Changelog for 4.24 - poiStripHTML field to handle HTML tags in POI TTS dialogs and dialog entries. - Only create localization files for handled languages. Thanks to Frontech for bug reports, suggestions, testing and more!

flightsim.to

자세한 사용 방법은 다음 동영상을 참고한다.

https://youtu.be/JCfpbqIP2cQ?si=LU3LNlo5jhXLDJyq 

BushMissionGen(BMG)에서 plan 불러오기

미션 타입을 Landing challenge로 선택

적절한 챌린지 타입을 선택

저작자 이름, 제목, 간단한 설명등을 입력한다.

 

BushMissionGen에 넣을 인풋 파일

# Input file for BushMissonGen
#
# Auto-generated in v4.07

author=Kyubot
title=Gimhae landing VOR DME-A 18R
project=rkpk-rwy18r
version=1.0.0
location=Gimhae Intl
plane=Airbus A320 Neo Asobo
tailNumber=N9999DE
airlineCallSign=HL
flightNumber=1234
introSpeech=
simFile=runway.FLT
parkingBrake=0.00
description=Circle to land Gimhae 18R, ROK
loadingTip=Generated by BushMissionGen.
intro=Try to land one of the most challenging approach to Gimhae RWY 18. You are located 15 DME from KMH R225. Maintain heading 45 until D3 and enter downwind. Then make right turn to land runway 18R while avoiding terrains.
latitude=N35°0'11.82"
longitude=E128°43'23.49"
altitude=+4000.00
pitch=0
bank=0
heading=45
weather=.\WeatherPresets\FewClouds.WPR
season=SUMMER
year=2022
day=167
hours=12
minutes=30
seconds=0
missionType=landing
challengeType=Famous
velocity=200

#icao rw    name        type            LL                   alt
||CUST0|U|N35° 0' 11.82",E128° 43' 23.49"|+004000.00
||CUST1|U|N35° 0' 11.82",E128° 43' 23.49"|+004000.00
||CUST2|U|N35° 8' 41.63",E128° 53' 42.73"|+002700.00
||CUST3|U|N35° 11' 53.59",E128° 53' 12.74"|+001683.60
||CUST4|U|N35° 13' 17.91",E128° 55' 41.66"|+000854.25
RKPK|18R|RKPK|A|N35° 10' 50.01",E128° 56' 16.98"|+000010.00

WS2812란?

ws2812라는 명칭 보다는 네오픽셀이라는 이름으로 더 많이 알려진 이 LED는

한개의 LED 안에 빨강(R), 녹색(G), 파랑(B)색 LED 그리고 아주 작은 컨트롤러가 내장된 RGB형 LED이다.

 

일반적인 RGB형 LED들이 1개의 Common Cathod(음극) 또는 1개의 Common Anode(양극)으로 구성되어 

각각의 핀에 High 또는 Low 신호를 주어서 제어하는 방식이다. 보통은 PWM신호를 만들어서 밝기를 제어한다.

이 방식은 1개의 LED를 제어하기에는 편하지만 여러개의 LED를 제어하려면 LED갯수 x 3개 만큼의 PWM 핀이 필요하므로 MCU에 부담이 커진다.

 

WS2812는 각각의 LED에 컨트롤러를 내장하여 +, -외에 1개의 신호를 입력받아 R,G,B의 밝기를 제어할 수 있는 제품이다. 그리고 신호선에는 입력용 신호 (DI)와 출력용 신호(DO)가 있는데 이 신호선끼리 Input 과 Output을 연결하면 여러개의 LED를 동시에 제어할 수 있다.

 WS2812 연결방법

일반적인 연결 방식은 다음과 같다. 

LED3개를 일렬로 연결하는데 첫번째 LED의 DIN은 컨트롤러의 핀으로 연결하고 DOUT을 다음 LED의 DIN으로

그리고 DOUT은 다음 LED의 DIN으로 연결하는 방식이다.

이렇게 하면 몇개의 LED든 3가닥의 선으로 연결할 수 있게 되는데 이러한 연결 방식을 데이지 체인 혹은 Cascade 방식이라고도 한다.

WS2812의 연결 방식

제어 프로토콜

그럼 대체 1개의 선으로 어떻게 R,G,B 값을 보내고 또 그것을 여러개의 LED에 전달하는 것일까?

언뜻 매우 복잡한 신호가 필요할꺼 같지만 실은 단순히 0과 1의 조합일 뿐이다.

대신 1개의 선으로 보내야 하므로 0과 1을 구분하기 위해 미리 정해진 시간만큼의 High 신호와 Low 신호의 비율로 전송해야 하고 그 규격은 다음과 같다.

 

1개의 신호는 약 1.25uS +/-0.6uS로 되어있고 이 시간 안에 0.9uS만큼의 High 신호가 있으면 1로 인식하고 이보다 적은 0.35uS만큼의 High신호로 감지되면 0으로 인식한다.

RGB값은 각각 0~255까지 8bit로 표현할 수 있으므로 8x3 = 24개의 비트를 보내면 1개의 제어신호(Data)가 완성된다.

n개의 LED를 제어하는 경우 한번에 n개의 제어신호를 한꺼번에 보내야 한다. 

그리고 다음 신호는 최소 50uS 이후에 보낼 수 있다.

다음 다이어그램은 D1을 지난 신호가 DOUT을 거쳐 D2에 전달될때 첫번째 Data가 사라진 것을 확인할 수 있다.

그리고 D2를 지난 신호에는 D3의 Data만 남아있게 된다.  

이것은 마치 기차가 한개의 역을 지날때마다 객차가 1개씩 사라지는것과 같다.

이제 각각의 데이터를 살펴보면 R,G,B 순서가 아닌 G, R, B의 순서로 되어있음을 알 수 있다.

그리고 각 byte는 MSB(Most Significant Byte First)로 좌측 정렬되어있다.

제어 방법

STM32계열 MCU에서 이러한 데이터를 만드는 방법은 크게 2가지가 있다.

1. PWM을 변조하여 보내는 방법

2. SPI에서 MOSI(Master Output) 신호를 사용하는 방법

 

1번 방법을 구현하기 위해서는 PWM + DMA 방식이 주로 사용된다.

여기서는 2번 방법을 사용하여 구현하는 방법을 소개한다.

 

SPI통신은 원래 클럭신호마다 1개의 데이터를 실어서 보내는 제어 방식인데 8개의 클럭을 묶어서 High 신호와 Low 신호의 비율로 나누면 마치 PWM 신호인 것 처럼 보낼 수 있다. 

일반적인 SPI신호 방식

CubeMX 설정하기

SPI 핀을 설정하기 위해 CubeMX를 열고 SPI설정을 한다.

여기서는 STM32F429ZI 가 내장된 Nucleo F429ZI를 기준으로 한다.

클럭 설정은 다음과 같다.

그리고 SPI 설정은 다음과 같다.

RX핀은 사용하지 않으므로 Mode를 Transmit Only Master로 설정하고

 

Prescaler는 SPI 베이스 클럭을 나눈 값으로 1초당 Bit의 수를 설정한다.

SPI1, 4, 5, 6의 베이스 클럭은 APB2 클럭 또는 84MHz 클럭이 사용된다.

Figure 4. STM32F427xx and STM32F429xx block diagram

 

앞서 WS2812의 제어 방식에서 1개의 bit가 약 1.25uS가 되어야 하므로

1bit/1.25uS * 1000,000uS/1S = 800,000bit/S 또는 800KBits/s 가 된다.

그런데 실제로는 8개의 클럭을 묶어서 보내야 하므로 이 값을 8채배 하면

800K * 8 = 6.4MHz 가 된다. 최대한 근사한 값을 만들기 위해  Prescaler를 조정하면

84000000 / 16 = 5.25MHz 즉 5.25MBits/S 가 된다.

이 값을 Parameter settings에 적용한다.

DMA 설정하기

MCU는 LED제어만 하는게 아니므로 정확한 전송 타이밍을 보장하기 위해서는 DMA방식으로 전송하는 것이 권장된다.

설정 방법은 일반적인 설정과 동일하다.

코드 작성

이제 ws2812_spi.h 코드를 다음과 같이 작성한다.

#ifndef __WS2812_SPI_H_
#define __WS2812_SPI_H_

#include "stm32f4xx_hal.h"


/* Exported types ------------------------------------------------------------*/

/* Private defines -----------------------------------------------------------*/
#define WS2812_NUM_LEDS 8
#define WS2812_RESET_PULSE 60
#define WS2812_BUFFER_SIZE (WS2812_NUM_LEDS * 24 + WS2812_RESET_PULSE)
/* Exported constants --------------------------------------------------------*/

/* Exported macro ------------------------------------------------------------*/

/* Exported functions prototypes ---------------------------------------------*/
void WS2812_SPI_Init(SPI_HandleTypeDef *hspi);
void WS2812_Set(uint16_t led_no, uint8_t r, uint8_t g, uint8_t b);
void WS2812_SetAll(uint8_t r, uint8_t g, uint8_t b);
void WS2812_Refresh(void);
#endif

WS2812_NUM_LEDS는 연결된 LED의 갯수이다.

이제 ws2812_spi.c 코드를 작성한다.

#include "mcb_drv_ws2812_spi.h"

#define WS2812_FILL_BUFFER(COLOR) \
for( uint8_t mask = 0x80; mask; mask >>= 1 ) { \
	if( COLOR & mask ) { \
		*ptr++ = 0xF8; \
	} else { \
		*ptr++ = 0xC0; \
	} \
}
static SPI_HandleTypeDef *m_Hspi;
	  
uint8_t ws2812_buffer[WS2812_BUFFER_SIZE];

void WS2812_SPI_Init(SPI_HandleTypeDef *hspi)
{
 	m_Hspi = hspi;
	HAL_SPI_Transmit_DMA(m_Hspi, ws2812_buffer, WS2812_BUFFER_SIZE);
}

void WS2812_Set(uint16_t led_no, uint8_t r, uint8_t g, uint8_t b) {
	if(led_no > WS2812_NUM_LEDS-1) return;
    uint8_t * ptr = &ws2812_buffer[24 * led_no];
    WS2812_FILL_BUFFER(g);
    WS2812_FILL_BUFFER(r);
    WS2812_FILL_BUFFER(b);
}

void WS2812_SetAll(uint8_t r, uint8_t g, uint8_t b) {
    uint8_t * ptr = ws2812_buffer;
    for( uint16_t i = 0; i < WS2812_NUM_LEDS; i++) {
        WS2812_FILL_BUFFER(g);
        WS2812_FILL_BUFFER(r);
        WS2812_FILL_BUFFER(b);
    }
}

void WS2812_Refresh(void)
{
	HAL_SPI_Transmit_DMA(m_Hspi, ws2812_buffer, WS2812_BUFFER_SIZE);
}

이제 main 함수에서

WS2812_Init 함수를 SPI 핸들러(hspi1 또는 해당하는 spi 헨들러)와 함께 호출한 다음

WS2812_SPI_Init(&hspi4);

프로그램 어딘가에서 

WS2812_SetAll(R, G, B);

또는

WS2812_Set(0~(n-1) 사이의 LED 번호, R, G, B값); 

을 호출한 다음 WS2812_Refresh() 함수를 호출하면 ws2812_buffer의 값을 LED에 적용할 수 있다.

 

 

 

+ Recent posts