ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / nds / atcodec_client.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include <ATcodec/ATcodec_api.h>
6 #include <VP_Stages/vp_stages_io_com.h>
7
8 #define AT_MESSAGES_HEADER "ATcodec/ATcodec_Messages_ex.h"
9
10 #include "ATcodec/ATcodec_api.h"
11 #include "VP_Os/vp_os_types.h"
12 #include "VP_Os/vp_os_thread.h"
13 #include "VP_Os/vp_os_delay.h"
14 #include "VP_Os/vp_os_print.h"
15 #include <Examples/common/atcodec_client.h>
16
17 #include <nds.h>
18
19 #ifndef AT_MESSAGES_HEADER
20 #error You need to define AT_MESSAGES_HEADER
21 #endif
22
23 extern AT_CODEC_MSG_IDS ids;
24
25 #define INTRA_CMD_DELAY 10
26
27
28 volatile int frame = 0;
29
30
31 //---------------------------------------------------------------------------------
32 void Vblank()
33 {
34   //---------------------------------------------------------------------------------
35   frame++;
36 }
37
38
39 //---------------------------------------------------------------------------------
40 void initNDS()
41 {
42   //---------------------------------------------------------------------------------
43   irqInit();
44   irqSet(IRQ_VBLANK, Vblank);
45   irqEnable(IRQ_VBLANK);
46   videoSetMode(0);      //not using the main screen
47   videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE);      //sub bg 0 will be used to print text
48   vramSetBankC(VRAM_C_SUB_BG);
49
50   SUB_BG0_CR = BG_MAP_BASE(31);
51         
52   BG_PALETTE_SUB[255] = RGB15(31,31,31);        //by default font will be rendered with color 255
53         
54   //consoleInit() is a lot more flexible but this gets you up and running quick
55   consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(31), (u16*)CHAR_BASE_BLOCK_SUB(0), 16);
56 }
57
58
59 //---------------------------------------------------------------------------------
60 THREAD_RET
61 thread_send_commands (THREAD_PARAMS data)
62 {
63   //---------------------------------------------------------------------------------
64   while(1)
65     {
66       ATcodec_Queue_Message_valist(ids.AT_MSG_ATCMD_CGMI);
67       vp_os_delay(INTRA_CMD_DELAY);
68       ATcodec_Queue_Message_valist(ids.AT_MSG_ATCMD_CGMI);
69       vp_os_delay(INTRA_CMD_DELAY);
70       //vp_os_thread_yield();
71
72       ATcodec_Queue_Message_valist(ids.AT_MSG_ATCMD_PM_QW,1);
73       vp_os_delay(INTRA_CMD_DELAY);
74       ATcodec_Queue_Message_valist(ids.AT_MSG_ATCMD_PM_QW,1);
75       vp_os_delay(INTRA_CMD_DELAY);
76       //vp_os_thread_yield();
77
78       ATcodec_Queue_Message_valist(ids.AT_MSG_ATCMD_PM_EXE,1,50);
79       vp_os_delay(INTRA_CMD_DELAY);
80       ATcodec_Queue_Message_valist(ids.AT_MSG_ATCMD_PM_EXE,1,50);
81       vp_os_delay(INTRA_CMD_DELAY);
82       //vp_os_thread_yield();
83     }
84 }
85
86
87 //---------------------------------------------------------------------------------
88 int main(int argc, char **argv)
89 {
90   //---------------------------------------------------------------------------------
91   touchPosition touchXY;
92   THREAD_HANDLE atcodec_test_handle;
93   THREAD_HANDLE atcodec_test_handle2;
94   THREAD_HANDLE cmds_handle;
95
96   initNDS();
97
98   AT_CODEC_FUNCTIONS_PTRS ptrs =
99     {
100       .init     = AT_CODEC_Client_init,
101       .shutdown = AT_CODEC_Client_shutdown,
102       .open     = AT_CODEC_Client_open,
103       .close    = AT_CODEC_Client_close,
104       .read     = AT_CODEC_Client_read,
105       .write    = AT_CODEC_Client_write,
106     };
107
108   ATcodec_Init_Library(&ptrs);
109
110   vp_os_thread_create(thread_ATcodec_Commands_Client, 0, &atcodec_test_handle);
111   vp_os_thread_create(thread_send_commands, 0, &cmds_handle);
112   vp_os_thread_create(thread_ATcodec_Commands_Server, 0, &atcodec_test_handle2);
113
114   vp_os_thread_join(cmds_handle);
115   vp_os_thread_join(atcodec_test_handle2);
116   vp_os_thread_join(atcodec_test_handle);
117
118   iprintf("      Hello DS dev'rs\n");
119   iprintf("     www.devkitpro.org\n");
120   iprintf("   www.drunkencoders.com");
121
122   while(1) {
123         
124     swiWaitForVBlank();
125     touchXY=touchReadXY();
126
127     // print at using ansi escape sequence \x1b[line;columnH
128     iprintf("\x1b[10;0HFrame = %d",frame);
129     iprintf("\x1b[16;0HTouch x = %04X, %04X\n", touchXY.x, touchXY.px);
130     iprintf("Touch y = %04X, %04X\n", touchXY.y, touchXY.py);
131         
132   }
133
134   return EXIT_SUCCESS;
135 }
136