3434/* middleware level*/
3535#include " u8g.h"
3636
37+ extern " C" {
3738#include " wm8978.h"
3839#include " wm8978i2s.h"
40+ }
3941#include " codec.h"
4042#include " mfcc.h"
4143
4244#include < stdlib.h>
43- #include "CppAddWrapper .h"
45+ #include " tensorflow/lite/micro/examples/voice_detection_experimental/main_functions .h"
4446
4547u8g_t u8g;
4648
@@ -51,86 +53,35 @@ void u8g_prepare(void) {
5153 u8g_SetFontPosTop (&u8g); /* set the reference position for the character and string draw procedure */
5254}
5355
54- /** first page in OLED */
55- void u8g_box_frame (uint8_t a ) {
56- //u8g_DrawStr(&u8g, 0, 0, "hello lnmm haha!"); /* draws a string at the specified x/y position */
57- u8g_DrawStr (& u8g , 0 , 0 , "hello Heathy" );
58- //u8g_DrawBox(&u8g, 5,10,20,10); /* draw a box (filled frame), starting at x/y position (upper left edge) */
59- //u8g_DrawBox(&u8g, 10+a,15,30,7);
60- //u8g_DrawStr(&u8g, 0, 30, "drawFrame");
61- //u8g_DrawFrame(&u8g, 5,10+30,20,10); /* draw a frame, starting at x/y position (upper left edge) */
62- //u8g_DrawFrame(&u8g, 10+a,15+30,30,7);
63- }
64-
65- /** second page in OLED */
66- void u8g_string (uint8_t a ) {
67- u8g_DrawStr (& u8g , 30 + a ,31 , " 0" );
68- u8g_DrawStr90 (& u8g , 30 ,31 + a , " 90" ); /* rotate string output by 90 degree */
69- u8g_DrawStr180 (& u8g , 30 - a ,31 , " 180" ); /* rotate string output by 180 degree */
70- u8g_DrawStr270 (& u8g , 30 ,31 - a , " 270" ); /* rotate string output by 270 degree */
71- }
72-
73- /** third page in OLED */
74- void u8g_line (uint8_t a ) {
75- u8g_DrawStr (& u8g , 0 , 0 , "drawLine" );
76- u8g_DrawLine (& u8g , 7 + a , 10 , 40 , 55 ); /* draw a line from (x1, y1) to (x2, y2) */
77- u8g_DrawLine (& u8g , 7 + a * 2 , 10 , 60 , 55 );
78- u8g_DrawLine (& u8g , 7 + a * 3 , 10 , 80 , 55 );
79- u8g_DrawLine (& u8g , 7 + a * 4 , 10 , 100 , 55 );
56+ void u8g_box_frame_path () {
57+ u8g_DrawStr (&u8g, 20 , 20 , " Detection Result:" ); /* draws a string at the specified x/y position */
58+ u8g_DrawStr (&u8g, 30 , 40 , " Pathological" );
8059}
8160
82- /** forth page in OLED */
83- void u8g_ascii_1 (void ) {
84- char s [2 ] = " " ;
85- uint8_t x , y ;
86- u8g_DrawStr (& u8g , 0 , 0 , "ASCII page 1" );
87- for ( y = 0 ; y < 6 ; y ++ ) {
88- for ( x = 0 ; x < 16 ; x ++ ) {
89- s [0 ] = y * 16 + x + 32 ;
90- u8g_DrawStr (& u8g , x * 7 , y * 10 + 10 , s );
91- }
92- }
61+ void u8g_box_frame_heal () {
62+ u8g_DrawStr (&u8g, 20 , 20 , " Detection Result:" ); /* draws a string at the specified x/y position */
63+ u8g_DrawStr (&u8g, 45 , 40 , " Healthy" );
9364}
9465
95- /** fifth page in OLED */
96- void u8g_ascii_2 (void ) {
97- char s [2 ] = " " ;
98- uint8_t x , y ;
99- u8g_DrawStr (& u8g , 0 , 0 , "ASCII page 2" );
100- for ( y = 0 ; y < 6 ; y ++ ) {
101- for ( x = 0 ; x < 16 ; x ++ ) {
102- s [0 ] = y * 16 + x + 160 ;
103- u8g_DrawStr (& u8g , x * 7 , y * 10 + 10 , s );
104- }
105- }
66+ /* * draw five pages in OLED */
67+ void draw_path () {
68+ u8g_prepare ();
69+ u8g_box_frame_path ();
10670}
10771
108- uint8_t draw_state = 0 ;
109-
110- /** draw five pages in OLED */
111- void draw (void ) {
72+ void draw_heal () {
11273 u8g_prepare ();
113- switch (draw_state >> 3 ) {
114- case 0 : u8g_box_frame (draw_state & 7 ); break ;
115- //case 1: u8g_string(draw_state&7); break;
116- //case 2: u8g_line(draw_state&7); break;
117- //case 3: u8g_ascii_1(); break;
118- //case 4: u8g_ascii_2(); break;
119- }
74+ u8g_box_frame_heal ();
12075}
12176
122- //extern DEV_BUFFER rx_buffer;
123- //static MFCC_STR mfcc_str;
77+ static MFCC_STR mfcc_str;
12478
12579void run_mic (MFCC_STR* mfcc_str, int8_t * mfcc_buffer)
12680{
127- mfcc_init (mfcc_str , NUM_MFCC_COEFFS , FRAME_LEN , MFCC_DEC_BITS );
128-
129- //printf("mfcc init\n");
130-
81+
13182 wm8978_init_func ();// Config the WM8978 through I2C.
13283
133- // printf("wm8978_init_func init\n");
84+ printf (" wm8978_init_func init\n " );
13485
13586 int16_t * rx_buffer1 = (int16_t *)malloc (sizeof (int16_t ) * 640 *18 );
13687
@@ -145,57 +96,82 @@ void run_mic(MFCC_STR* mfcc_str, int8_t* mfcc_buffer)
14596 printf (" The audio_buf malloc failed\n " );
14697 }
14798
148- // printf("codec init\n");
99+ printf (" codec init\n " );
149100
150101 CODEC_init (audio_buf, rx_buffer1);// Start I2S transfer and interrupts.
151102
152- // printf("codec end\n");
103+ printf (" codec end\n " );
153104
154105 extract_features (mfcc_str, audio_buf, mfcc_buffer);
155106
156- // for(int i = 0; i < NUM_FRAMES * NUM_MFCC_COEFFS; i++)
157- // printf("%d ", mfcc_buffer[i]);
158- // printf("\n");
107+ for (int i = 0 ; i < NUM_FRAMES * NUM_MFCC_COEFFS; i++)
108+ printf (" %d " , mfcc_buffer[i]);
109+ printf (" \n " );
159110
160111
161112 free (rx_buffer1);
162113 free (audio_buf);
163-
164114}
165115
166- void run_oled ()
116+ void oled_init ()
167117{
168118 EMBARC_PRINTF (" oled init\n " );
169119 u8g_InitComFn (&u8g, &u8g_dev_ssd1306_128x64_2x_i2c, U8G_COM_SSD_I2C);
170120 // EMBARC_PRINTF("u8glib\r\n");
171121 EMBARC_PRINTF (" Display Width: %u, Display Height: %u\r\n " , u8g_GetWidth (&u8g), u8g_GetHeight (&u8g));
172122 u8g_Begin (&u8g);
173123
124+ }
174125
175- printf ("draw oled\n" );
126+ void show_pathological_result ()
127+ {
176128 u8g_FirstPage (&u8g);
177- do {
178- draw ();
129+ do
130+ {
131+ draw_path ();
179132 } while (u8g_NextPage (&u8g));
180133
181134}
182135
136+ void show_healthy_result ()
137+ {
138+ u8g_FirstPage (&u8g);
139+ do
140+ {
141+ draw_heal ();
142+ } while (u8g_NextPage (&u8g));
143+
144+ }
145+
146+
183147/* * main entry for running ntshell */
184148int main (void )
185- {
149+ {
150+
151+ mfcc_init (&mfcc_str, NUM_MFCC_COEFFS, FRAME_LEN, MFCC_DEC_BITS);
152+ printf (" mfcc init\n " );
153+
186154 int8_t * mfcc_buffer = (int8_t *)malloc (sizeof (int8_t ) * NUM_FRAMES * NUM_MFCC_COEFFS);
187155 if (mfcc_buffer == NULL )
188156 {
189157 printf (" The mfcc_buffer malloc failed\n " );
190158 }
191159
192- MFCC_STR mfcc_str ;
193-
194- cppsetup ();
195160 run_mic (&mfcc_str, mfcc_buffer);
196161
197- cpploop (mfcc_buffer );
198- run_oled ();
162+ int result = run_model (mfcc_buffer);
163+
164+ printf (" result = %d\n " , result);
165+
166+ oled_init ();
167+
168+ if (result == 1 )
169+ {
170+ show_pathological_result ();
171+ }else if (result == 0 )
172+ {
173+ show_healthy_result ();
174+ }
199175
200176 free (mfcc_buffer);
201177
0 commit comments