import router from '@ohos.router'
import { buffer } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit';
import { common } from '@kit.AbilityKit';
import { sensor } from '@kit.SensorServiceKit';
// 获取应用文件路径
let context = getContext(this) as common.UIAbilityContext;
let filesDir = context.filesDir;
function Write_to_File(content: string): void {
// 文件不存在时创建并打开文件,文件存在时打开文件
let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 写入一段内容至文件
let writeLen = fs.writeSync(file.fd, content);
console.info("The length of str is: " + writeLen);
console.info("file path:" + filesDir);
// 创建一个大小为1024字节的ArrayBuffer对象,用于存储从文件中读取的数据
let arrayBuffer = new ArrayBuffer(102400);
// 设置读取的偏移量和长度
let readOptions: ReadOptions = {
offset: 0,
length: arrayBuffer.byteLength
};
// 读取文件内容到ArrayBuffer对象中,并返回实际读取的字节数
let readLen = fs.readSync(file.fd, arrayBuffer, readOptions);
// 将ArrayBuffer对象转换为Buffer对象,并转换为字符串输出
let buf = buffer.from(arrayBuffer, 0, readLen);
console.info("the content of file: " + buf.toString());
// 关闭文件
fs.closeSync(file);
}
@Entry
@Component
struct Face {
@State message: string = '采集数据集'
build() {
Row() {
Column() {
Text(this.message)
.fontSize(30)
Column({ space: 20 }) {
Row({ space: 20 }) {
Button('0->1', { type: ButtonType.Normal, stateEffect: true })
.borderRadius(8)
.backgroundColor(0x317aff)
.width(90)
.height(40)
.fontSize(15)
.onClick(() => {
console.info(`开始采集传感器数据`)
this.message = "数据集采集中"
try {
let ACC = 'ACC\n';
sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => {
ACC += 'X:' + data.x.toFixed(5) + ' ';
ACC += 'Y:' + data.y.toFixed(5) + ' ';
ACC += 'Z:' + data.z.toFixed(5) + '\n';
}, { interval: 1000000 });
setTimeout(() => {
sensor.off(sensor.SensorId.ACCELEROMETER);
this.message = "采集数据集"
Write_to_File(ACC);
}, 500);
} catch (error) {
let e: BusinessError = error as BusinessError;
console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
}
})
Button('1->2', { type: ButtonType.Normal, stateEffect: true })
.borderRadius(8)
.backgroundColor(0x317aff)
.width(90)
.height(40)
.fontSize(15)
.onClick(() => {
console.info(`开始采集传感器数据`)
this.message = "数据集采集中"
})
}
Row({ space: 20 }) {
Button('2->3', { type: ButtonType.Normal, stateEffect: true })
.borderRadius(8)
.backgroundColor(0x317aff)
.width(90)
.height(40)
.fontSize(15)
.onClick(() => {
import router from '@ohos.router'
import { sensor } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
@State message: string = '研电赛'
build() {
Row() {
Column({ space: 20 }) {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.message = "横竖屏翻转App"
})
Row({ space: 30 }){
// 添加一个按钮
Button({ type: ButtonType.Normal, stateEffect: true }) {
Text('录制数据集')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
// 按钮点击事件
// 跳转按钮绑定onClick事件,点击时跳转到第二页
.onClick(() => {
console.info(`开始跳转`)
// 跳转到第二页
router.pushUrl({ url: 'pages/sensor' }).then(() => {
console.info('跳转成功')
})
})
.backgroundColor('#ff31b1ff')
// 添加一个按钮
Button({ type: ButtonType.Normal, stateEffect: true }) {
Text('横竖屏旋转展示')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
// 按钮点击事件
// 跳转按钮绑定onClick事件,点击时跳转到第二页
.onClick(() => {
console.info(`开始跳转`)
// 跳转到第二页
router.pushUrl({ url: 'pages/rotation' }).then(() => {
console.info('跳转成功')
})
})
}
}
// 居中
.width('100%')
}
.height("100%")
}
}