Чтение файла с SD
- Войдите на сайт для отправки комментариев
Ср, 14/11/2012 - 15:57
Доброго времени суток. Подключил СД-карту к Ардуино. Все примеры работают. Нужно прочитать такую структуру файла:
<?xml version="1.0" encoding="UTF-8"?> <map version="1.0" orientation="orthogonal" width="64" height="8" tilewidth="8" tileheight="8"> <tileset firstgid="1" name="texture" tilewidth="8" tileheight="8"> <image source="texture/texture.bmp" trans="ffffff" width="128" height="8"/> </tileset> <tileset firstgid="17" name="textureAnimated" tilewidth="8" tileheight="8"> <image source="texture/animation/texture.bmp" trans="ffffff" width="128" height="8"/> </tileset> <layer name="Твердость" width="64" height="8" opacity="0.18"> <data encoding="csv"> 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,0,0,0,0,0,0,0, 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,0,0,0,0,0,0,0 </data> </layer> <layer name="Блоки" width="64" height="8"> <data encoding="csv"> 15,15,15,15,0,0,0,0,0,0,0,0,0,15,15,15,15,15,3,15,15,15,15,15,0,0,0,0,0,0,0,0,15,15,15,15,15,3,15,15,15,15,15,0,0,0,0,0,0,0,0,15,15,15,15,15,3,15,15,15,15,15,0,0, 15,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,3,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,3,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,3,15,0,0,0,0,0,0, 15,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,2,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,2,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,2,0,0,0,0,0,0,0, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,3,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,3,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,3,0,0,0,0,0,0,0 </data> </layer> </map>
Мне нужно первое множество чисел передатьв переменную map1, а второе множество числе - нужно перевести в int и записать в переменную map2. При этом проигнорировав запятые и начало файла и д.р. Написал такой код-
mapFile = SD.open("map.tmx", FILE_READ); for(int yj; yj<566; yj++) {buffer = mapFile.read();} //Пропуск начала файла while(fy<8) { while(fx<64) { switch(mapFile.read()) { case '0': map1[fy][fx] = 0; buffer = mapFile.read(); break; //Если 0, то пишем 0 default: map1[fy][fx] = 1; buffer = mapFile.read(); if (buffer!=',') {buffer=mapFile.read();} break; //Если что-то другое, то пишем 1 } fx++; } buffer = mapFile.read(); //Пропуск символа перевода строки fy++; fx=0; } fx=0,fy=0; for(int yj; yj<91; yj++) {buffer = mapFile.read(); Serial.print(buffer);} //Пропуск букв разных.... while(fy<8) { while(fx<64) { char cmn[3] , *end; cmn[2] = '\0'; //Переменная под числа формата char ('1','0') cmn[0] = mapFile.read(); //Читаем первый символ числа buffer = mapFile.read(); if (buffer!=',') {cmn[1] = buffer;} else {cmn[1] = cmn[0]; cmn[0] = '0';} //Если затем следует запятая, то перемещаем символы числа. ('1',',' - '0','1') map2[fy][fx] = int(strtol(cmn, &end, 10)); Serial.println(int(strtol(cmn, &end, 10)), DEC); //Преобразование символов чисел в int. fx++; } buffer = mapFile.read(); //Пропуск символа перевода строки fy++; fx=0; } fx=0,fy=0; mapFile.close();
Все компилируется, но при запуске - программа выдает числа, близкие к оригиналу, но вразброс и как-то криво. По моему все должно работать. Прошу просмотреть программу с чистого взгляда.
Оригинальный файл.